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

时间:2011-11-14 09:48:03

标签: c# wcf nettcpbinding

  

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

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

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

已经尝试将maxReceivedMessageSizemaxBufferSizemaxStringContentLength增加到5 MB的5242880(我觉得绰绰有余),但没有运气。

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

请指导。

Update:

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

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,但后来我意识到我忘了给数据表命名,只是检查一下你是否也错过了它......

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

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

答案 1 :(得分:0)

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

  • 在服务器上:

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

答案 2 :(得分:0)

以下是如何增加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