[轉]使用代碼去描述WCF設定檔

來源:互聯網
上載者:User

標籤:sed   應該   ota   setimeout   ble   上傳下載   host   ima   mod   

轉自:使用代碼去描述WCF設定檔

在應用程式部署的時候,WCF用戶端因為伺服器位址的變化,需要修改程式設定檔的地址URL,手動修改很不方便,還會造成錯誤,所以盡量把描述WCF設定檔的配置使用代碼方式進行描述,通過在軟體中輸入伺服器IP地址的方式,動態修改訪問的URL,這樣比較方便,也不會出錯,導致程式運行異常。

下面我將一個WCF部署檔案採用代碼方式描述: 

WCF用戶端的設定檔:

<?xml version="1.0" encoding="utf-8"?>  <configuration>      <system.serviceModel>          <bindings>              <wsHttpBinding>                  <binding name="WSHttpBinding_IMainService" closeTimeout="00:01:00"                      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"                      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"                      maxBufferPoolSize="524288" maxReceivedMessageSize="65536"                      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"                      allowCookies="false">                      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"                          maxBytesPerRead="4096" maxNameTableCharCount="16384" />                      <reliableSession ordered="true" inactivityTimeout="00:10:00"                          enabled="false" />                      <security mode="Message">                          <transport clientCredentialType="Windows" proxyCredentialType="None"                              realm="" />                          <message clientCredentialType="Windows" negotiateServiceCredential="true"                              algorithmSuite="Default" establishSecurityContext="true" />                      </security>                  </binding>              </wsHttpBinding>          </bindings>          <client>              <endpoint address="http://localhost:8888/DataCenter.Factory/MainService/"                  binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMainService"                  contract="IMainService" name="WSHttpBinding_IMainService">                  <identity>                      <servicePrincipalName value="host/WIN-QFIKKT28EHC" />                  </identity>              </endpoint>          </client>      </system.serviceModel>  </configuration>

編程中使用代碼描述上述檔案:在這裡用戶端引用的WCF服務命名空間定義為DataCenterFatoryC,用戶端訪問對象為  DataCenterFatoryC.MainServiceClient MainServiceClient1;

   using System.ServiceModel;       WSHttpBinding Bindins;       DataCenterFatoryC.MainServiceClient MainServiceClient1;         private void MainForm_Load(object sender, EventArgs e)     {          Bindins = new WSHttpBinding();//設定綁定          Bindins.CloseTimeout = TimeSpan.Parse("00:01:00");          Bindins.OpenTimeout = TimeSpan.Parse("00:01:00");          Bindins.ReceiveTimeout = TimeSpan.Parse("00:10:00");          Bindins.SendTimeout = TimeSpan.Parse("00:01:00");          Bindins.BypassProxyOnLocal = false;          Bindins.TransactionFlow = false;          Bindins.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;          Bindins.MaxBufferPoolSize = 524288;          Bindins.MaxReceivedMessageSize = 65535;          Bindins.MessageEncoding = WSMessageEncoding.Text;          Bindins.TextEncoding = Encoding.UTF8;          Bindins.UseDefaultWebProxy = true;          Bindins.ReaderQuotas.MaxDepth = 32;          Bindins.ReaderQuotas.MaxStringContentLength = 8192;          Bindins.ReaderQuotas.MaxArrayLength = 16383;          Bindins.ReaderQuotas.MaxBytesPerRead = 4096;          Bindins.ReaderQuotas.MaxNameTableCharCount = 16384;          Bindins.ReliableSession.Ordered = true;          Bindins.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");          Bindins.ReliableSession.Enabled = false;          Bindins.Security.Mode = SecurityMode.Message;          Bindins.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;          Bindins.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;          Bindins.Security.Message.ClientCredentialType = MessageCredentialType.Windows;          Bindins.Security.Message.NegotiateServiceCredential = true;          Bindins.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;          Bindins.Security.Message.EstablishSecurityContext = true;             string address = "http://localhost:8888/DataCenter.Factory/MainService/";//服務端點的URL          MainServiceClient1 = new DataCenterFatoryC.MainServiceClient(Bindins, new EndpointAddress(address));          MainServiceClient1.Open();      }

通過對Bindins的參數設定,能有效描述訪問過程的一些問題,比如上傳下載大檔案,大資料表都依賴重要參數的設定,否則運行中會有異常錯誤發生,這是我們應該注意的。

[轉]使用代碼去描述WCF設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.