简体   繁体   English

在自托管的c#WCF控制台应用程序中使用web.config(设置MaxStringContentLength服务器端)

[英]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. 我有一个简单的自托管 WCF控制台Windows应用程序,可以从客户端正常连接。 I have a problem though sending large XML strings through to the server. 我通过发送大型XML字符串到服务器时遇到了问题。 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..." “ System.Xml.XmlException:读取XML数据时,已超出最大字符串内容长度配额(8192)。可以通过更改XmlDictionaryReaderQuotas上的MaxStringContentLength属性来增加此配额。”

I can set the MaxStringContentLength in the client by changing its app.config file (generated by svcutil.exe). 我可以通过更改客户端的app.config文件(由svcutil.exe生成)来设置MaxStringContentLength

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? 我已经阅读了有关web.config文件的信息,但不确定WCF控制台应用程序是否可以包含一个文件,如果可以,我该如何阅读和使用它? 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中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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