gpt4 book ai didi

.net - 为什么 IChannelFactory 不定义无参数 CreateChannel?

转载 作者:行者123 更新时间:2023-12-02 17:43:39 25 4
gpt4 key购买 nike

为什么 IChannelFactory< TChannel> 没有接口(interface)定义无参数CreateChannel()

混凝土ChannelFactory< TChannel>另一方面,类确实有一个无参数CreateChannel()

出于可测试性/SoC 的原因,我想传递 IChannelFactory接口(interface),但这迫使我也传递 EndpointAddress用于 CreateChannel(EndpointAddress) .

作为解决方法,我创建了一个 IChannelFactory2< IChannel>其中确实有一个无参数 CreateChannel() .

但最终我只是好奇为什么要这样设计(通常 WCF 有合理的设计选择,但我只是懒得单独解决这个问题!)

最佳答案

ChannelFactory<T>.CreateChannel()方法只是一个辅助方法,通过 ChannelFactory<T> 的实现细节实现类。

如果你看ChannelFactory<T>源代码,你会看到这个:

public TChannel CreateChannel()
{
return this.CreateChannel(this.CreateEndpointAddress(this.Endpoint), (Uri) null);
}

CreateEndpointAddress 方法由抽象 ChannelFactory 内部实现。类,其中ChannelFactory<T>继承自:

internal EndpointAddress CreateEndpointAddress(ServiceEndpoint endpoint)
{
if (endpoint.Address == (EndpointAddress) null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((Exception) new InvalidOperationException(System.ServiceModel.SR.GetString("SFxChannelFactoryEndpointAddressUri")));
else
return endpoint.Address;
}

如您所见,端点信息已可通过 ChannelFactory<T>.Endpoint 获取。属性,通过the various constructors初始化ChannelFactory<T>的类(class)。事实上,这些构造函数允许使用者指定要调用的端点的详细信息。

由于接口(interface)无法定义构造函数,因此传递所需信息的最合适方法是通过将要使用它的方法的参数,即 CreateChannel方法。

关于.net - 为什么 IChannelFactory<T> 不定义无参数 CreateChannel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7137046/

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