【发布时间】:2012-01-21 19:56:19
【问题描述】:
我有一个托管 WCF 服务的 Windows 服务。我还有一个连接到它的客户端,并且来回发送消息。当我从客户端向服务发送消息时,客户端会捕获以下异常:
服务器没有提供有意义的回复;这可能是由于合同不匹配、会话过早关闭或内部服务器错误造成的。
这有点奇怪,因为我不希望服务直接回复客户端发送的消息。服务成功获取消息,但随后客户端抛出此异常并且似乎失去了与服务的连接。
这里是服务 app.config。忽略关于 RESTfull 服务的部分:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="OSAERest.api" behaviorConfiguration="MyServiceBehavior">
<endpoint address="http://localhost:8732/api"
binding="webHttpBinding"
contract="OSAERest.IRestService"
behaviorConfiguration="WebHttp"/>
</service>
<service name="WCF.WCFService" behaviorConfiguration="WCFBehavior">
<endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="WCFBinding" contract="WCF.IWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/WCF/WCFService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="WCFBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceTimeouts transactionTimeout="05:05:00" />
<serviceThrottling maxConcurrentCalls="500" maxConcurrentSessions="500"
maxConcurrentInstances="2147483647" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsDualHttpBinding>
<binding name="WCFBinding">
<security mode="None">
</security>
</binding>
</wsDualHttpBinding>
</bindings>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib" />
</assemblyBinding>
</runtime>
</configuration>
这里是客户端 app.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ServiceIP" value="127.0.0.1"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="lib" />
</assemblyBinding>
</runtime>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IWCFService" closeTimeout="00:01:00"
clientBaseAddress="http://localhost:8733/Design_Time_Addresses/WCF/WCFService/"
openTimeout="00:00:10" receiveTimeout="00:10:00" sendTimeout="00:00:10"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="None">
<message clientCredentialType="Windows" negotiateServiceCredential="true" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8731/Design_Time_Addresses/WCF/WCFService/"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IWCFService"
contract="WCFService.IWCFService" name="WSDualHttpBinding_IWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
为什么客户端会捕捉到这个异常?
编辑:
如果有帮助,这是我将消息发送到服务的 C# 代码:
WCFServiceClient wcfObj;
EndpointAddress ep = new EndpointAddress("http://localhost:8731/Design_Time_Addresses/WCF/WCFService/");
InstanceContext context = new InstanceContext(this);
wcfObj = new Manager_WPF.WCFService.WCFServiceClient(context, "WSDualHttpBinding_IWCFService", ep);
wcfObj.Subscribe();
wcfObj.messageHost(message);
【问题讨论】:
-
每次客户端调用服务或发送几条消息后(或多个客户端运行时)都会发生这种情况
-
只连接了一个客户端。每次我向服务发送消息时都会发生这种情况。
-
能否出示合同,服务是否在处理请求期间尝试向客户端进行回调?
-
你真的需要创建一个instancecontext并将它传递给WCFServiceClient。我猜只是使用 WCFServiceClient obj = new WCFServiceClient();就足够了,因为默认情况下它会映射到客户端 app.config 中指定的关联端点,除非您有多个同名端点。
-
您解决了这个问题吗?
标签: c# .net wcf client-server wcf-client