我已將簡單注入器配置爲我的DI容器。我跟着this。 我猜我的問題與註冊容器到SimpleInjectorServiceHostFactory
有關。 我正在使用Web應用程序上託管的wcf服務的類庫。我沒有.svc文件,但我配置了相對地址。我在哪裏做容器註冊?WCF:爲此對象依賴注入定義無參數構造函數簡單注入器
public static class ContainerBootstrap
{
public static Container Container { get; set; }
static ContainerBootstrap()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.RegisterSingleton<ITypeAdapterFactory, AutomapperTypeAdapterFactory>();
container.Register<IDepartmentService, DepartmentService>();
var typeAdapterFactory = container.GetInstance<ITypeAdapterFactory>();
TypeAdapterFactory.SetCurrent(typeAdapterFactory);
Container.Verify();
Container = container;
}
}
AutomapperTypeAdapterFactory:
public class AutomapperTypeAdapterFactory : ITypeAdapterFactory
{
public AutomapperTypeAdapterFactory()
{
var profiles = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.BaseType == typeof(Profile));
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
if (profile.FullName != "AutoMapper.SelfProfiler`2")
cfg.AddProfile(Activator.CreateInstance(profile) as Profile);
}
});
ContainerBootstrap.Container.Register<MapperConfiguration>(() => config);
ContainerBootstrap.Container.Register<IMapper>(() =>
ContainerBootstrap.Container.GetInstance<MapperConfiguration>()
.CreateMapper());
}
public ITypeAdapter Create() => new AutomapperTypeAdapter();
}
定製服務工廠
public class WcfServiceFactory : SimpleInjectorServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new SimpleInjectorServiceHost(ContainerBootstrap.Container, serviceType,
baseAddresses);
}
}
在虛擬主機提供商的web.config
<serviceActivations>
<add factory="Department.InstanceProviders.WcfServiceFactory"
service="Department.DepartmentService" relativeAddress="DepartmentService.svc" />
</serviceActivations>
你能解釋爲什麼你有自定義工廠?爲什麼不使用默認的簡單噴油器供應? –
這是一個設計決定,除非你想告訴我這是[email protected]的過程 – mkaris
在這一點上,我真的不知道這是否是例外的原因。但我會試着從默認開始看它是否是。 –