【问题标题】:Host Web Api in self hosted NServiceBus在自托管的 NServiceBus 中托管 Web Api
【发布时间】:2014-02-24 19:31:52
【问题描述】:

我正在寻找如何使用自托管的 NServiceBus,它启动并托管 Web Api。我似乎找不到任何关于它的资源。有人愿意为我指明方向或提供一些示例吗?

谢谢

【问题讨论】:

  • 查看 MVC 示例应用程序。设置应该相同。
  • 我认为@stephenl 指的是the video store sample
  • 是 NServiceBus 托管 WebApi 还是 WebApi 托管 NServiceBus?通常是第二个,但这个问题似乎是倒退的。

标签: asp.net-web-api nservicebus


【解决方案1】:

这是一个示例应用程序,它介绍了在自托管 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 命令以将其安装为服务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2019-02-24
    • 2015-01-09
    • 2017-11-17
    • 2016-12-15
    相关资源
    最近更新 更多