前言
本還想寫一集WCF入門教程的,心情實在不好,明天又還有面試,改天再寫吧。
說一下今天遇到的入職坑。面試能坑,上班能坑,完全沒想到入職也能坑。切身經歷。
今年10月份想換工作,更新了一下簡歷,接到北京漢克時代面試邀請,去了。后來通過了。然后談了薪資,各方面還算穩妥。同時短信方式向對方確認了本人入職要1個月時間,是否可以。對方回答可以。於是向原公司提出離職申請。在上周5辦完所有手續。期間,11月6號接到電話說會發表格給我,讓我填寫個人信息,后在11月13號回復了郵件,確認了各種信息。
今早去公司入職,公交還不順,等了好久才到公司,約好時間是9點,到公司已是9點15。和hr通了電話,告知已在門口,后等了半個小時不見有人出來接待我進去。繼續打電話告知說稍等有人出來。一段時間后,接到hr電話,該職位已被另一家公司給了別人。我就只能呵呵了。因為已經離職,原公司的年終獎也沒有。個人損失不祥。
后hr又打電話,說推薦其他公司,暫時沒有拒絕,不過,真心不舒服。。。我是不是太好說話了。
其他不想多說什么,反正這個坑,以后各位同學們長點心眼。記住這個以及這類公司。
第22集 在代碼中動態配置endpoint Configure endpoint dynamically in code
通過前面的學習,有個感觸,WCF的配置還是很重要的。不過,基本上能在配置文件中的實現的東西,從純粹實現功能的角度,代碼中也能配置。比如是否使用可靠的鏈接(reliableSession),是否包含包含異常信息(includeExceptionDetailInFaults)。但是實際項目中推薦使用配置文件的方式,因為這樣不需要對代碼重新編譯,直接改,直接用。
但是這集要講的是如何代碼中動態配置endpoint,實際應用場景暫時不明,不過可以提供一個WCF中實現某些功能的思路。外加看完之后覺得和以前的IErrorHandler接口實現中有異曲同工之妙。還有一點要說明的是WCF中的binding擴展性還不錯,學習完這集應該能有一點收獲。
開始正題。
首先是endpoint的三要素,Address, Binding, Contract。因為我們要實現動態配置,所以,可以在config里面移除這些配置。
這是原先的endpoint配置。
<endpoint address="" binding="netTcpBinding" contract="HelloService.IHelloService"></endpoint>
以及為了可以讓客戶端自動生成代理類,我們配置如下serviceBehavior,httpGetEnable為true。
<serviceBehaviors> <behavior name="myBehaviorsConfiguration"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors>
現在,我們可以把這些刪除。(因為includeExceptionDetailInFault可以在特性里面配置,所以也可以刪除。)
然后,代碼看起來就是這個樣子了:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <!--<serviceBehaviors> <behavior name="myBehaviorsConfiguration"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors>--> </behaviors> <bindings> <basicHttpBinding > </basicHttpBinding> <netTcpBinding> <binding></binding> </netTcpBinding> </bindings> <services> <service name="HelloService.HelloService" behaviorConfiguration =""> <!--<endpoint address="" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>--> <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />--> <host> <baseAddresses> <add baseAddress="http://localhost:8733/" /> <!--<add baseAddress ="net.tcp://localhost:8734"/>--> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
注釋了兩個endpoint和一個serviceBehaviors,同時移除behaviorConfiguration對上面設置的引用。baseAddress其實也可以移除,代碼中也可以配置,不過暫時先放着吧。
然后,如果我們再在客戶端重新添加該服務引用,會得到如下錯誤。
由圖可知該service已無法使用。
第2步:在open host之前,我們需要手動添加個endpoint給這個host。
原先代碼如下:
static void Main(string[] args) { using(var host = new ServiceHost(typeof(HelloService.HelloService))) { host.Open(); Console.WriteLine("Service Started @ " + DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss")); Console.ReadLine(); } }
改動后:
static void Main(string[] args) { using(var host = new ServiceHost(typeof(HelloService.HelloService))) { var serviceBehavior = new ServiceMetadataBehavior() { HttpGetEnabled=true }; host.Description.Behaviors.Add(serviceBehavior); host.AddServiceEndpoint(typeof(HelloService.IHelloService), new BasicHttpBinding(), ""); host.Open(); Console.WriteLine("Service Started @ " + DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss")); Console.ReadLine(); } }
稍微解釋一下,
1. 實例化一個serviceBehavior,這個屬於System.ServiceModel.Description命名空間。
2. 把這個serviceBehavior添加到host的description的behaviors集合中。
3. 給host 添加一個endpoint,三個參數分別描述了Contract,Binding類型,Address。
然后再啟動這個服務。
測試一下客戶端的調用。
重新添加Reference:
測試調用:
works fine。
這集講了動態配置endpoint,雖然暫時還沒有什么實際項目經歷,不過可以讓我們加深一些對WCF Endpoint的理解。
Thank you!