将数据表发送到WCF服务时发生序列化错误

将数据表发送到WCF服务时发生序列化错误

问题描述:

消息Method1的序列化正文中出现错误:'生成XML文档时出错。将数据表发送到WCF服务时发生序列化错误

我得到当我试图通过一个数据表作为参数传递给一个WCF方法这个错误,我已经使用这种类型的在我的WCF服务方法(即DataTable作为参数),即工作正常。

但是在这种情况下,我认为原因可能是DataTable的大小,但不确定。

已经尝试增加maxReceivedMessageSize,maxBufferSizemaxStringContentLength到5242880这是5 MB(我认为绰绰有余),但没有运气。

找到许多相关的文章,但没有人指出这个特定的问题,因此张贴。

请指导。

Update:

DataTable还含有包含一些XML内容,这会是问题,因为这是XML格式进行序列化。

我有同样的问题,但后来我意识到,我忘了名字的数据表,只是检查,如果你也是失踪了......

DataTable dt = new DataTable(); 
... 
dt.TableName = "Table1"; 

现在通过表作为参数,希望它有助于

+0

这是我的同样的问题,非常感谢你,和其他人的努力:) – Bravo

+0

谢谢先生为这个答案。经过无数谷歌搜索,我终于找到了这个答案,它实际上解决了我的问题。 – SolutionYogi

尝试在App.config文件添加behavior配置(和你的服务和端点引用它):

  • 在服务器上:

    <behaviors> 
        <serviceBehaviors> 
         <behavior name="MyServiceBehavior"> 
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
         </behavior> 
        </serviceBehaviors> 
    </behaviors> 
    
  • 在客户端:

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

如果还是不行,请尝试为绑定设置的最高值:

<bindings> 
    <netTcpBinding> 
     <binding name="MyBindingConf" 
       maxReceivedMessageSize="2147483647" 
       maxBufferSize="2147483647" 
       maxBufferPoolSize="2147483647" 
       sendTimeout="00:30:00"> 
      <readerQuotas maxDepth="32" 
          maxStringContentLength="2147483647" 
          maxArrayLength="2147483647" 
          maxBytesPerRead="4096" 
          maxNameTableCharCount="16384" /> 
      <security mode="None" /> 
     </binding> 
    </netTcpBinding> 
</bindings> 
+0

我想他们两个,并没有为我工作。 – Bravo

+0

@Bravo:你是否引用了“行为”? (用' ...'和' – Otiel

+0

'也是一样的)Otiel,我已经为服务器配置做了这些,但没有找到任何绑定在客户端。所以就这样离开了...我做错了什么。我也想知道这是错误的真正原因,因为我的数据表不能大于1或2 MB。 – Bravo

这里有一个如何提高存储在SharePoint中的自定义WCF的Web服务的消息长度一个很好的例子:

using System; 
using System.Diagnostics; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using Microsoft.SharePoint.Client.Services; 

namespace MyNamespace 
{ 
    public class MyFactory : MultipleBaseAddressBasicHttpBindingServiceHostFactory 
    { 
     protected override ServiceHost CreateServiceHost(Type serviceType, 
      Uri[] baseAddresses) 
     {    
      ServiceHost host = new MultipleBaseAddressBasicHttpBindingServiceHost(
       serviceType, baseAddresses); 

      if (host != null) 
      { 
       host.Opening += new EventHandler(OnHost_Opening); 
      } 

      return host; 
     } 

     private void OnHost_Opening(object sender, EventArgs e) 
     { 
      Debug.Assert(sender != null); 
      ServiceHost host = sender as ServiceHost; 
      Debug.Assert(host != null); 

      // Configure the binding values 
      if (host.Description != null && host.Description.Endpoints != null) 
      { 
       foreach (var endPoint in host.Description.Endpoints) 
       {      
        if (endPoint != null && endPoint.Binding != null) 
        { 
         BasicHttpBinding basicBinding = endPoint.Binding 
          as BasicHttpBinding; 

         if (basicBinding != null) 
         { 
          ConfigureBasicHttpBinding(basicBinding); 
         } 
        } 
       } 
      } 
     } 

     private static void ConfigureBasicHttpBinding(BasicHttpBinding basicBinding) 
     { 
      Debug.Assert(basicBinding != null); 
      basicBinding.MaxReceivedMessageSize = Int32.MaxValue; 
      basicBinding.ReaderQuotas.MaxArrayLength = Int32.MaxValue; 
      basicBinding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue; 
      basicBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue; 
     } 
    } 
} 
As you can see I’m setting maximum length of the incoming messages. 

Now we need to modify our service svc file to use our custom Factory Configuration: 
<%@ ServiceHost Language="C#" Debug="true" 
    Service="MyNamespace.MyService, $SharePoint.Project.AssemblyFullName$" 
    CodeBehind="MyService.svc.cs" 
    Factory="MyNamespace.MyFactory, $SharePoint.Project.AssemblyFullName$"%> 

example