Host Web Api in self hosted NServiceBus

人盡茶涼 提交于 2020-01-15 16:50:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!