首页 > 代码库 > [转]使用代码去描述WCF配置文件

[转]使用代码去描述WCF配置文件

<style></style>

转自:使用代码去描述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配置文件