NServiceBus Configuration with Custom Container

怎甘沉沦 提交于 2019-12-24 20:36:24

问题


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) { }
}

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

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


回答1:


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

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

For example:

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


来源:https://stackoverflow.com/questions/18621897/nservicebus-configuration-with-custom-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!