2013-04-06 188 views
3

我在我的控制檯應用程序託管WCF服務如下:MaxReceivedMessageSize在WCF的控制檯應用程序託管服務

static void Main(string[] args) 
    { 
     Uri baseAddress = new Uri("http://localhost:8080/Test"); 
     // Create the ServiceHost. 
     using (ServiceHost host = new ServiceHost(typeof(TestService), baseAddress)) 
     { 
      // Enable metadata publishing. 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
      host.Description.Behaviors.Add(smb); 

      host.Open(); 

      Console.WriteLine("The Test service is ready at {0}", baseAddress); 
      Console.WriteLine("Press <Enter> to stop the service."); 



      Console.ReadLine(); 

      // Close the ServiceHost. 
      host.Close(); 
     } 
    } 

我有一個客戶在Windows應用商店(WinRT的)應用程序。我越來越

「(413)請求實體過大」

試圖通過一個大的字節數組時。我如何通過代碼在我的服務中設置MaxReceivedMessageSize

回答

6

你需要創建一個綁定,然後指定MaxReceivedMessageSize:

Uri baseAddress = new Uri("http://localhost:8080/Test"); 
var serviceHost = new ServiceHost(typeof(TestService)); 
var basicHttpBinding = new BasicHttpBinding(); 
basicHttpBinding.MaxReceivedMessageSize = int.MaxValue; 
serviceHost.AddServiceEndpoint(typeof(IService), basicHttpBinding, baseAddress); 
-1

如果你的字節數組太大,那麼你可以隨時將其分割到更小的塊,並把這些在一個循環。您甚至可能希望在另一個線程中執行此操作並更新用戶界面的進度。

相關問題