使用自定义容器的NServiceBus配置 [英] NServiceBus Configuration with Custom Container

查看:94
本文介绍了使用自定义容器的NServiceBus配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在通过解决方案中的一些服务使用的程序集中重用服务注册.我按照NServiceBus网站上的 列出的示例来实施该解决方案.在执行此操作后,除非添加IWantCustomInitialization接口,否则我的Init方法(和IoC容器实现)似乎无法正常运行.当我实现了该接口后,我得到了异常(列在SO问题中此处此处).我似乎无法正常工作,没有异常,并且MessageHandler中的依赖项已正确填充.这是我当前的EndpointConfig实现.

I am trying to re-use the service registrations in an assembly that I use through a few services in my solution. I follow the example listed from the NServiceBus website to implement the solution. When following that, unless I add the IWantCustomInitialization interface, my Init method (and IoC container implementation) appears not to function. When I have that interface implemented, I get exceptions (listed in SO questions here and here). I can't seem to get it to work that there are no exceptions AND the dependencies in my MessageHandler are being populated properly. Here is my current EndpointConfig implementation.

[EndpointSLA("00:00:30")]
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, UsingTransport<Msmq>, INeedInitialization {
    public void Init() {
        Configure.With().ObjectBuilderAdapter();
    }
}
public class ObjectBuilderAdapter : IContainer {
    readonly IDependencyInjector injector;

    public ObjectBuilderAdapter(IDependencyInjectionBuilder dependencyInjectionBuilder) {
        injector = dependencyInjectionBuilder.Create(); //This method does all the common service registrations that I am trying to re-use
        //injector.RegisterType<ExtractIncomingPrincipal, PrincipalExtractor>();
    }

    public void Dispose() {
        injector.Dispose();
    }

    public object Build(Type typeToBuild) {
        return injector.Resolve(typeToBuild);
    }

    public IContainer BuildChildContainer() {
        return new ObjectBuilderAdapter(new DependencyInjectorBuilder());
    }

    public IEnumerable<object> BuildAll(Type typeToBuild) {
        return injector.ResolveAll(typeToBuild);
    }

    public void Configure(Type component, DependencyLifecycle dependencyLifecycle) {
        injector.RegisterType(component);
    }

    public void Configure<T>(Func<T> component, DependencyLifecycle dependencyLifecycle) {
        injector.RegisterType(component);
    }

    public void ConfigureProperty(Type component, string property, object value) {
        if (injector is AutofacDependencyInjector) {
          ((AutofacDependencyInjector)injector).ConfigureProperty(component, property, value);
        } else {
            Debug.WriteLine("Configuring {0} for property {1} but we don't handle this scenario.", component.Name, property);
        }
    }

    public void RegisterSingleton(Type lookupType, object instance) {
        injector.RegisterInstance(lookupType, instance);
    }

    public bool HasComponent(Type componentType) {
        return injector.IsRegistered(componentType);
    }

    public void Release(object instance) { }
}

公共静态类扩展{ 公共静态配置ObjectBuilderAdapter(此配置配置){ ConfigureCommon.With(config,new ObjectBuilderAdapter(new DependencyInjectorBuilder())); 返回配置; } }

public static class Extensions { public static Configure ObjectBuilderAdapter(this Configure config) { ConfigureCommon.With(config, new ObjectBuilderAdapter(new DependencyInjectorBuilder())); return config; } }

注意:当我使用INeedInitialization接口时,在寻找IStartableBus时会收到ComponentNotRegisteredException.

Note: When I use the INeedInitialization interface, I get the ComponentNotRegisteredException when it's looking for IStartableBus.

推荐答案

尝试交换内置容器时,需要在实现IConfigureThisEndpoint的同一类中实现IWantCustomInitialization.

When you are trying to swap the built in container, then you need to implement IWantCustomInitialization in the same class that implements IConfigureThisEndpoint.

您可以使用自己的容器并在其中注册所有类型,并告诉NSB使用该容器.

You can use your own container and register all your types in there and tell NSB to use that container.

例如:

 public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public void Init()
    {
        var container = new ContainerBuilder().Build();
        Configure.With()
            .AutofacBuilder(container);
    }
}

这篇关于使用自定义容器的NServiceBus配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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