2011-11-14 43 views
3

消息Method1的序列化正文中出現錯誤:'生成XML文檔時出錯。將數據表發送到WCF服務時發生序列化錯誤

我得到當我試圖通過一個數據表作爲參數傳遞給一個WCF方法這個錯誤,我已經使用這種類型的在我的WCF服務方法(即DataTable作爲參數),即工作正常。

但是在這種情況下,我認爲原因可能是DataTable的大小,但不確定。

已經嘗試增加maxReceivedMessageSize,maxBufferSizemaxStringContentLength到5242880這是5 MB(我認爲綽綽有餘),但沒有運氣。

找到許多相關的文章,但沒有人指出這個特定的問題,因此張貼。

請指導。

Update:

DataTable還含有包含一些XML內容,這會是問題,因爲這是XML格式進行序列化。

回答

6

我有同樣的問題,但後來我意識到,我忘了名字的數據表,只是檢查,如果你也是失蹤了......

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

現在通過表作爲參數,希望它有助於

+0

這是我的同樣的問題,非常感謝你,和其他人的努力:) – Bravo

+0

謝謝先生爲這個答案。經過無數谷歌搜索,我終於找到了這個答案,它實際上解決了我的問題。 – SolutionYogi

0

嘗試在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

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

相關問題