Last active
February 10, 2020 22:11
-
-
Save hd9/74893e28d9549cc58a59d8b0594cc5fa to your computer and use it in GitHub Desktop.
Sample debug/async initialization code for NServiceBus running as Azure WebJobs
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
// ********************************************** | |
// Fore more information, visit: | |
// https://blog.hildenco.com/2020/02/migrating-nservicebus-backends-to-azure.html | |
// ********************************************** | |
// Related docs: | |
// - NSB WebJobs: https://docs.particular.net/samples/azure/webjob-host/ | |
// - Azure WebJobs: https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started | |
public class Program | |
{ | |
static void Main() | |
{ | |
try | |
{ | |
var ec = new EndpointConfiguration("MyApp.NServiceBus.Backend"); | |
new EndpointConfig().Customize(ec); | |
Task.Run(async () => await StartAsync(ec)).Wait(); | |
} | |
catch (AggregateException e) | |
{ | |
Console.Error.WriteLine(e); | |
} | |
} | |
private static async Task StartAsync(EndpointConfiguration ec) | |
{ | |
IEndpointInstance endpoint = null; | |
endpoint = await Endpoint.Start(ec).ConfigureAwait(false); | |
#if DEBUG | |
Console.ReadLine(); | |
#else | |
var cs = ConfigurationManager.ConnectionStrings["MyApp.NServiceBus.Backend.ConnectionString"].ConnectionString; | |
var builder = new HostBuilder(); | |
var host = builder.Build(); | |
using (host) | |
{ | |
host.Run(); | |
}; | |
#endif | |
// shuts down NSB to properly release all acquired resources | |
await endpoint.Stop().ConfigureAwait(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment