gpt4 book ai didi

.net - WCF:如何以编程方式重新创建这些 App.config 值?

转载 作者:行者123 更新时间:2023-12-04 03:14:12 25 4
gpt4 key购买 nike

如果我创建服务而不指定任何绑定(bind)或端点(当我通过 Visual Studio 注册 WCF 时,它从 App.config 中生成的值中读取它),我有一个工作正常的 WCF 服务。

我有一个返回服务引用的简单方法:

return new SmsServiceReference.SmsEngineServiceClient();

这工作正常(因为值是从配置中读取的)。但是,我想在数据库中包含其中一些值(例如 URI),并且想做这样的事情:
        Binding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );

return new SmsServiceReference.SmsEngineServiceClient(binding,endpointAddress);

这行不通。当我尝试使用服务引用时,它会引发异常。

我怀疑这是因为我的 App.config 有更多信息,而这两行没有提供(显然)。问题是,如何以编程方式复制以下 App.Config 值?

这是我的 App.Config 的片段:(URI 已被更改以保护无辜者)。
  <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISmsEngineService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.myuri.com/Services/Services.svc/basic"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISmsEngineService"
contract="SmsServiceReference.ISmsEngineService" name="BasicHttpBinding_ISmsEngineService" />
</client>

最佳答案

App 配置中的大多数值也是绑定(bind)中的属性,可以通过编程方式重新创建。就个人而言,我使用如下方法来创建绑定(bind)

public static BasicHttpBinding CreateBasicHttpBinding()
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.AllowCookies = false;
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
binding.OpenTimeout = new TimeSpan(0, 1, 0);
binding.SendTimeout = new TimeSpan(0, 1, 0);
// add more based on config file ...
//buffer size
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 534288;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;

//quotas
binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 8192;
// add more based on config file ...

return binding;
}

我使用这样的东西来创建我的端点地址

public static EndpointAddress CreateEndPoint()
{
return new EndpointAddress(Configuration.GetServiceUri());
}

serviceUri 将是服务 URL,例如 http://www.myuri.com/Services/Services.svc/basic

最后创建服务客户端

Binding httpBinding = CreateBasicHttpBinding();
EndpointAddress address = CreateEndPoint();
var serviceClient = new MyServiceClient(httpBinding, address);

关于.net - WCF:如何以编程方式重新创建这些 App.config 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/835613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com