[英]WCF: no parameterless constructor defined for this object dependency injection Simple Injector
I have configured simple injector as my DI container. 我已将简单的进样器配置为我的DI容器。 I have followed this .
我跟着这个 。 I'm guessing my issue has got to do with Registering the container to the
SimpleInjectorServiceHostFactory
. 我猜我的问题与将容器注册到
SimpleInjectorServiceHostFactory
。 I'm using class library for wcf services hosted on web application. 我正在使用类库来托管Web应用程序上的wcf服务。 I have no .svc files but I have configured relative addresses.
我没有.svc文件,但我配置了相对地址。 Where do I do the container registration?
我在哪里进行集装箱登记?
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: 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();
}
Custom ServiceFactory 自定义ServiceFactory
public class WcfServiceFactory : SimpleInjectorServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new SimpleInjectorServiceHost(ContainerBootstrap.Container, serviceType,
baseAddresses);
}
}
In WebHost web.config 在WebHost web.config中
<serviceActivations>
<add factory="Department.InstanceProviders.WcfServiceFactory"
service="Department.DepartmentService" relativeAddress="DepartmentService.svc" />
</serviceActivations>
You should add a factory entry under the serviceActivation
element in the web.config file. 您应该在web.config文件中的
serviceActivation
元素下添加工厂条目。 This ensures that the SimpleInjectorServiceHostFactory
is used to activate the service. 这可确保使用
SimpleInjectorServiceHostFactory
来激活服务。
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,
SimpleInjector.Integration.Wcf"
relativeAddress="~/Service1.svc"
service="TestService.Service1" />
</serviceActivations>
</serviceHostingEnvironment>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.