WCF:如何编程创建这些App.config中值? [英] WCF: How can I programatically recreate these App.config values?
问题描述
我有一个WCF服务工作正常,如果我创建服务不指定任何约束或端点(它在App.config生成的值读它时,我通过Visual Studio中注册的WCF)。
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();
该工程确定(因为该值从配置读取)。不过,我想有一些值的数据库(URI为例),并希望做这样的事情:
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.
我怀疑这是因为我的App.config中有更多的信息,这两条线在那里不提供(明显)。现在的问题是,我怎么可以复制以下的app.config值编程?
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 programatically?
下面是我的app.config的片段:(URI中已经改变,以保护inocent)
Here's the fragment of my App.Config: (the URI has been altered to protect the inocent).
<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());
}
该serviceUri分别将服务URL,如<一个href="http://www.myuri.com/Services/Services.svc/basic">http://www.myuri.com/Services/Services.svc/basic
最后,创建服务客户端
Binding httpBinding = CreateBasicHttpBinding();
EndpointAddress address = CreateEndPoint();
var serviceClient = new MyServiceClient(httpBinding, address);
这篇关于WCF:如何编程创建这些App.config中值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!