• <small id='3Fryj'></small><noframes id='3Fryj'>

      1. <i id='3Fryj'><tr id='3Fryj'><dt id='3Fryj'><q id='3Fryj'><span id='3Fryj'><b id='3Fryj'><form id='3Fryj'><ins id='3Fryj'></ins><ul id='3Fryj'></ul><sub id='3Fryj'></sub></form><legend id='3Fryj'></legend><bdo id='3Fryj'><pre id='3Fryj'><center id='3Fryj'></center></pre></bdo></b><th id='3Fryj'></th></span></q></dt></tr></i><div id='3Fryj'><tfoot id='3Fryj'></tfoot><dl id='3Fryj'><fieldset id='3Fryj'></fieldset></dl></div>
        <tfoot id='3Fryj'></tfoot>
        <legend id='3Fryj'><style id='3Fryj'><dir id='3Fryj'><q id='3Fryj'></q></dir></style></legend>
        • <bdo id='3Fryj'></bdo><ul id='3Fryj'></ul>
      2. 在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者

        时间:2023-08-26
        <tfoot id='rDq5M'></tfoot>

          <i id='rDq5M'><tr id='rDq5M'><dt id='rDq5M'><q id='rDq5M'><span id='rDq5M'><b id='rDq5M'><form id='rDq5M'><ins id='rDq5M'></ins><ul id='rDq5M'></ul><sub id='rDq5M'></sub></form><legend id='rDq5M'></legend><bdo id='rDq5M'><pre id='rDq5M'><center id='rDq5M'></center></pre></bdo></b><th id='rDq5M'></th></span></q></dt></tr></i><div id='rDq5M'><tfoot id='rDq5M'></tfoot><dl id='rDq5M'><fieldset id='rDq5M'></fieldset></dl></div>

          <small id='rDq5M'></small><noframes id='rDq5M'>

              <tbody id='rDq5M'></tbody>
              <bdo id='rDq5M'></bdo><ul id='rDq5M'></ul>
              <legend id='rDq5M'><style id='rDq5M'><dir id='rDq5M'><q id='rDq5M'></q></dir></style></legend>
                • 本文介绍了在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个 ASP.NET Core 应用程序,我想在其中使用 RabbitMQ 消息.

                  I have an ASP.NET Core application where I would like to consume RabbitMQ messages.

                  我已在命令行应用程序中成功设置发布者和消费者,但我不确定如何在 Web 应用程序中正确设置.

                  I have successfully set up the publishers and consumers in command line applications, but I'm not sure how to set it up properly in a web application.

                  我想在 Startup.cs 中初始化它,但是一旦启动完成它当然就死了.

                  I was thinking of initializing it in Startup.cs, but of course it dies once startup is complete.

                  如何从网络应用以正确的方式初始化消费者?

                  How to initialize the consumer in a the right way from a web app?

                  推荐答案

                  使用单例模式让消费者/侦听器在应用程序运行时保留它.使用 IApplicationLifetime 接口在应用程序启动/停止时启动/停止使用者.

                  Use the Singleton pattern for a consumer/listener to preserve it while the application is running. Use the IApplicationLifetime interface to start/stop the consumer on the application start/stop.

                  public class Startup
                  {
                      public void ConfigureServices(IServiceCollection services)
                      {
                          services.AddSingleton<RabbitListener>();
                      }
                  
                  
                      public void Configure(IApplicationBuilder app)
                      {
                          app.UseRabbitListener();
                      }
                  }
                  
                  public static class ApplicationBuilderExtentions
                  {
                      //the simplest way to store a single long-living object, just for example.
                      private static RabbitListener _listener { get; set; }
                  
                      public static IApplicationBuilder UseRabbitListener(this IApplicationBuilder app)
                      {
                          _listener = app.ApplicationServices.GetService<RabbitListener>();
                  
                          var lifetime = app.ApplicationServices.GetService<IApplicationLifetime>();
                  
                          lifetime.ApplicationStarted.Register(OnStarted);
                  
                          //press Ctrl+C to reproduce if your app runs in Kestrel as a console app
                          lifetime.ApplicationStopping.Register(OnStopping);
                  
                          return app;
                      }
                  
                      private static void OnStarted()
                      {
                          _listener.Register();
                      }
                  
                      private static void OnStopping()
                      {
                          _listener.Deregister();    
                      }
                  }
                  

                  • 您应该注意应用的托管位置.例如,IIS 可以回收并阻止您的代码运行.
                  • 这种模式可以扩展到一个听众池.
                  • 这篇关于在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:指定公共交通中的发布超时 下一篇:等待一条带有超时的 RabbitMQ 消息

                  相关文章

                  最新文章

                • <small id='ftupO'></small><noframes id='ftupO'>

                    <bdo id='ftupO'></bdo><ul id='ftupO'></ul>
                • <tfoot id='ftupO'></tfoot>

                      <legend id='ftupO'><style id='ftupO'><dir id='ftupO'><q id='ftupO'></q></dir></style></legend>

                      <i id='ftupO'><tr id='ftupO'><dt id='ftupO'><q id='ftupO'><span id='ftupO'><b id='ftupO'><form id='ftupO'><ins id='ftupO'></ins><ul id='ftupO'></ul><sub id='ftupO'></sub></form><legend id='ftupO'></legend><bdo id='ftupO'><pre id='ftupO'><center id='ftupO'></center></pre></bdo></b><th id='ftupO'></th></span></q></dt></tr></i><div id='ftupO'><tfoot id='ftupO'></tfoot><dl id='ftupO'><fieldset id='ftupO'></fieldset></dl></div>