gpt4 book ai didi

wcf - 如何确保 WCF ChannelFactory 使用 xml 配置中的 Binding 设置(忽略 MaxArrayLength)

转载 作者:行者123 更新时间:2023-12-04 16:03:09 25 4
gpt4 key购买 nike

如何确保 WCF ChannelFactory 使用 xml 配置中的 Binding 设置(忽略 MaxArrayLength)

嗨,我是 Wcf 新手,正在编写我的第一个 Wcf 服务和客户端。我不喜欢使用
生成配置的工具;我宁愿自己编写配置。我正在尝试的问题
要解决的是客户端通过 netTcp 与服务通信。该服务可以
可能返回非常大的有效载荷(大于默认值
readerQuotas.maxArrayLength)。我最初开发的组件在
字节流有效负载相对较低(即小于它认为的默认值约 16K)。
我可以通过创建绑定(bind)并设置 MaxArrayLength 以编程方式解决此问题
到足够大的值。但是,我需要能够在
xml配置。

我的 app.config(客户端)是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>

<client>
<endpoint address="net.tcp://localhost:9000/WcfDataService/RemoteDataRequesterService/"
binding="netTcpBinding" bindingConfiguration="unsecureNetTcpBinding"
contract="WcfDataServiceLib.IRemoteDataRequester"
name="DataRequesterEndpoint" />
</client>

<bindings>
<netTcpBinding>
<binding name="unsecureNetTcpBinding" maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="1000000" />
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>

</system.serviceModel>
</configuration>

创建客户端代理的代码如下:
private void Init()
{
var address = new EndpointAddress(@"net.tcp://localhost:9000/WcfDataService/RemoteDataRequesterService/");
const string endpointName = "DataRequesterEndpoint";

ChannelFactory<IRemoteDataRequester> factory = new ChannelFactory<IRemoteDataRequester>(
endpointName);

IRemoteDataRequester proxy = factory.CreateChannel(address);

// call business methods on proxy ...
}

请注意,代码通过变量“endpointName”链接到配置。

服务端配置(我认为这无关紧要,但为了完整性而包含在内):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfDataServiceLib.RemoteDataRequesterService" behaviorConfiguration="WcfDataServiceLib.RemoteDataRequesterServiceBehavior">
<host>
<baseAddresses>
<add baseAddress = "net.tcp://localhost:9000/WcfDataService/RemoteDataRequesterService" />
<add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfDataService/RemoteDataRequesterService/"/>
</baseAddresses>
</host>
<endpoint address ="" binding="netTcpBinding" bindingConfiguration="netTcpBindingConfig" contract="WcfDataServiceLib.IRemoteDataRequester">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfDataServiceLib.RemoteDataRequesterServiceBehavior">
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>

<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfig" receiveTimeout="00:00:30">
<readerQuotas maxArrayLength="1000000"/>
</binding>
<binding name="netTcpReliableSession" receiveTimeout="00:00:30" >
<reliableSession enabled="true"/>
</binding>
</netTcpBinding>
</bindings>

</system.serviceModel>
</configuration>

当我在服务返回大字节流的场景中运行客户端时,
抛出异常,异常内的消息是:

发生通信错误:套接字连接被中止。这可能是由于
由于处理您的消息时出错或远程主机超出接收超时,
或底层网络资源问题。本地套接字超时为 '00:00:59.9687494'

(这不是超时,因为错误会立即发生。)

如前所述,我可以通过编程方式解决此问题,如下所示:
var binding = new NetTcpBinding
{
ReaderQuotas = { MaxArrayLength = 10000000 }
};

return new ChannelFactory<IRemoteDataRequester>(binding);

这工作正常,但我需要通过配置来进行测试。

我还尝试了以下方法:
var binding = new NetTcpBinding("unsecureNetTcpBinding");
return new ChannelFactory<IRemoteDataRequester>(binding);

但这没有什么区别。

所以我的问题是为什么,当我从端点配置创建 channel 时,它包含一个绑定(bind)
MaxArrayLength 设置为合适的值,是否忽略此值?

许多问候。

好的,我找到了解决方案。配置一直在工作。但是,我提供的配置(“unsecureNetTcpBinding”)是我从说明 http 服务的代码示例中找到的(不是我正在设计的 net tcp 服务)。配置的流氓部分是
'安全模式="无"'
当我把它拿出来时,它起作用了。如果我更改 readerQuotas maxArrayLength 这将根据我的需要应用。我的代码工作的原因是因为我没有将安全模式设置为无。感谢您的意见和帮助。

最佳答案

我认为问题可能是在配置中你的数字只有 6 个零,而在代码中你有 7 个零。也许?

关于wcf - 如何确保 WCF ChannelFactory 使用 xml 配置中的 Binding 设置(忽略 MaxArrayLength),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1840425/

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