Giter Club home page Giter Club logo

webjobactivator's Introduction

WebJobActivator

Build Status WebJobActivator.Core WebJobActivator.Autofac
Build status

This provides core implementations of IJobActivator for Azure WebJob to be used by various IoC container libraries.

Overview

Azure WebJob SDK provides the IJobActivator interface so that we can easily manage dependencies. WebJobActivator.Core provides abstraction classes that are inherited and interfaces that are implemented by IoC container libraries. WebJobActivator.Autofac is one of implementations using WebJobActivator.Core.

Getting Started

Using WebJobActivator.Autofac

There is a WebJob function looks after a queue.

public class Functions
{
    private readonly Helper _helper;

    public Functions(Helper helper)
    {
        if (helper == null)
        {
            throw new ArgumentNullException(nameof(helper));
        }

        this._helper = helper;
    }

    public void Run([QueueTrigger("queue")] string message, TextWriter log)
    {
        var output = this._helper.GetValue(message);
        log.WriteLine(output);
    }
}

This class has a method of Run() without the static modifier. Therefore this class allows dependency injection. It currently has the Helper instance as its dependency.

Now, we need to prepare dependencies using Autofac's container builder. You can add dependencies individually, but it's easier to use RegisterModule() method.

public class WebJobModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<Helper>().AsSelf().InstancePerDependency();
        builder.RegisterType<Functions>().AsSelf().InstancePerDependency();
    }
}

Finally, we need to create an instance of AutofacJobHostBuilder to create a WebJob host and run it.

public static class Program
{
    // Initialises the WebJob host builder by injecting dependencies and configurations
    public static IJobHostBuilder WebJobHost { get; set; } = new AutofacJobHostBuilder(new WebJobModule())
                                                                 .AddConfiguration(p => p.UseDevelopmentSettingsIfNecessary())
                                                                 // Add as many number of extensions as possible
                                                                 .AddConfiguration(p => p.UseTimers())
                                                                 .AddConfiguration(p => p.UserSendGrid())
                                                                 ;
    public static void Main()
    {
        WebJobHost.BuildHost();
        WebJobHost.IsRunning = true;
        WebJobHost.RunAndBlock();
    }
}

By implementing WebJob host in this way, we can easily test all instances including the Program.cs class.

[TestClass]
public class ProgramTests
{
    [TestMethod]
    public void Given_JobHostBuilder_Main_ShouldReturn_Result()
    {
        // Mock IJobHostBuilder
        var builder = new Mock<IJobHostBuilder>();
        builder.SetupProperty(p => p.IsRunning);

        // Inject the mocked instance
        Program.WebJobHost = builder.Object;

        // Run the method
        Program.Main();

        // Check the result
        builder.Object.IsRunning.Should().BeTrue();
    }
}

Exetending WebJobActivator.Core

If you want to use another IoC container library, extending WebJobActivator.Core couldn't be easier. Here is how WebJobActivator.Autofac extends it.

Extending WebJobActivator

First of all, WebJobActivator class needs to be extended:

public class AutofacJobActivator : Core.WebJobActivator
{
    private readonly ContainerBuilder _builder;
    private IContainer _container;

    public AutofacJobActivator()
    {
        this._builder = new ContainerBuilder();
    }

    public override IWebJobActivator RegisterDependencies<THandler>(THandler handler = default(THandler))
    {
        if (handler == null)
        {
            this._container = this._builder.Build();
            return this;
        }

        ...

        this._container = this._builder.Build();
        return this;
    }

    public override T CreateInstance<T>()
    {
        return this._container.Resolve<T>();
    }
}

Extending RegistrationHandler

As WebJobActivator optionally requires RegistrationHandler, it would be great to extend it so that your preferred IoC container can easily register dependencies.

public class AutofacRegistrationHandler : RegistrationHandler
{
    public Action<ContainerBuilder> RegisterModule { get; set; }

    public Action<ContainerBuilder> RegisterType { get; set; }
}

For Autofac, the handler contains RegisterModule to register depdendencies all one go, or RegisterType to register dependencies individually.

Extending JobHostBuilder

Finally, in order to build JobHost with Autofac, we need to extend JobHostBuilder.

public class AutofacJobHostBuilder : JobHostBuilder
{
    public AutofacJobHostBuilder(IModule module)
        : this(GetJobHostConfigurationBuilder(module))
    {
    }

    private static JobHostConfigurationBuilder GetJobHostConfigurationBuilder(IModule module)
    {
        if (module == null)
        {
            return null;
        }

        var handler = new AutofacRegistrationHandler() { RegisterModule = p => p.RegisterModule(module) };
        var activator = new AutofacJobActivator().RegisterDependencies(handler);
        var builder = new JobHostConfigurationBuilder(activator);

        return builder;
    }
}

This is specific implementation for Autofac. As long as a module is ready, it builds JobHost for WebJob controller to use.

The example above is only for Autofac. However, if you prefer using SimpleInjector, Ninject or anything else for your IoC container library, you can easily integrate it like above.

Limitations

  • WebJobActivator won't support .NET Standard until the referencing Azure WebJob SDK supports it.

TO-DO Lists

Contributions

Your contributions are always welcome! All your work should be done in your forked repository. Once you finish your work with corresponding tests, please send us a pull request onto our dev branch for review.

License

WebJobActivator is released under MIT License

The MIT License (MIT)

Copyright (c) 2017 Aliencube Community

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

webjobactivator's People

Contributors

justinyoo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

agilewallaby

webjobactivator's Issues

.NET Core support

Many thanks for this project.
I tried to install WebJobActivator.Autofac in my Microsoft.Azure.WebJobs.Core project (netcoreapp2.1). But this ends with the following error:

Package 'WebJobActivator.Autofac 1.0.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project.

I'm not sure where to start. Is this WebJobActivator compatible with .NET core, too? Or do I have to fight with IJobActivator on my own?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.