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

发布于 2025-01-08 10:57:14 字数 5210 浏览 1 评论 0原文

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

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

谢谢

I've got a C# WCF service that passes a collection of instantiated .Net objects to the caller. Works fine, however if I try to pass too many objects in the collection it fails with the following exception:

{"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"}

It's fully reproduceable and is definitely related to the number objects in the collection and not the contents of the object.

Based on another post I made about it, I tried making changes to both my client side app.config file and the service server side web.config file. However the changes did not fix the problem, which makes me think it's possible I did not set up my config files correctly.

Below is my client-side app.config file (non-related stuff removed):

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

And here is my server-side web.config file (non-related stuff removed):

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

Do these look correct? If so, is there anything I'm missing in either one that would make this work?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2025-01-15 10:57:14

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

Got it...Finally. The problem was that on the server-side web.config, the behavior maxItemsInObjectGraph had to be a servicebehavior, not an endpoint behavior.

一刻暧昧 2025-01-15 10:57:14

查看可用的服务日志记录,它使查找实际问题变得更加容易

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>

Check out the service logging available, it makes finding the actual problem much easier

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

To start, Add this to your web.config and then find the file using the viewer (SvcTraceViewer.exe)

Which for me is located here (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>
相思故 2025-01-15 10:57:14

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

Have you tried removing the mex endpoint?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文