Search code examples
c#wcfwindows-runtimewcf-binding

MaxReceivedMessageSize in WCF Hosted Service in console application


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?


Solution

  • 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);