猿问

C# 如何在运行时更新 Visual Studio 2107 xxx.exe.config 绑定?

当我编译我的 C# web 服务代码时,会创建一个 xxx.exe.config 文件。但是,我想在运行时修改其中一个绑定。Visual Studio 生成的 xxx.exe.config 文件的示例是:


<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <binding name="BasicHttpBinding_ContentService"

                         messageEncoding="Mtom" />

            </basicHttpBinding>

        </bindings>

    </system.serviceModel>

</configuration>

我想在运行时向名为“BasicHttpBinding_ContentService”的绑定添加一些参数,相当于手动编码:


<binding name="BasicHttpBinding_ContentService"                             

              messageEncoding="Mtom"

              maxBufferSize="5000000"

              maxBufferPoolSize="524288"

              maxReceivedMessageSize="2147483648"

              transferMode="Streamed" />

我认为定义参数设置的 C# 代码如下所示:


using System.ServiceModel;


BasicHttpBinding myBinding = new BasicHttpBinding("BasicHttpBinding_ContentService");

myBinding.MaxBufferSize = 5000000;

myBinding.MaxBufferPoolSize = 524288;

myBinding.MaxReceivedMessageSize = 2147483648;

myBinding.TransferMode = TransferMode.Streamed;

但是我找不到将“myBinding”应用于我的 Visual Studio .config 文件的 C# 示例(有效)。我确实找到了在我的环境中未定义的参考类,如 ServiceHost 和 ServiceClient。


注意 - 我使用的是 .NET 4.6。


桃花长相依
浏览 132回答 2
2回答

墨色风雨

尝试这样的事情,让我知道......NTEGRAWSSOAPClient _wsProtheus = null; // NTEGRAWSSOAPClient is a SOAP client class generated by vs2017 on register webservicevar endpointConfiguration = NTEGRAWSSOAPClient.EndpointConfiguration.INTEGRAWSSOAP; //INTEGRAWSSOAP is a ConfigurationName of service_wsProtheus = new NTEGRAWSSOAPClient(endpointConfiguration,new EndpointAddress("http://new address?wsdl", new SpnEndpointIdentity("your identi")));BasicHttpBinding bind = (BasicHttpBinding)_wsProtheus.Endpoint.Binding;bind.CloseTimeout = TimeSpan.Parse("02:00:00");bind.OpenTimeout = TimeSpan.Parse("02:00:00");bind.ReceiveTimeout = TimeSpan.Parse("02:00:00");bind.SendTimeout = TimeSpan.Parse("02:00:00");bind.MaxBufferPoolSize = Int32.MaxValue;bind.MaxBufferSize = Int32.MaxValue;bind.MaxReceivedMessageSize = Int32.MaxValue;bind.ReaderQuotas.MaxDepth = 32;bind.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;bind.ReaderQuotas.MaxArrayLength = Int32.MaxValue;bind.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;bind.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;bind.Security.Mode = BasicHttpSecurityMode.None;
随时随地看视频慕课网APP
我要回答