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

本文关键字:序列化 服务 错误 WCF 数据表 | 更新日期: 2023-09-27 18:14:46

序列化消息Method1的正文时出错:"生成XML文档时出错。">

当我试图将数据表作为参数传递给WCF方法时,我遇到了这个错误,我已经在WCF服务中使用了这种类型的方法(即DataTable作为参数(,这很好。

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

已经尝试将maxReceivedMessageSizemaxBufferSizemaxStringContentLength增加到5242880,也就是5MB(我认为这已经足够了(,但运气不佳。

发现了许多相关的文章,但没有人指出这个特定的问题,因此发布了。

请引导。

Update:

我的DataTable还包含一些XML内容,这可能是问题所在,因为这是以XML格式序列化的。

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

我也遇到了同样的问题,但后来我意识到我忘记了命名数据表,只是检查一下你是否也遗漏了它…

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

现在将该表作为参数传递,希望它能帮助

尝试在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>

这里有一个关于如何增加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$"%>

示例