WCF - 获取大量数据时出错

时间:2014-11-04 09:50:03

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

我不打算谈论错误

  

已超出传入邮件的最大邮件大小限额(65536)。   要增加配额,请在相应的绑定元素上使用MaxReceivedMessageSize属性。

我已经连接到 Microsoft Dynamics CRM 的wcf服务,我启动项目为 .NET 4.5 但是它所在的服务器只能放置主机 .NET 3.5

当我更改WCF项目框架时,某些操作合同在其返回的数据为中等大小时(数组中有1000个项目)停止工作。

我检查了XML并且它有〜1mb,所以我不认为它是关于配额

我收到的错误:

  

客户找到了''的响应内容类型,但预期' text / xml'。该   请求失败且回复为空。

服务器端:

public account[] GetX(DateTime? Range)
{
    Service svc = Helper.CreateService();
    account[] arr = null;

    if (Range == null)
        arr = svc.GetAllX();
    else
        arr = svc.GetAllXByDate(Range.Value);

    return arr;
}

Web-Config system.serviceModel:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="myBasicHttpBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
          maxArrayLength="16348" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      </binding>
    </basicHttpBinding>
  </bindings>

  <services>
    <service name="OROC.Proxy.ProxyService" behaviorConfiguration="ServiceBehaviors">
      <endpoint name="Endpoint"
                binding="basicHttpBinding"
                bindingConfiguration="myBasicHttpBinding"
                contract="OROC.Proxy.IProxyService" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehaviors">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <!--<protocolMapping>
    <add binding="basicHttpsBinding" scheme="http" />
  </protocolMapping>-->
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

哦,错误发生在它返回项目时,而不是在 return arr; 中,就在方法结束后。 小提琴手捕获所有XML,所以我猜问题是客户端

编辑1 04/10/2014 10:09

客户端:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

  <bindings>
    <basicHttpBinding>
      <binding name="myBasicBinding" maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647">
        <readerQuotas maxDepth="128" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      </binding>
    </basicHttpBinding>
  </bindings>

  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>

    <endpointBehaviors>
      <behavior name="ServiceBehaviour">
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <client>
    <endpoint name="basicEndpoint"
        address="http://localhost:58364/ProxyService.svc"
        binding="basicHttpBinding"
        bindingConfiguration="myBasicBinding"
        contract="*"
        behaviorConfiguration="ServiceBehaviour">
    </endpoint>
  </client>

</system.serviceModel>

EDIT 2 04/10/2014 10:34

我已经使用IE测试了XML并且它没有正确解析,但是当我用visual studio打开xml时它警告我关于unicode字符并且它取代了它们,然后我保存了文件并且用IE再次打开,它被正确解析。这些信息非常有用。

有想法的人吗? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您是否在客户端上设置了maxReceivedMessageSize =“2147483647”?

答案 1 :(得分:0)

我解决了!

我发现我需要增加 maxItemsInObjectGraph ,但无论我做什么,我都无法做到。

我是怎么找到的?我是如何解决这个问题的?

第一个问题:我已将引用添加为Web引用而不是ServiceReference,我无法将ServiceBehavior和端点工作。

第二个问题:我需要启用TraceFile,所以我看到的不仅仅是一个简单的

  

服务器未提供有意义的回复;这可能是由合同不匹配,过早的会话关闭或内部服务器错误引起的

第三个问题: web.config 中的serviceModel在服务器端和客户端都必须相同。

解决方案:首先,我删除了网络参考并添加了服务参考,当我这样做时,我需要更改代码隐藏(客户端)中的某些方法。

然后我匹配了web.config&#39。

客户端WebConfig

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

  <bindings>
    <basicHttpBinding>
      <binding name="myBasicBinding" receiveTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" maxBufferPoolSize="524288" maxBufferSize="2147483647"
        maxReceivedMessageSize="2147483647">
        <security mode="None" />
        <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
          maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      </binding>
      <binding name="Endpoint" />
    </basicHttpBinding>
  </bindings>

  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </serviceBehaviors>

    <endpointBehaviors>
      <behavior name="ServiceBehaviour">
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <client>
    <endpoint address="http://localhost:58364/ProxyService.svc" behaviorConfiguration="ServiceBehaviour"
      binding="basicHttpBinding" bindingConfiguration="myBasicBinding"
      contract="ProxyService.IProxyService" name="basicEndpoint" />
  </client>
</system.serviceModel>

服务器端WebConfig

<system.serviceModel>
  <extensions>
    <bindingElementExtensions>
      <add name="customTextMessageEncoding" type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingBindingSection, CustomTextMessageEncoder" />
    </bindingElementExtensions>
  </extensions>
  <bindings>
    <basicHttpBinding>
      <binding name="myBasicHttpBinding" receiveTimeout="00:10:00" closeTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
        <security mode="None" />
        <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
          maxArrayLength="16348" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      </binding>
    </basicHttpBinding>
  </bindings>

  <services>
    <service name="OROC.Proxy.ProxyService" behaviorConfiguration="ServiceBehaviors">
      <endpoint name="Endpoint"
                binding="basicHttpBinding"
                bindingConfiguration="myBasicHttpBinding"
                contract="OROC.Proxy.IProxyService" />
      <!--<endpoint kind="udpDiscoveryEndpoint" binding="ProxyService" />-->
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceBehaviors">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

为了发现我遇到了什么问题,我只是在服务器端webconfig上启用了TraceFile,并使用默认程序(Microsoft Service Trace Viewer)打开了该文件。

<强>诊断

<system.diagnostics>
  <trace autoflush="true"/>
  <sources>
    <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
      <listeners>
        <add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="TraceFile.svclog"/>
      </listeners>
    </source>
  </sources>
</system.diagnostics>