如何在ASP.NET Core Web API中使用MassTransit事件总线?

时间:2018-07-15 01:27:40

标签: c# asp.net asp.net-core asp.net-core-webapi masstransit

通过http://masstransit-project.com/MassTransit/usage/configuration.html按照说明配置MassTransit之后

您如何在

中使用(发布/订阅)它
  1. ASP.NET Core Web API控制器?
  2. 在界面服务中?

namespace App1.Message.API
{
    public class Startup
    {
        .....
        private IContainer container { get; set; }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
           ......
            var builder = new ContainerBuilder();
            builder.Register(c =>
            {
                return Bus.Factory.CreateUsingRabbitMq(sbc =>
                    sbc.Host("localhost", "/", h =>
                    {
                        h.Username("guest");
                        h.Password("guest");
                    })
                );
            })
            .As<IBusControl>()
            .As<IBus>()
            .As<IPublishEndpoint>()
            .SingleInstance();
            builder.Populate(services);
            container = builder.Build();
            return new AutofacServiceProvider(container);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
        {
            ......
            //resolve the bus from the container
            var bus = container.Resolve<IBusControl>();
            //start the bus
            var busHandle = TaskUtil.Await(() => bus.StartAsync());

            //register an action to call when the application is shutting down
            lifetime.ApplicationStopping.Register(() => busHandle.Stop());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

此示例可能是您最好的选择,这正是您要问的:

https://github.com/phatboyg/Sample-DotNetCore-DI