gpt4 book ai didi

c# - WCF:没有为此对象定义无参数构造函数依赖注入(inject)简单注入(inject)器

转载 作者:行者123 更新时间:2023-11-30 15:21:47 24 4
gpt4 key购买 nike

我已将简单注入(inject)器配置为我的 DI 容器。我关注了this .我猜我的问题与将容器注册到 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;
}
}

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);
}
}

在 WebHost 的 web.config 中

<serviceActivations>
<add factory="Department.InstanceProviders.WcfServiceFactory"
service="Department.DepartmentService" relativeAddress="DepartmentService.svc" />
</serviceActivations>

最佳答案

您应该在 web.config 文件中的 serviceActivation 元素下添加一个工厂条目。这确保 SimpleInjectorServiceHostFactory 用于激活服务。

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,
SimpleInjector.Integration.Wcf"
relativeAddress="~/Service1.svc"
service="TestService.Service1" />
</serviceActivations>
</serviceHostingEnvironment>

关于c# - WCF:没有为此对象定义无参数构造函数依赖注入(inject)简单注入(inject)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36765228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com