MaxReceivedMessageSize in WCF Hosted Service in co

2019-07-13 11:27发布

I have a hosted WCF service in my console application as follow:

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();
        }  
    }

I have a client in a Windows Store (WinRT) application. I'm getting

"(413) Request Entity Too Large"

when trying to pass a large byte array. How can I set MaxReceivedMessageSize in my service by code?

2条回答
疯言疯语
2楼-- · 2019-07-13 12:03

If your byte array is too big, then you could always split it to smaller blocks and send those in a loop. You might even want do it in another thread and update progress to the user interface.

查看更多
Fickle 薄情
3楼-- · 2019-07-13 12:09

You need to create a Binding, and then specify 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);
查看更多
登录 后发表回答