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

标签 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>

这些看起来正确吗?如果是这样,我在其中任何一个中是否缺少任何可以使这项工作成功的东西?

谢谢

最佳答案

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

关于c# - WCF 服务调用失败...我的配置文件有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9398434/

相关文章:

c# - "ASP.NET AJAX Server Control"和 "ASP.NET Server Control"项目类型有什么区别?

c# - 从 SQLite 数据库文件检索一百万条记录并将其显示在 WPF 数据网格中的最快方法是什么?

c# - 从 UIElement 生成 BitmapSource

c# - 当 WCF 服务和 jquery 函数在不同的项目中但在同一解决方案中时,使用 jquery 调用 WCF 服务函数

WCF 并发连接和 Azure 服务总线中继

c# - 在 C# 中是否有一种方便的方法将字符串的一部分从一个索引获取到另一个索引?

c# - 在 Xamarin Forms 中居中对齐工具栏项目

c# 通过outlook 2013发送html电子邮件

c# - 有没有办法让 XAML/C# StringFormat 显示较大金额的整美元,但较小金额的美分?

c# - 每次关闭表单应用程序时注销 C# WCF