WCF服务调用失败...我的配置文件有什么问题?

时间:2012-02-22 16:05:40

标签: c# wpf wcf web-services

我有一个C#WCF服务,它将一组实例化的.Net对象传递给调用者。工作正常,但如果我尝试在集合中传递太多对象,则会失败,但出现以下异常:

{"An error occurred while receiving the HTTP response to http://myserver/MyAppService/MyAppService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."}
{"The underlying connection was closed: An unexpected error occurred on a receive."}
{"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
{"An existing connection was forcibly closed by the remote host"}

它完全可重现,并且肯定与集合中的数字对象有关,而不是对象的内容。

根据我提到的另一篇文章,我尝试更改我的客户端app.config文件和服务服务器端web.config文件。但是这些更改并没有解决问题,这让我觉得我没有正确设置配置文件。

下面是我的客户端app.config文件(删除了非相关内容):

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_iMyAppService" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="01:00:00" sendTimeout="01:00:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
        <security mode="None">
          <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
          <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="Behaviors.EndpointBehavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <client>      
    <endpoint address="http://myserver/MyAppService/MyAppService.svc"
    behaviorConfiguration="Behaviors.EndpointBehavior"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_iMyAppService"
    contract="MyAppService.iMyAppService" name="BasicHttpBinding_iMyAppService" />
  </client>
</system.serviceModel>

这是我的服务器端web.config文件(删除了非相关内容):

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="Behaviors.EndpointBehavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_iMyAppService"
             closeTimeout="00:01:00"
      openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
        <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
          maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
        <security mode="None">
          <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
          <message clientCredentialType="UserName" algorithmSuite="Default" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  <services>
    <service name="MyApp_WCFService.MyAppService">
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost/MyAppService/"/>
        </baseAddresses>
      </host>
      <endpoint address="" behaviorConfiguration="Behaviors.EndpointBehavior" binding="basicHttpBinding" contract="MyApp_WCFService.iMyAppService" bindingConfiguration="BasicHttpBinding_iMyAppService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
  </modules>
</system.webServer>

这些看起来是否正确?如果是这样,那么任何一个我都会遗漏任何可以使这项工作的东西吗?

由于

3 个答案:

答案 0 :(得分:1)

得到它......最后。问题是在服务器端web.config上,行为maxItemsInObjectGraph必须是servicebehavior,而不是端点行为。

答案 1 :(得分:0)

查看可用的服务日志记录,它可以更轻松地找到实际问题

http://msdn.microsoft.com/en-us/library/ms732023.aspx

首先,将其添加到您的web.config,然后使用查看器(SvcTraceViewer.exe)找到该文件

我的位置(C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin)

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing" >
            <listeners>
                <add name="ServiceModelTraceListener"/>
            </listeners>
        </source>
        <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
            <listeners>
                <add name="ServiceModelTraceListener"/>
            </listeners>
        </source>
        <source name="System.Runtime.Serialization" switchValue="Verbose,ActivityTracing">
            <listeners>
                <add name="ServiceModelTraceListener"/>
            </listeners>
        </source>
    </sources>

    <sharedListeners>
        <add initializeData="App_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp"/>
    </sharedListeners>
</system.diagnostics>

答案 2 :(得分:0)

您是否尝试过删除mex端点?