How to perform dependency injection using Azure WebJobs SDK

0 votes

The problem is that the Azure WebJobs SDK supports only public static methods as job entry-points which means there is no way of implementing constructor/property injection.

I am unable to find anything about this topic in official WebJobs SDK documentation/resources. The only solution that I came across is based on service locator (anti) pattern described on this post here.

Is there a good way to use "proper" dependency injection for projects based on Azure WebJobs SDK?

Aug 23, 2018 in Azure by null_void
• 3,220 points
2,107 views

1 answer to this question.

0 votes

Azure WebJobs SDK now supports instance methods. Combining this with a custom IJobActivator allows you to use DI.

First, create the custom IJobActivator that can resolve a job type using your favourite DI container:

public class MyActivator : IJobActivator
{
    private readonly IUnityContainer _container;

    public MyActivator(IUnityContainer container)
    {
        _container = container;
    }

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

You need to register this class using a custom JobHostConfiguration:

var config = new JobHostConfiguration
{
    JobActivator = new MyActivator(myContainer)
};
var host = new JobHost(config);

Then, you can use a simple class with instance methods for your jobs (here I'm using Unity's constructor injection feature):

public class MyFunctions
{
    private readonly ISomeDependency _dependency;

    public MyFunctions(ISomeDependency dependency)
    {
        _dependency = dependency;
    }

    public Task DoStuffAsync([QueueTrigger("queue")] string message)
    {
        Console.WriteLine("Injected dependency: {0}", _dependency);

        return Task.FromResult(true);
    }
}

Hope it helped!

answered Aug 23, 2018 by club_seesharp
• 3,450 points

Related Questions In Azure

+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
1,934 views
0 votes
1 answer

How to choose between Azure Webjobs and Azure Functions?

There are a couple options here within ...READ MORE

answered Jul 9, 2018 in Azure by club_seesharp
• 3,450 points
6,691 views
0 votes
1 answer

How to import Azure SQL backup (.bacpac) to LocalDB using Visual Studio?

If you have this folder on your ...READ MORE

answered Sep 24, 2018 in Azure by null_void
• 3,220 points
2,181 views
0 votes
1 answer

How to perform Full Text Search on SQL Azure?

Azure Search is a search-as-a-service solution that ...READ MORE

answered Sep 25, 2018 in Azure by null_void
• 3,220 points
1,502 views
0 votes
2 answers
0 votes
1 answer

How to automatically install Application Initialization in Azure Web Role (SDK v1.8) ?

Follow these steps - Install the feature using ...READ MORE

answered Apr 11, 2019 in Azure by Prerna
• 1,960 points
668 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to add an App Settings to existing Azure Web Application using Azure Power Shell?

The example is for slot-specific settings, if ...READ MORE

answered Jun 13, 2018 in Azure by club_seesharp
• 3,450 points
1,252 views
0 votes
2 answers

How to process a file using Azure Function?

You don't really need a http trigger ...READ MORE

answered Jun 18, 2018 in Azure by club_seesharp
• 3,450 points
6,912 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP