Skip to content

Instantly share code, notes, and snippets.

@nicka-cme
Created June 21, 2017 19:55
Show Gist options
  • Save nicka-cme/999a6d6ebfbdedc912e1938888edbb1a to your computer and use it in GitHub Desktop.
Save nicka-cme/999a6d6ebfbdedc912e1938888edbb1a to your computer and use it in GitHub Desktop.
Masstransit webjob setup
// extract from from prgram.cs
public class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
public static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterModule<DatastoreModule>();
builder.RegisterModule<CommonModule>();
builder.RegisterModule<BusModule>(); // this AutoFac module handling configuring the MT Bus
builder.RegisterModule<WebJobModule>(); // this module registers the WebJobService class
var container = builder.Build();
var jobHostConfiguration = new JobHostConfiguration
{
JobActivator = new AutofacJobActivator(container)
};
if (jobHostConfiguration.IsDevelopment)
{
jobHostConfiguration.UseDevelopmentSettings();
}
var host = new JobHost(jobHostConfiguration);
var method = typeof(WebJobService).GetMethod("RunAsync");
Task.WaitAll(host.CallAsync(method, new { }));
}
}
// extract from from WebJobService.cs
public class WebJobService
{
private readonly IBusControl busControl;
public WebJobService(IBusControl busControl)
{
// buscontrol is Dependency injected in. Assuming you have already configured the MT Bus
this.busControl = busControl;
}
[NoAutomaticTrigger]
public async Task RunAsync(TextWriter log)
{
// construct the magic webjob shutdown watcher to help us later shut down nicely
var token = new WebJobsShutdownWatcher().Token;
// obviously start the bus
await this.busControl.StartAsync();
// wait and check this is to check whether the webjob has been asked us to shutdown nicely
while (!token.IsCancellationRequested)
Thread.Sleep(5000);
// obviously stop the bus
await this.busControl.StopAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment