如何正确设置WebApi,EasyNetQ和Ninject?

时间:2015-05-15 16:46:06

标签: asp.net-web-api rabbitmq easynetq

这就是我到目前为止所拥有的。我坚持概念/在哪里实现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);
    }
 }

0 个答案:

没有答案