自托管NServiceBus中的Host Web Api

时间:2014-01-18 09:40:23

标签: asp.net-web-api nservicebus

我正在寻找如何使用自托管的NServiceBus,它启动并托管Web Api。我似乎无法找到任何资源。有人想指点我的方向或提供一些例子吗?

由于

1 个答案:

答案 0 :(得分:0)

这是一个示例应用程序,它介绍了自我托管NServiceBus时应该知道的各种事项https://github.com/SimonCropp/NServiceBus.SelfHost

主要代码如下

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();
        }
    }
}

它还会引导您完成各种sc.exe命令以将其安装为服务