C# WCF:没有为此对象依赖项注入简单注入器定义无参数构造函数
我已将simple injector配置为我的DI容器。我跟着。 我猜我的问题与将容器注册到C# WCF:没有为此对象依赖项注入简单注入器定义无参数构造函数,c#,wcf,simple-injector,C#,Wcf,Simple Injector,我已将simple injector配置为我的DI容器。我跟着。 我猜我的问题与将容器注册到SimpleInjectorServiceHostFactory有关。 我正在使用web应用程序上承载的wcf服务的类库。我没有.svc文件,但配置了相对地址。我在哪里办理集装箱登记 public static class ContainerBootstrap { public static Container Container { get; set; } static Contain
SimpleInjectorServiceHostFactory
有关。
我正在使用web应用程序上承载的wcf服务的类库。我没有.svc文件,但配置了相对地址。我在哪里办理集装箱登记
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;
}
}
在WebHost web.config中
您应该在web.config文件中的
serviceActivation
元素下添加工厂条目。这可确保使用SimpleInjectorServiceHostFactory
激活服务
添加一个工厂条目,这意味着我已经有了一个在我的web.config中定义的自定义条目。我没有看到什么,maybe@StevenCan你解释一下为什么你有一个定制工厂?为什么不使用simple injector提供的默认值呢?这是一个设计决策,除非您想告诉我这是整个过程exception@Ric.NetAt这一点我真的不知道这是否是例外的原因。但我会尝试从默认值开始,看看它是否是。
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);
}
}