シンプルなセルフホストのWCFコンソールWindowsアプリがあり、クライアントから正常に接続できます。大きなXML文字列をサーバーに送信しても問題が発生します。次のエラーが発生します。
「System.Xml.XmlException:XMLデータの読み取り中に最大文字列コンテンツ長のクォータ(8192)を超えました。このクォータは、XmlDictionaryReaderQuotasのMaxStringContentLengthプロパティを変更することで増やすことができます...」
app.configファイル(svcutil.exeによって生成される)を変更することにより、クライアントでMaxStringContentLengthを設定できます。
しかし、サーバー側では、これを変更できる場所がありません。web.configファイルについて読んだことがありますが、WCFコンソールアプリにそのファイルを含めることができるかどうか、またそうであれば、どのように読み込んで使用できるかわかりません。私のセルフホスティングコードは以下のとおりです。
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();
}
}