WCF釋放服務(wù)對(duì)象最直接方式解讀
作者:佚名 
  我們今天將會(huì)為大家介紹一個(gè)比較基礎(chǔ)直接的方式來進(jìn)行WCF釋放服務(wù)對(duì)象,希望對(duì)此又需要的朋友們可以從中獲得一些幫助。
 WCF中低服務(wù)對(duì)象的釋放,可以通過多種方法來進(jìn)行實(shí)現(xiàn),其中一個(gè),ReleaseServiceInstance這一WCF釋放服務(wù)對(duì)象的方式應(yīng)該是最直接的方式了。看下面例子的輸出結(jié)果,我們可以看到在客戶端代理對(duì)象釋放之前,服務(wù)實(shí)例就被釋放了。
WCF釋放服務(wù)對(duì)象具體代碼示例:
- [ServiceContract(SessionModeSessionMode = SessionMode.Required)]
 - public interface IMyService
 - {
 - [OperationContract]
 - void Test();
 - }
 - [ServiceBehavior(InstanceContextModeInstanceContextMode =
 
InstanceContextMode.PerSession)]- public class MyServie : IMyService, IDisposable
 - {
 - public MyServie()
 - {
 - Console.WriteLine("Constructor");
 - }
 - [OperationBehavior]
 - public void Test()
 - {
 - OperationContext.Current.InstanceContext.ReleaseServiceInstance();
 - }
 - public void Dispose()
 - {
 - Console.WriteLine("Dispose");
 - }
 - }
 - public class WcfTest
 - {
 - public static void Test()
 - {
 - AppDomain.CreateDomain("Server").DoCallBack(delegate
 - {
 - ServiceHost host = new ServiceHost(typeof(MyServie),
 
new Uri("http://localhost:8080/MyService"));- host.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "");
 - host.Open();
 - });
 - //-----------------------
 - IMyService channel = ChannelFactory<IMyService>.CreateChannel
 
(new WSHttpBinding(),- new EndpointAddress("http://localhost:8080/MyService"));
 - using (channel as IDisposable)
 - {
 - channel.Test();
 - Thread.Sleep(2000);
 - Console.WriteLine("Dispose Client Proxy...");
 - }
 - }
 - }
 
輸出:
- Constructor
 - Dispose
 - Dispose Client Proxy...
 
以上就是對(duì)WCF釋放服務(wù)對(duì)象的相關(guān)介紹。
【編輯推薦】
責(zé)任編輯:曹凱 
                    來源:
                    CSDN
 














 
 
 
 
 
 
 