gpt4 book ai didi

c# - 为什么 Unity.Wcf 中的服务主机无法正确解析服务实例?

转载 作者:行者123 更新时间:2023-11-30 16:04:40 25 4
gpt4 key购买 nike

<分区>

我有一个统一容器:

var unityContainer = new UnityContainer();

配置如下:

unityContainer.RegisterType<IExampleDomainService, ExampleDomainService>();

unityContainer.RegisterType<IExampleWebService, ExampleWebService>();

ExampleWebService 类型及其构造函数如下所示:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ExampleWebService
{
public ExampleWebService(IExampleDomainService exampleDomainService)
{
this.exampleDomainService = exampleDomainService;
}

// ...

ExampleDomainService 没有定义构造函数(当我为此类型显式定义无参数构造函数时,问题是一样的)。

接下来,如 Unity.Wcf 的 documentation 中所述:

If you are hosting your WCF service within a Windows Service using a ServiceHost, replace the ServiceHost instance with the custom Unity.Wcf.UnityServiceHost. You will find that the UnityServiceHost takes in a Unity container as its first parameter but is otherwise identical to the default ServiceHost.

我做了以下事情:

 var host = new UnityServiceHost(unityContainer, typeof(ExampleWebService), baseAddress);

然而,这将引发 System.InvalidOperationException 并显示以下消息:

The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.

查看UnityServiceHost implementation at GitHub它将给定的 serviceType(在本例中为 typeof(ExampleWebService))直接传递给 WCF 的 native ServiceHost:

public sealed class UnityServiceHost : ServiceHost
{
public UnityServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
^^^^^^^^^^^
???????????

这显然会崩溃,因为 ServiceHost 对 Unity 及其容器一无所知,并且在缺少无参数构造函数时无法应对。

对于非 WAS/非 IIS 托管,Unity.Wcf 是否完全损坏或者(我希望)我做的事情完全错误?

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