溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

WCF服務(wù)如何操作SayHello()

發(fā)布時(shí)間:2021-12-15 09:22:46 來源:億速云 閱讀:116 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“WCF服務(wù)如何操作SayHello()”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

這里我們分別定義了三種激活類型的服務(wù)類:?jiǎn)握{(diào)服務(wù)(Per-CallService),會(huì)話服務(wù)(SessionfulService),單例服務(wù)(SingletonService),托管宿主分別進(jìn)行托管,這里為了測(cè)試,我們使用的綁定協(xié)議也是TCP方式,其他的協(xié)議這里沒做具體的實(shí)現(xiàn),有興趣的朋友可以自己擴(kuò)展修改代碼,進(jìn)行測(cè)試。

WCF服務(wù)支持三種實(shí)例激活的類型:?jiǎn)握{(diào)服務(wù)(Per-Call Service):每次的客戶端請(qǐng)求分配一個(gè)新的服務(wù)實(shí)例。類似于Net Remoting的SingleCall模式;會(huì)話服務(wù)(Sessionful Service):則為每次客戶端連接分配一個(gè)服務(wù)實(shí)例。類似于Net Remoting的客戶端激活模式;單例服務(wù)(Singleton Service):所有的客戶端會(huì)為所有的連接和激活對(duì)象共享一個(gè)相同的服務(wù)實(shí)例。類似于Net Remoting的SingleTon模式。

WCF服務(wù):定義了一個(gè)服務(wù)契約,一個(gè)操作SayHello(),具體的WCF服務(wù)類型定義和激活類型配置如下:

//此例定義了一個(gè)服務(wù)契約,三種服務(wù)分別為單調(diào)服務(wù)、會(huì)話服務(wù)、單例服務(wù)或單件服務(wù)  namespaceWCFService  {  //1.服務(wù)契約  [ServiceContract(SessionModeSessionMode=SessionMode.Allowed,Namespace="http://www.cnblogs.com/frank_xl/")]  publicinterfaceIWCFService  {  //操作契約  [OperationContract]  voidSayHello();  }  //2.服務(wù)類.單調(diào)服務(wù)  [ServiceBehavior(InstanceContextModeInstanceContextMode=InstanceContextMode.PerCall)]  publicclassWCFServicePerCall:IWCFService,IDisposable  {  //服務(wù)實(shí)例計(jì)數(shù)  privateintmCcount=0;  //構(gòu)造函數(shù)  publicWCFServicePerCall()  {  Console.WriteLine("WCFServicePerCallInstanceisCreated");  }  //實(shí)現(xiàn)接口定義的方法  publicvoidSayHello()  {  mCcount++;  Console.WriteLine("WCFServicePerCallInstanceCountis:{0}",mCcount);  }  //實(shí)現(xiàn)接口定義的方法Dispose  publicvoidDispose()  {  Console.WriteLine("WCFServicePerCallInstanceisdisposed");  }  }  //3.服務(wù)類.會(huì)話服務(wù)  [ServiceBehavior(InstanceContextModeInstanceContextMode=InstanceContextMode.PerSession)]  publicclassWCFServicePerSession:IWCFService  {  //服務(wù)實(shí)例計(jì)數(shù)  privateintmCcount=0;  //構(gòu)造函數(shù)  publicWCFServicePerSession()  {  Console.WriteLine("WCFServicePerSessionInstanceisCreated");  }  //實(shí)現(xiàn)接口定義的方法  publicvoidSayHello()  {  mCcount++;  Console.WriteLine("WCFServicePerSessionInstanceCountis:{0}",mCcount);  }  //實(shí)現(xiàn)接口定義的方法Dispose  publicvoidDispose()  {  Console.WriteLine("WCFServicePerSessionInstanceisdisposed");  }  }  //4.服務(wù)類.單例服務(wù)  [ServiceBehavior(InstanceContextModeInstanceContextMode=InstanceContextMode.Single)]  publicclassWCFServiceSingleTon:IWCFService  {  //服務(wù)實(shí)例計(jì)數(shù)  privateintmCcount=0;  //構(gòu)造函數(shù)  publicWCFServiceSingleTon()  {  Console.WriteLine("WCFServiceSingleTonInstanceisCreated");  }  //實(shí)現(xiàn)接口定義的方法  publicvoidSayHello()  {  mCcount++;  Console.WriteLine("WCFServiceSingleTonInstanceCountis:{0}",mCcount);  }  //實(shí)現(xiàn)接口定義的方法Dispose  publicvoidDispose()  {  Console.WriteLine("WCFServiceSingleTonInstanceisdisposed");  }  }   }

“WCF服務(wù)如何操作SayHello()”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI