WCF:如何以编程方式重新创建这些 App.config 值? [英] WCF: How can I programmatically recreate these App.config values?
问题描述
如果我在不指定任何绑定或端点的情况下创建服务(当我通过 Visual Studio 注册 WCF 时,它从 App.config 中生成的值中读取它),我有一个 WCF 服务可以正常工作.
我有一个返回服务引用的简单方法:
return new SmsServiceReference.SmsEngineServiceClient();
这可以正常工作(因为值是从配置中读取的).但是,我希望在数据库(例如 URI)中包含其中一些值,并希望执行以下操作:
绑定 binding = new BasicHttpBinding();EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );返回新的 SmsServiceReference.SmsEngineServiceClient(binding,endpointAddress);
这不起作用.当我尝试使用服务引用时,它会抛出异常.
我怀疑这是因为我的 App.config 有更多的信息,上面的两行没有提供(显然).问题是,如何以编程方式复制以下 App.Config 值?
这是我的 App.Config 的片段:(已更改 URI 以保护无辜者).
<绑定><基本HttpBinding><绑定名称="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"/><安全模式=无"><transport clientCredentialType="None" proxyCredentialType="None"领域=""/><message clientCredentialType="UserName" algorithmSuite="默认"/></安全></binding></basicHttpBinding></绑定><客户><端点地址="http://www.myuri.com/Services/Services.svc/basic"binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISmsEngineService"contract="SmsServiceReference.ISmsEngineService" name="BasicHttpBinding_ISmsEngineService"/></客户端>
App 配置中的大多数值也是绑定中的属性,可以通过编程方式重新创建.就个人而言,我使用如下方法创建绑定
<前><代码>公共静态 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);//根据配置文件添加更多...//缓冲区大小binding.MaxBufferSize = 65536;binding.MaxBufferPoolSize = 534288;binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;//配额binding.ReaderQuotas.MaxDepth = 32;binding.ReaderQuotas.MaxStringContentLength = 8192;//根据配置文件添加更多...返回绑定;}我使用这样的东西来创建我的端点地址
<前><代码>公共静态端点地址 CreateEndPoint(){return new EndpointAddress(Configuration.GetServiceUri());}serviceUri 将是服务 URL,例如 http://www.myuri.com/Services/Services.svc/basic
最后创建服务客户端
<前><代码>绑定 httpBinding = CreateBasicHttpBinding();EndpointAddress 地址 = CreateEndPoint();var serviceClient = new MyServiceClient(httpBinding, address);I have a WCF service that works ok if I create the service without specifying any binding or endpoint (it reads it from the generated values in the App.config when I registered the WCF via Visual Studio).
I have a simple method that returns the service reference:
return new SmsServiceReference.SmsEngineServiceClient();
This works ok (because the values are read from the config). However, I'd like to have some of these values in a Database (the URI for example) and would like to do something like this:
Binding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );
return new SmsServiceReference.SmsEngineServiceClient(binding,endpointAddress);
This doesn't work. It throws an exception when I try to use the service reference.
I suspect that this is because my App.config has more information that the two lines up there are not providing (obviously). The question is, how can I replicate the following App.Config values programmatically?
Here's the fragment of my App.Config: (the URI has been altered to protect the innocent).
<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>
Most of the values in the App config are also properties in the binding and can be recreated programatically. Personally, I use a method such as the one below to create the binding
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;
}
And I use something like this for creating my Endpoint address
public static EndpointAddress CreateEndPoint()
{
return new EndpointAddress(Configuration.GetServiceUri());
}
The serviceUri will be the service URL such as http://www.myuri.com/Services/Services.svc/basic
Finally to create the service client
Binding httpBinding = CreateBasicHttpBinding();
EndpointAddress address = CreateEndPoint();
var serviceClient = new MyServiceClient(httpBinding, address);
这篇关于WCF:如何以编程方式重新创建这些 App.config 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!