c# - 为什么我的 WCF 客户端在调用引发事件的 WCF 服务器中的方法后超时?

标签 c# .net wcf events windows-services

我在 Windows 服务中托管了一个 WCF 服务。 WCF 服务中有一个方法会引发 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 调用,它会正常工作,并且只会按预期记录“客户端消息”。此外,当抛出有关超时的错误时,我可以单击继续,客户端继续运行并收到来自主机的消息。为什么会发生这种奇怪的行为?

编辑: Marc 带领我在正确的地方搜索:http://www.codeproject.com/KB/WCF/WCF_Duplex_UI_Threads.aspx

最佳答案

这里的症状向我暗示好像发生了死锁,可能是由于同步上下文(WCF 默认情况下尊重同步上下文)。意思是:向外消息正在等待访问上下文,但本身现有上下文正在等待。 p>

最简单的调查方法是在工作线程上执行向外消息:

ThreadPool.QueueUserWorkItem(delegate {
    wcfObj.messageHost();
});

相信您还可以配置 WCF 来更改它处理同步上下文的方式,但我不记得如何...

关于c# - 为什么我的 WCF 客户端在调用引发事件的 WCF 服务器中的方法后超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7524927/

相关文章:

c# - 如何使用 DI 将自定义授权添加到 WCF Web 服务

c# - 在 .Net Compact Framework 中绘图时控制闪烁

c# - DbSet.Attach 试图为生成的标识列插入显式值

c# - Azure 引用了错误的 DLL

c# - SHGetImageList - 用于较小图标的 SHIL_JUMBO (32,32)

c# - 使用 WebRequest 和 WebResponse 时的 session 问题

.net - 在 .net 应用程序中使用 Cucumber 和 rspec 的教程

.net - .NET 4.6.1 中的默认 ServicePointManager.SecurityProtocol 值是什么?

asp.net - HttpHandler Hook * .svc请求

wcf - Windows 7 中的 kerberos 身份验证