Skip to content

Instantly share code, notes, and snippets.

@ry8806
Last active May 10, 2016 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ry8806/bfbcc94793185062b7616f3b3d8e6a50 to your computer and use it in GitHub Desktop.
Save ry8806/bfbcc94793185062b7616f3b3d8e6a50 to your computer and use it in GitHub Desktop.
WebJobs and DI
using Autofac;
using Microsoft.Azure.WebJobs.Host;
namespace WebJob1
{
public class AutofacActivator : IJobActivator
{
private readonly IContainer _container;
public AutofacActivator(IContainer container)
{
_container = container;
}
public T CreateInstance<T>()
{
return _container.Resolve<T>();
}
}
}
using System.IO;
using Microsoft.Azure.WebJobs;
using WebJob1.Implementations;
namespace WebJob1
{
public class Functions
{
private readonly IStringGenerator _stringGenerator;
public Functions(IStringGenerator strGenerator)
{
// The Job Activator will inject the Concrete type (defined in the container) into this class
_stringGenerator = strGenerator;
}
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
{
log.WriteLine(message);
// This will print out "Hello" to our Azure WebJob log
log.WriteLine(_stringGenerator.GetWord());
}
}
}
using Microsoft.Azure.WebJobs;
namespace WebJob1
{
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
var config = new JobHostConfiguration
{
JobActivator = new AutofacActivator(ContainerConfig.GetContainer())
};
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment