为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
桃花长相依 2021-06-29 13:48:12
当我编译我的 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。
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

尝试这样的事情,让我知道......


NTEGRAWSSOAPClient _wsProtheus = null; // NTEGRAWSSOAPClient is a SOAP client class generated by vs2017 on register webservice


var 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;


查看完整回答
反对 回复 2021-07-10
  • 2 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信