问题
I'm looking for how to use a self hosted NServiceBus, which starts and hosts Web Api. I can't seem to find any resources on it. Anyone care to point me to a direction or provide some examples?
Thanks
回答1:
Here is a sample app that walks though the various things you should know when self hosting NServiceBus https://github.com/SimonCropp/NServiceBus.SelfHost
The main code is as follows
class SelfHostService : ServiceBase
{
IStartableBus bus;
static void Main()
{
using (var service = new SelfHostService())
{
// so we can run interactive from Visual Studio or as a service
if (Environment.UserInteractive)
{
service.OnStart(null);
Console.WriteLine("\r\nPress any key to stop program\r\n");
Console.Read();
service.OnStop();
}
else
{
Run(service);
}
}
}
protected override void OnStart(string[] args)
{
LoggingConfig.ConfigureLogging();
Configure.Serialization.Json();
bus = Configure.With()
.DefaultBuilder()
.UnicastBus()
.CreateBus();
bus.Start(() => Configure.Instance.ForInstallationOn<Windows>().Install());
}
protected override void OnStop()
{
if (bus != null)
{
bus.Shutdown();
}
}
}
It also walks you through the various sc.exe commands to install it as a service
来源:https://stackoverflow.com/questions/21202411/host-web-api-in-self-hosted-nservicebus