WCF请求期间的CommunicationException,底层连接已关闭

时间:2015-07-01 11:47:06

标签: c# web-services wcf exception web-config

这个问题与System.ServiceModel.CommunicationException: The underlying connection was closed重复,但并没有解决我的问题。

我正在使用写入和读取数据库的WCF服务。但在某些情况下,请求会通过一个CommunicationException来解除基础连接的关闭。没有进一步的细节。

我启用了跟踪,但日志中没有显示有关此请求的条目。我尝试将MaxItemsInObjectGraph设置为65535,但没有任何改变。也许我的配置配置错误,有人可以看看它们吗?

的Web.config

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

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true" >
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
      <source name="myUserTraceSource"
              switchValue="Information, ActivityTracing">
        <listeners>
          <add name="xml"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml"
           type="System.Diagnostics.XmlWriterTraceListener"
           initializeData="Error.svclog" />
    </sharedListeners>
  </system.diagnostics>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- Legen Sie die Werte unten vor der Bereitstellung auf "false" fest, um die Veröffentlichung von Metadateninformationen zu vermeiden. -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- Damit in Fehlern Ausnahmedetails zum Debuggen angezeigt werden, legen Sie den Wert unten auf "true" fest. Legen Sie ihn vor der Bereitstellung auf "false" fest, um die Veröffentlichung von Ausnahmeinformationen zu vermeiden. -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <!-- Umgeht eine CommunicationException, die auftritt, wenn der ObjectGraph überläuft -->
          <dataContractSerializer maxItemsInObjectGraph="65535"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IDBUpdater" 
                 closeTimeout="00:01:00" 
                 openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" 
                 sendTimeout="00:01:00"
                 maxBufferSize="2147483647" 
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647"
                 allowCookies="false" 
                 bypassProxyOnLocal="false" 
                 hostNameComparisonMode="StrongWildcard"
                 messageEncoding="Text" 
                 textEncoding="utf-8" 
                 transferMode="StreamedResponse" 
                 useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" 
                        maxStringContentLength="2147483647" 
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647" 
                        maxNameTableCharCount="2147483647" />
        </binding>
        <binding maxReceivedMessageSize="2147483647" allowCookies="true">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" 
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647"
      maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <protocolMapping>
      <remove scheme="http" />
      <add scheme="http" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDBUpdater" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        Um das Stammverzeichnis der Webanwendung beim Debuggen auszuwählen, legen Sie den Wert unten auf "true" fest.
        Legen Sie ihn vor der Bereitstellung auf "false" fest, um die Veröffentlichung von Informationen über den Webanwendungsordner zu vermeiden.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

App.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IDBUpdater" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647"
          maxReceivedMessageSize="2147483647" useDefaultWebProxy="true" />
        <binding allowCookies="true" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://10.1.58.48/DBUpdate/UpdateService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDBUpdater"
        contract="UpdateService.IDBUpdater" />
    </client>

  </system.serviceModel>
</configuration>

我对WCF配置不是很熟悉。有什么不对或为什么我一直收到此错误消息?

代码示例:

string cmd = @"SELECT trainer.TID, anrede.Anrede, trainer.Titel, trainer.Vorname, trainer.Nachname, trainer.Festnetz, trainer.Mobil, trainer.Email, trainer.Trainerstatus, trainer.Honorar
                           FROM trainer
                           left join anrede on trainer.Anrede = anrede.ID
                            Group by trainer.TID";
var client = new DBUpdaterClient();
dt = client.ExecuteReaderUGV(Helpers.GetUsername(), cmd);
调用ExecuteReaderUGV()后出现

错误,生成的数据表对象应该有大约3500个条目。使用较小的数据表(&lt; 50),它可以正常工作。

错误讯息: 底层连接已关闭:连接意外关闭。

更新

我们将问题跟踪到关键字left join。当被inner join替换时,它可以正常工作。但是,我们不清楚为什么会发生这种情况。当我们在没有Web服务的情况下访问数据库并执行带有left join的sql命令时,它可以工作。当我们使用网络服务时,它没有。

更新II:

问题是left join的条件:如果无法评估条件(例如,一个值不存在),则Web服务调用将以Communication Exception回答。有谁知道更多关于这个?

3 个答案:

答案 0 :(得分:0)

由于我无法发表评论,您是否抓住了CommumnicationException?这个例外的Message属性是什么意思?

答案 1 :(得分:0)

如果您的WCF返回Datatable,则在创建Datatable对象时需要使用此costructor:

var dt = new DataTable("Anything");

您可以使用此功能:

   public DataTable ExecuteRequest(string requete)
    {

        var dt = new DataTable("Anything"); // this will resolve your problem
        if (requete!= string.Empty)
        {
            using (con = new SqlConnection(ConString))
            {
               var cmd = new SqlCommand(requete, con);
               var sda = new SqlDataAdapter(cmd);


                sda.Fill(dt);


            } 
        }
        return dt;
    }

我希望它能解决你的问题

答案 2 :(得分:0)

成功解决方案:将DataTable替换为DataSet

我不知道为什么,但从那以后异常就消失了。