Last active
May 10, 2016 09:17
-
-
Save ry8806/bfbcc94793185062b7616f3b3d8e6a50 to your computer and use it in GitHub Desktop.
WebJobs and DI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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