我正在尝试在我通过解决方案中的一些服务使用的程序集中重用服务注册。我按照from the NServiceBus website列出的示例来实现解决方案。在此之后,除非我添加IWantCustomInitialization接口,否则我的Init方法(和IoC容器实现)似乎无法运行。当我实现了该接口时,我会获得异常(在SO问题here和here中列出)。我似乎无法让它工作,没有例外和我的MessageHandler中的依赖正在正确填充。这是我当前的EndpointConfig实现。
[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配置ObjectBuilderAdapter(此配置配置){ ConfigureCommon.With(config,new ObjectBuilderAdapter(new DependencyInjectorBuilder())); return config; } }
注意:当我使用INeedInitialization接口时,我在查找IStartableBus时会收到ComponentNotRegisteredException。
答案 0 :(得分:0)
当您尝试交换内置容器时,您需要在实现IConfigureThisEndpoint的同一个类中实现IWantCustomInitialization。
您可以使用自己的容器并在那里注册所有类型,并告诉NSB使用该容器。
例如:
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
public void Init()
{
var container = new ContainerBuilder().Build();
Configure.With()
.AutofacBuilder(container);
}
}