控制台应用程序中的WCF托管服务中的MaxReceivedMessageSize

时间:2013-04-06 12:02:12

标签: c# wcf windows-runtime wcf-binding

我的控制台应用程序中有托管的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

2 个答案:

答案 0 :(得分:6)

您需要创建一个Binding,然后指定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 :(得分:-1)

如果您的字节数组太大,那么您可以随时将其拆分为较小的块并在循环中发送它们。您甚至可能希望在另一个线程中执行此操作并将进度更新到用户界面。