简体   繁体   中英

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

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

WCF配置数据进入正在执行托管的exe的app.config中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM