为什么我的WCF客户端在调用引发事件的WCF服务器中的方法后超时?
本文关键字:WCF 服务器 方法 超时 客户端 我的 调用 为什么 事件 | 更新日期: 2023-09-27 18:09:22
我有一个托管在Windows服务中的WCF服务。WCF服务中有5个方法将引发Windows服务订阅的事件。当事件被引发并且Windows服务捕获它时,Windows服务调用另一个WCF方法向所有连接的客户端发送消息。由于某种原因,当我在这个序列中发生所有这些时,会抛出一个错误:
This request operation sent to http://localhost:8731/Design_Time_Addresses/WCF/WCFService/ did not receive a reply within the configured timeout (00:00:29.9939996)....
下面是来自WCF服务的相关代码:
public event EventHandler<CustomEventArgs> CustomEvent;
private static readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();
public void SendMessageToClients(string msgType, string message)
{
subscribers.ForEach(delegate(IMessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnMessageReceived(msgType, message, DateTime.Now);
}
else
{
subscribers.Remove(callback);
}
});
}
public bool messageHost()
{
try
{
if (CustomEvent != null)
CustomEvent(null, new CustomEventArgs());
return true;
}
catch
{
return false;
}
}
和来自Windows服务的代码:
wcfService = new WCF.WCFService();
sHost = new ServiceHost(wcfService);
wcfService.CustomEvent += new EventHandler<WCF.CustomEventArgs>(wcfService_CustomEvent);
sHost.Open();
private void wcfService_CustomEvent(object source, WCF.CustomEventArgs e)
{
wcfService.SendMessageToClients("log", "CLient connected!!");
osae.AddToLog("client message"); //just writes to a log file
}
客户端调用这个:
wcfObj.messageHost();
下面是客户端配置:
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IWCFService" closeTimeout="00:00:10"
openTimeout="00:00:10" receiveTimeout="00:10:00" sendTimeout="00:00:30"
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="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</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>
<services>
<service name="WCF.WCFService" behaviorConfiguration="WCFBehavior">
<endpoint address="" binding="wsDualHttpBinding" 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="WCFBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
如果我从Windows服务中的事件中删除SendMessageToClients调用,它就会工作,并按预期记录"客户端消息"。此外,当抛出关于超时的错误时,我可以单击continue,客户端继续运行,并从主机接收到消息。为什么会出现这种奇怪的行为?
编辑:Marc带我在正确的地方搜索:http://www.codeproject.com/KB/WCF/WCF_Duplex_UI_Threads.aspx
这里的症状在我看来,好像发生了死锁,可能是由于同步上下文(WCF默认尊重同步上下文)。含义:向外的消息正在等待访问上下文,但是本身才是现有的上下文正在等待的东西。
最简单的方法是在工作线程上执行向外消息:ThreadPool.QueueUserWorkItem(delegate {
wcfObj.messageHost();
});
我相信你也可以配置WCF来改变它如何处理同步上下文,但我不记得如何…