不总是调用WCF异步方法

时间:2014-05-05 17:14:10

标签: c# .net wcf asynchronous

我有三个相同的WCF自托管Net.Tcp服务在三台独立的机器上监听。从我的客户端,我为每个服务器创建一个任务,其中包含我的WCF客户端实例(每个服务器一个),最终异步调用每个服务上的一个WCF操作。

我注意到的是服务电话不可靠。也就是说,在某些情况下,呼叫会起作用,而在其他情况下则不起作用。每次运行都在三台机器中的另一台机器上失败,但总是只有三台机器中的一台,从不是两台或全部三台。

我在所有服务器和我的客户端都启用了详细的WCF跟踪。对于失败的服务器,服务器端没有证据表明已建立连接。在客户端,服务跟踪日志甚至不指示已进行连接尝试。我的控制台调试日志显示代码确实被击中了。

这是我调用的异步方法:

public async Task InstallAsync(string installerPath, string commandLine)
{
    Debug.WriteLine("[{0}] InstallerAgentServiceClient.InstallAsync({1}, {2})", _endpointAddress,
        installerPath, commandLine);

    try
    {
        _proxy = new InstallerAgentService.InstallerAgentServiceClient(
            new InstanceContext(this), // assign callback class instance
            new NetTcpBinding(),
            _endpointAddress);

        _proxy.InnerChannel.Faulted +=
            (sender, args) =>
            {
                throw new Exception(string.Format("Inner channel faulted for {0}!", _endpointAddress));
            };

        _proxy.InnerChannel.Closed +=
            (sender, args) =>
            {
                throw new Exception(string.Format("Inner channel closed for {0}!", _endpointAddress));
            };

        await _proxy.InstallAsync(installerPath, commandLine);  // <=== *Sometimes* this doesn't work, and never throws an exception
    }
    catch (CommunicationException ce)
    {
        Logger.ErrorException(ce.Message, ce);
        _proxy.Abort();
    }
    catch (TimeoutException te)
    {
        Logger.ErrorException(te.Message, te);
        _proxy.Abort();
    }
    catch (Exception e)
    {
        Logger.ErrorException(e.Message, e);
        _proxy.Abort();
        throw;
    }
}

客户端app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <appSettings>
    ...
  </appSettings>

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Verbose, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Verbose">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:\logs\Cmc.Installer.App.svclog" />
    </sharedListeners>
    <trace autoflush="true">
      <listeners>
        <add name="logListener" type="System.Diagnostics.TextWriterTraceListener"
             initializeData="c:\logs\Cmc.Installer.App.log.txt" />
        <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>

  <system.serviceModel>
    <diagnostics>
      <messageLogging maxMessagesToLog="30000"
                      logEntireMessage="true"
                      logMessagesAtServiceLevel="true"
                      logMalformedMessages="true"
                      logMessagesAtTransportLevel="true"
                      logKnownPii="true">
      </messageLogging>
    </diagnostics>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IInstallerAgentService"
                 openTimeout="00:00:30"
                 sendTimeout="01:00:00"
                 closeTimeout="00:00:30"
                 receiveTimeout="10675199.02:48:05.4775807">
        </binding>  
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address=""
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IInstallerAgentService"
        contract="InstallerAgentService.IInstallerAgentService" name="NetTcpBinding_IInstallerAgentService">
        <identity>
          <userPrincipalName value="c2kbuild@mydomain.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

服务app.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <appSettings>
    <add key="MsiLog" value="C:\logs\Cmc.Installer.Agent.MSI.log.txt" />
  </appSettings>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Verbose, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Verbose">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="c:\logs\Cmc.Installer.Agent.svclog" />
    </sharedListeners>
    <trace autoflush="true">
      <listeners>
        <add name="logListener" type="System.Diagnostics.TextWriterTraceListener"
             initializeData="c:\logs\Cmc.Installer.Agent.Console.log.txt" />
        <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
      </listeners>
    </trace>
  </system.diagnostics>
  <system.serviceModel>
    <diagnostics>
      <messageLogging maxMessagesToLog="30000"
                      logEntireMessage="true"
                      logMessagesAtServiceLevel="true"
                      logMalformedMessages="true"
                      logMessagesAtTransportLevel="true">
      </messageLogging>
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding openTimeout="00:00:30"
                 sendTimeout="01:00:00"
                 closeTimeout="00:00:30"
                 receiveTimeout="10675199.02:48:05.4775807">
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="mexBehavior" name="Cmc.Installer.Agent.InstallerAgentService">
        <endpoint address=""
                  binding="netTcpBinding"
                  bindingNamespace="http://mydomain.com/installer/2014/05"
                  contract="Cmc.Installer.Agent.IInstallerAgentService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://*:8890" />
            <add baseAddress="net.tcp://*:8889" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
</configuration>

0 个答案:

没有答案
相关问题