Dot-Net

WCF:如何以程式方式重新創建這些 App.config 值?

  • November 16, 2019

如果我創建服務而不指定任何綁定或端點(當我通過 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 配置中的大多數值也是綁定中的屬性,可以通過程式方式重新創建。就個人而言,我使用如下方法來創建綁定


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

引用自:https://stackoverflow.com/questions/835613