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

问题描述 投票:10回答:2

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

我有一个简单的方法可以返回服务参考:

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>

我有一个WCF服务,如果我在不指定任何绑定或端点的情况下创建该服务,就可以正常工作(当我通过Visual ...注册WCF时,它会从App.config中的生成值中读取它。

App config中的大多数值也是绑定中的属性,可以通过编程方式重新创建。我个人使用以下方法来创建绑定

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

嗯,配置中的客户端端点指定了此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)。

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

马克

.net wcf wcf-binding
2个回答
16
投票
App config中的大多数值也是绑定中的属性,可以通过编程方式重新创建。我个人使用以下方法来创建绑定

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


1
投票
嗯,配置中的客户端端点指定了此URL:

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

但是在您的代码示例中,您创建:
© www.soinside.com 2019 - 2024. All rights reserved.