WCF:我如何以编程方式创建这些App.config值?

时间:2009-05-07 16:10:50

标签: .net wcf wcf-binding

如果我创建服务时没有指定任何绑定或端点(我通过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>

2 个答案:

答案 0 :(得分:16)

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


 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将是http://www.myuri.com/Services/Services.svc/basic

等服务网址

最后创建服务客户端


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

答案 1 :(得分:1)

嗯,配置中的客户端端点指定了这个URL:

 <endpoint address="http://www.myuri.com/Services/Services.svc/basic"

但是在您的代码示例中,您创建了:

 EndpointAddress endpointAddress = new EndpointAddress( "my.uri.com/service.svc" );

地址必须匹配 - 如果配置中的地址有效,则需要将代码更改为:

 EndpointAddress endpointAddress = new EndpointAddress( "http://www.myuri.com/Services/Services.svc/basic" );

请注意 - 您的代码示例中存在各种小错字(my.uri.com与www.myuri.com,/ service.svc而非/Services/Services.svc)。

是否可以使用更正的端点地址?

马克