这就是我迄今为止所拥有的。我被困在概念/在哪里实施 IBus 注入/创建点(发布者)。我试图将发布者功能全部保留在项目中,而不是为其创建单独的服务。
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
这是我的第一次通过,所以我愿意接受建议和最佳实践建议:-)
剩下要做的事情:
- 创建一个具有适当设置的rabbitmq配置文件,定义队列名称和?
- 创建一个负责连接管理的消息处理程序是什么?
- 在应用程序启动时创建发布者,何时正确处理?
EasyNetQ Wrapper 替换 EasyNetQ 内部 IoC,Ninject 替换:
public class NinjectContainerWrapper: IContainer, IDisposable
{
public NinjectContainerWrapper()
{
}
//ninject container/kernal? here
//private readonly ISomeNinjectInterface container;
public TService Resolve<TService>() where TService : class
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService, TImplementation>()
where TService : class
where TImplementation : class, TService
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService>(Func<EasyNetQ.IServiceProvider, TService> serviceCreator) where TService : class
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAppSettings>().To<AppSettings>();
kernel.Bind<IDmpeService>().To<DmpeService>();
kernel.Bind<IPublisher>().To<DmpePublisher>();
kernel.Bind<IEasyNetQLogger>().To<GdmEasyNetQLogger>();
kernel.Bind<ILoggingService>().ToMethod(x =>
{
var scope = x.Request.ParentRequest.Service.FullName;
var log = (ILoggingService)LogManager.GetLogger(scope, typeof(LoggingService));
return log;
});
}
发布者类:
public class DmpePublisher: IPublisher
{
private readonly IEasyNetQLogger _logger;
private readonly IAppSettings _appSettings;
private readonly IBus bus = null;
public DmpePublisher(IEasyNetQLogger logger, IAppSettings appSettings)
{
this._logger = logger;
this._appSettings = appSettings;
// register our alternative container factory
RabbitHutch.SetContainerFactory(() =>
{
var ninjectContainer = new NinjectContainerWrapper();
// wrap it in our implementation of EasyNetQ.IContainer
//return new NinjectContainerWrapper(ninjectContainer);
});
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
}
public void PublishMessage(Messages.IMessage message)
{
throw new NotImplementedException();
//log post
//_logger.InfoWrite("Publishing message: {0}", message);
}
}