在自托管 NServiceBus 中托管 Web Api [英] Host Web Api in self hosted NServiceBus

查看:63
本文介绍了在自托管 NServiceBus 中托管 Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢

推荐答案

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

Here is a sample app that walks though the various things you should know when self hosting 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 命令以将其安装为服务

It also walks you through the various sc.exe commands to install it as a service

这篇关于在自托管 NServiceBus 中托管 Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆