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

本文关键字:我的 配置文件 有问题 服务 调用 失败 什么 WCF | 更新日期: 2023-09-27 18:25:31

我有一个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>

这些看起来正确吗?如果是这样的话,我在这两个方面都缺少什么吗?

感谢

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

终于明白了。问题是在服务器端web.config上,行为maxItemsInObjectGraph必须是服务行为,而不是端点行为。

查看可用的服务日志,它使更容易发现实际问题

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>

您是否尝试删除mex端点?