开发者

Using web.config in a Self-Hosted c# WCF console app (setting MaxStringContentLength server side)

开发者 https://www.devze.com 2023-01-18 12:41 出处:网络
I have a simple self hosted WCF Console windows app, and I can connect fine from my client. I have a problem though sending large XML strings through to the server. I get the following error:

I have a simple self hosted WCF Console windows app, and I can connect fine from my client. I have a problem though sending large XML strings through to the server. I get the following error:

"System.Xml.XmlException: The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas..."

I can set the MaxStringContentLength in the client by 开发者_StackOverflowchanging its app.config file (generated by svcutil.exe).

But on the server side I have nowhere I can change this. I have read about a web.config file and am not sure if a WCF console app can have one and if so how I can read it in and use it? My self hosting code is below:

static void RunWCFService()
{
    // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
    Uri baseAddress = new Uri("http://localhost:8000/MyService/WcfService");

    // Step 2 of the hosting procedure: Create ServiceHost
    ServiceHost selfHost = new ServiceHost(typeof(MyServiceWcf), baseAddress);
    try
    {
        // Step 3 of the hosting procedure: Add a service endpoint.
        selfHost.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "MyService");

        // Step 4 of the hosting procedure: Enable metadata exchange.
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        selfHost.Description.Behaviors.Add(smb);                

        // Step 5 of the hosting procedure: Start (and then stop) the service.
        selfHost.Open();
        Console.WriteLine("Press <ENTER> to terminate service.");       
        Console.ReadLine();
        // Close the ServiceHostBase to shutdown the service.
        selfHost.Close();
    }
    catch (CommunicationException ce)
    {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        selfHost.Abort();
    }
 }


The WCF config data goes in the app.config of the exe that is doing the hosting.

0

精彩评论

暂无评论...
验证码 换一张
取 消