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

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

      1. .NET 中带有 Unity IOC 容器的 RabbitMQ

        时间:2023-08-24
      2. <legend id='ZKE4s'><style id='ZKE4s'><dir id='ZKE4s'><q id='ZKE4s'></q></dir></style></legend>
        <i id='ZKE4s'><tr id='ZKE4s'><dt id='ZKE4s'><q id='ZKE4s'><span id='ZKE4s'><b id='ZKE4s'><form id='ZKE4s'><ins id='ZKE4s'></ins><ul id='ZKE4s'></ul><sub id='ZKE4s'></sub></form><legend id='ZKE4s'></legend><bdo id='ZKE4s'><pre id='ZKE4s'><center id='ZKE4s'></center></pre></bdo></b><th id='ZKE4s'></th></span></q></dt></tr></i><div id='ZKE4s'><tfoot id='ZKE4s'></tfoot><dl id='ZKE4s'><fieldset id='ZKE4s'></fieldset></dl></div>
      3. <tfoot id='ZKE4s'></tfoot>

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

              <tbody id='ZKE4s'></tbody>
              <bdo id='ZKE4s'></bdo><ul id='ZKE4s'></ul>

                  本文介绍了.NET 中带有 Unity IOC 容器的 RabbitMQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Unity App Block 作为我的 IOC 容器,用于 WCF 项目的服务层.使用 Unity.WCF 库将其插入每个 WCF 服务时效果很好.

                  I am using Unity App Block as my IOC container for my service layer of a WCF project. This works quite well using the Unity.WCF library to plug it into each WCF service.

                  我最近将 RabbitMQ 引入了我的服务层,我目前正在使用使用"块来连接并添加到队列中.我不喜欢这样,我希望使用 HierachicalLifetimeManager 来创建和销毁我与 RabbitMQ 的连接,因为我需要它们?这听起来对吗?

                  I recently introduced RabbitMQ into my service layer and I am currently using the "using" blocks to connect and add to the queue. I dont like this though and am looking to use the HierachicalLifetimeManager to create and destroy my connection to RabbitMQ as I need them? Does this sound correct?

                  我正在寻找这方面的样本,或者至少是一些关于最佳方法的指导?(例如,我应该封装连接并根据需要注入每个服务吗?我将如何封装 RabbitMQ 消费者等?)

                  I'm looking for a sample of this, or atleast some guidance on the best approach? (e.g. Should I encapsulate the connection and inject into each service as needed? How would I encapsulate RabbitMQ consumer etc?)

                  推荐答案

                  我建议将 IConnection 注册为单例.

                  I would advise registering the IConnection as a singleton.

                  要将 IConnection 注册为 Unity 中的单例,您可以使用 ContainerControlledLifetimeManager,例如

                  To register the IConnection as a singleton in Unity you would use a ContainerControlledLifetimeManager, e.g.

                  var connectionFactory = new ConnectionFactory
                  {
                      // Configure the connection factory
                  };
                  unityContainer.RegisterInstance(connectionFactory);
                  
                  unityContainer.RegisterType<IConnection, AutorecoveringConnection>(new ContainerControlledLifetimeManager(),
                      new InjectionMethod("init"));
                  

                  AutorecoveringConnection 实例,一旦第一次解析,将保持活动状态,直到拥有的 UnityContainer 被释放.

                  The AutorecoveringConnection instance, once resolved for the first time will stay alive until the owning UnityContainer is disposed.

                  因为我们已经用<​​code>Unity注册了ConnectionFactory,它会自动注入到AutorecoveringConnection的构造函数中.InjectionMethod 确保在第一次解析 AutorecoveringConnection 时,调用 init 方法.

                  Because we have registered the ConnectionFactory with Unity, this will automatically be injected into the constructor of AutorecoveringConnection. The InjectionMethod ensures that the first time the AutorecoveringConnection is resolved, the init method is invoked.

                  关于您是否应该从您的服务中抽象出 RabbitMQ 的问题,我的回答是肯定的,但是我不会简单地创建一个 IMessageQueue 抽象.想想你使用消息队列的目的是什么,是推送状态吗?如果是这样,有一个 IStatusNotifier 接口和 RabbitMQ 的具体实现.如果要获取更新,请使用 IUpdateSource 接口和 RabbitMQ 的具体实现.你可以看到我的目标.

                  As for your question about whether you should abstract away RabbitMQ from your services, my answer would be yes, however I would not simply create an IMessageQueue abstraction. Think about what purpose you are using your message queue for, is it to push statuses? If so, have an IStatusNotifier interface with a concrete implementation for RabbitMQ. If it's to fetch updates, have an IUpdateSource interface with a concrete implementation for RabbitMQ. You can see where I am going with this.

                  如果您为 Message Queue 创建抽象,您将自己限制为仅在所有 Message Queue 实现中可用的功能.通过为不同的 Message Queue 实现使用不同的 IStatusNotifier 实现,您可以利用不同技术所独有的功能,同时在未来采用完全不同的技术时也能保持灵活性(例如,编写到 SQL 数据库或输出到控制台).

                  If you create an abstraction for a Message Queue, you are limiting yourself to features only available across all Message Queue implementations. By having a different implementation of IStatusNotifier for different Message Queue implementations, you are able to take advantage of features which are unique to different technologies while also remaining flexible in case completely different technologies are employed in future (e.g. Writing to a SQL database or outputting to a console).

                  这篇关于.NET 中带有 Unity IOC 容器的 RabbitMQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:RabbitMQ 连接错误没有一个指定的端点是可达的& 下一篇:Rabbitmq 消息到达时间戳

                  相关文章

                  最新文章

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

                  <tfoot id='G2Q5U'></tfoot>

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

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