.NET 中带有 Unity IOC 容器的 RabbitMQ [英] RabbitMQ with Unity IOC Container in .NET

查看:48
本文介绍了.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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