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 22, 2018 in Azure by null_void
• 3,220 points
196 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
374 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
774 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
107 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
135 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 in Azure by Prerna
• 1,940 points
35 views
+1 vote
4 answers

How do Connect to Azure website via FTP?

First set up your FTP credentials are ...READ MORE

answered Oct 23, 2018 in Azure by abc
141 views
0 votes
1 answer

How to upload a file on to Azure Blob storage without writing a code?

You can find the below tools useful ...READ MORE

answered Apr 13, 2018 in Azure by club_seesharp
• 3,450 points
68 views
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
117 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
755 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.