我正在尝试在我的解决方案中通过一些服务使用的程序集中重新使用服务注册。我按照NServiceBus 网站上列出的示例来实施该解决方案。之后,除非我添加 IWantCustomInitialization 接口,否则我的 Init 方法(和 IoC 容器实现)似乎不起作用。当我实现该接口时,我会遇到异常(在此处和此处的 SO 问题中列出)。我似乎无法让它工作,没有异常并且我的 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 Configure ObjectBuilderAdapter(this Configure config) { ConfigureCommon.With(config, new ObjectBuilderAdapter(new DependencyInjectorBuilder())); 返回配置;} }
注意:当我使用 INeedInitialization 接口时,它在寻找 IStartableBus 时会收到 ComponentNotRegisteredException。