コードで作成された自己ホスト型Webサービスがあります。
protected void StartService(Type serviceType, Type implementedContract, string serviceDescription)
{
    Uri addressTcp = new Uri(_baseAddressTcp + serviceDescription);
    ServiceHost selfHost = new ServiceHost(serviceType, addressTcp);
    Globals.Tracer.GeneralTrace.TraceEvent(TraceEventType.Information, 0, "Starting service " + addressTcp.ToString());
    try
    {
        selfHost.AddServiceEndpoint(implementedContract, new NetTcpBinding(SecurityMode.None), "");
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        selfHost.Description.Behaviors.Add(smb);
        System.ServiceModel.Channels.Binding binding = MetadataExchangeBindings.CreateMexTcpBinding();
        selfHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, "mex");
        selfHost.Open();
        ServiceInfo si = new ServiceInfo(serviceType, implementedContract, selfHost, serviceDescription);
        try
        {
            lock (_hostedServices)
            {
                _hostedServices.Add(serviceType, si);
            }
        }
        catch (ArgumentException)
        {
             //...
        }
    }
    catch (CommunicationException ce)
    {
        //...
        selfHost.Abort();
    }
}
これは問題なく動作しますが、大量のデータを送信しようとすると、次の例外が発生します。
エラー:フォーマッタがメッセージの逆シリアル化を試みているときに例外をスローしました:パラメータ@@@を逆シリアル化しようとしているときにエラーが発生しました。InnerExceptionメッセージは次のとおりです。'タイプ@@@のオブジェクトの逆シリアル化中にエラーが発生しました。XMLデータの読み取り中に、文字列コンテンツの最大長クォータ(8192)を超えました。このクォータは、XMLリーダーの作成時に使用されるXmlDictionaryReaderQuotasオブジェクトのMaxStringContentLengthプロパティを変更することで増やすことができます。詳細については、InnerExceptionを参照してください。at:at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation、ProxyRpc&rpc)
解決策は、MaxStringContentLengthプロパティをバインディングに追加しているようです。Web.config(リンク)でそれを行う方法を理解しています:
...バインディングname="wsHttpBindingSettings" maxReceivedMessageSize = "2147483647">
コードでバインディングのmaxReceivedMessageSizeを変更する方法を探しています。私が使用しているバインディングのタイプでもそれは可能ですか?
ありがとう。
編集:もう少し学んだ後(そして私が受け取った応答のガイダンスで)私は問題を理解しました:私はそれを宣伝するためだけに使用されるサービスのMEX部分を変更しようとしていました。リンクを参照してください。NetTcpBindingのバインディングを変更する必要がありました(tryステートメントの最初の行)。これで、私の(動作中の)コードは次のようになります。
...
    try
    {
        //add the service itself
        NetTcpBinding servciceBinding = new NetTcpBinding(SecurityMode.None);
        servciceBinding.ReaderQuotas.MaxStringContentLength = 256 * 1024;
        servciceBinding.ReaderQuotas.MaxArrayLength = 256 * 1024;
        servciceBinding.ReaderQuotas.MaxBytesPerRead = 256 * 1024;
        selfHost.AddServiceEndpoint(implementedContract, servciceBinding, "");
...