免费体验120秒视频_榴莲榴莲榴莲榴莲官网_2021国产麻豆剧果冻传媒入口_一二三四视频社区在线
軟件首頁 |  文章首頁
最新更新 軟件分類 設為首頁 加入收藏 聯系我們
當前位置:首頁文章首頁 IT學院 VC(VC++)

WCF學習:Instance context model(實例模型) 與 Session(會話) 的關系

作者:東坡下載  來源:uzzf  發布時間:2010-10-14 9:19:37  點擊:

        在WCF中對Session是默認支持的,但是和ASP.NET中的支持完全不同,說到Session,那么肯定有服務端(Service)和客戶端(Client),客戶端通過代理(Proxy)來訪問服務端,所以Session的周期和Proxy的周期綁定。對分布式的程序而言,根據業務的要求,我們會有三種需求:
        第一:服務端不用保存客戶端的狀態,每次客戶端的訪問都是獨立的;
        第二:服務端需要保持客戶端的狀態,每次客服端的請求會用到同一個Session; 
        第三:在多用戶中共享同一個實例(其實這里類似ASP.NET中的Application,但完全不一樣)。
       下面我們就針對上述的三種要求,分享一下再WCF中的實現方式. 以及各種實現方式的優勢和缺點。對上述三種使用場景,WCF已經提供了良好的支持,這就是Instancing Management。具體是實現方式是:Instance context model?梢赃@么說,Instance Context Mode決定著不同的Session表現。WCF中有三種 Instance Context Mode,他們分別是:
        >> Per-Call:每次的客戶端請求分配一個新的服務實例。 類似于Net Remoting的SingleCall模式, 在這種方式下,程序的擴展性是最強的,在事務編程與隊列服務中優勢更為明顯。但是由于頻繁地創建與銷毀實例,會對性能造成一定的影響。當然網上的牛人,已經考慮到使用線程池的方式來減少頻繁地創建與銷毀實例(參考地址:http://www.cnblogs.com/artech/archive/2008/08/05/1260594.html)
        >> PerSession: 服務端需要保持客戶端的狀態,為每次客戶端連接分配一個服務實例,客戶端的每次調用,會使用到同一個實例,如果實例銷毀,客戶端的調用會拋出異常。類似于Net Remoting的客戶端激活模式;這是wcf的默認支持方式. 由于每個客戶端都需要維護一個會話,需要占用較多的資源來保存服務會話狀態。如果存在多個獨立的客戶端,則創建專門的服務實例的代價太大。
        >> Singleton: 所有客戶端而言,都只有一個服務實例,當服務端被host的時候,就會創建,有且僅有一個服務實例來響應客戶端服務調用的請求,在多個客戶端請求下,服務端只會處理一個客戶端的請求,其他的排隊等候處理。因此在系統的吞吐量、相應效率、系統服務性能上都存在嚴重的瓶頸
好處是,可以共享數據.
      特別的強調兩點:
      第一:上述三種 Instance context model ,但是并不是所有的Binding都支持 Session ,對Per-Call和Singleton而言,binding影響不大。對PerSession,就必須使用特定的Binding, 才可以,否則最終指向的是PerCall,見下表:      

Binding

Session mode

Context mode

Async Dispose()

Instance mode

Basic

Allowed/NotAllowed

PerCall/PerSession

Yes

PerCall

TCP, IPC

Allowed/Required

PerCall

No

PerCall

TCP, IPC

Allowed/Required

PerSession

Yes

PerSession

WS (no security, no reliability)

NotAllowed/Allowed

PerCall/PerSession

Yes

PerCall

WS (with security or reliability)

Allowed/Required

PerSession

Yes

PerSession

WS (with security or reliability)

NotAllowed

PerCall/PerSession

Yes

PerCall

 

      第二:由于PerSession, 為每次客戶端連接分配一個服務實例,會消耗服務器的資源,所以可以把PerSession和PerCall結合使用。在特定的條件下我們完全可以在客戶端的Message中包括特定的用戶****來,請求PerCall,來代替每次請求PerSession。當然這是在特定的情況下。
      下面來看具體的執行代碼:
      1、建立服務契約

    [ServiceContract(Namespace = "http://www.cnblogs.com/wanqiming/")]
    public interface IInstanceContextMode
    {
        //操作契約
        [OperationContract]
        void SayHello();
    }

 

      2、實現PerCall,PerSession,Single的服務類

    #region PerCall

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WCFServicePerCall : IInstanceContextMode, IDisposable
    {
        //服務實例計數
        private int mCcount = 0;
        //構造函數
        public WCFServicePerCall()
        {
            Console.WriteLine("WCFServicePerCall Instance is Created ");
        }
        //實現接口定義的方法
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServicePerCall Instance Count is: {0} ", mCcount);
        }
        //實現接口定義的方法Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServicePerCall Instance is disposed ");
        }
    }
    #endregion

 

    #region PerSession
    //3.服務類.會話服務
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class WCFServicePerSession : IInstanceContextMode
    {
        //服務實例計數
        private int mCcount = 0;
        //構造函數
        public WCFServicePerSession()
        {
            Console.WriteLine("WCFServicePerSession Instance is Created ");
        }
        //實現接口定義的方法
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServicePerSession Instance Count is: {0} ", mCcount);
        }
        //實現接口定義的方法Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServicePerSession Instance is disposed ");
        }
    }
    #endregion

    #region Single
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class WCFServiceSingleTon : IInstanceContextMode
    {
        //服務實例計數
        private int mCcount = 0;
        //構造函數
        public WCFServiceSingleTon()
        {
            Console.WriteLine("WCFServiceSingleTon Instance is Created ");
        }
        //實現接口定義的方法
        public void SayHello()
        {
            mCcount++;
            Console.WriteLine("WCFServiceSingleTon Instance Count is: {0} ", mCcount);
        }
        //實現接口定義的方法Dispose
        public void Dispose()
        {
            Console.WriteLine("WCFServiceSingleTon Instance is disposed ");
        }
    }
    #endregion

       第三:服務端的配置文件

      <behaviors>
        <serviceBehaviors>
          <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="100" maxConcurrentInstances="100" />
            <serviceDebug includeExceptionDetailInFaults="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>

 

      <services>
          
        <service  name="Fish.ServiceImpl.WCFServicePerCall">
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8080/Fish/WCFServicePerCall/>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServicePerCall"/>
            </baseAddresses>
          </host>
        </service>

        <service name="Fish.ServiceImpl.WCFServicePerSession">
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServicePerSession"/>
              <add baseAddress="http://localhost:8080/Fish/WCFServicePerSession/>
            </baseAddresses>
          </host>
        </service>
       
        <service behaviorConfiguration="DefaultBehavior" name="Fish.ServiceImpl.WCFServiceSingleTon">
          <endpoint address="" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <endpoint address="" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode"></endpoint>
          <host>
            <baseAddresses>
              <add baseAddress="net.tcp://localhost:808/Fish/WCFServiceSingleTon"/>
              <add baseAddress="http://localhost:8080/Fish/WCFServiceSingleTon/>
            </baseAddresses>
          </host>
        </service>
          

       第四:Host

 

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List<ServiceHost> hosts = new List<ServiceHost>();
                hosts.Add(new ServiceHost(typeof(WCFServicePerCall)));          //PerCall
                hosts.Add(new ServiceHost(typeof(WCFServicePerSession)));       //PerSession
                hosts.Add(new ServiceHost(typeof(WCFServiceSingleTon)));        //SingleTon
                
                hosts.ForEach(host => host.Open());

                Console.WriteLine("All services are started...");
                Console.Read();
            }
            catch(Exception e)
            {
                Console.WriteLine("錯誤信息是:"+e.ToString());
            }
        }
    }

 

      第五:客戶端執行代碼

class Program
    {
        static void Main(string[] args)
        {
            PerCall();

            PerSession();

            Single();

            Console.WriteLine("  ");

        }

        #region PerCall
        private static void PerCall()
        {
            var perCall = new ChannelFactory<IInstanceContextMode>("tcpPerCall").CreateChannel();
            perCall.SayHello();
            perCall.SayHello();
            perCall.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perCall);
            
            var tcpPerCall = new ChannelFactory<IInstanceContextMode>("tcpPerCall").CreateChannel();
            tcpPerCall.SayHello();
            tcpPerCall.SayHello();
            tcpPerCall.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerCall);

 

        }
        #endregion

        #region PerSession
        private static void PerSession()
        {
            var perSession = new ChannelFactory<IInstanceContextMode>("basicHttpPerSession").CreateChannel();
            perSession.SayHello();
            perSession.SayHello();
            perSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perSession);

            var tcpPerSession = new ChannelFactory<IInstanceContextMode>("tcpPerSession").CreateChannel();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerSession);


        }
        #endregion

        #region Single
        private static void Single()
        {
            var perSession = new ChannelFactory<IInstanceContextMode>("basicHttpSingleTon").CreateChannel();
            perSession.SayHello();
            perSession.SayHello();
            perSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(perSession);

            var tcpPerSession = new ChannelFactory<IInstanceContextMode>("tcpSingleTon").CreateChannel();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            tcpPerSession.SayHello();
            ServiceBroker.DisposeService<IInstanceContextMode>(tcpPerSession);


        }
        #endregion
    }



      第六:客戶端配置文件
    <client>
      <endpoint name="httpPerCall" address="http://localhost:8080/Fish/WCFServicePerCall" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpPerCall" address="net.tcp://localhost:808/Fish/WCFServicePerCall" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="basicHttpPerSession" address="http://localhost:8080/Fish/WCFServicePerSession" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpPerSession" address="net.tcp://localhost:808/Fish/WCFServicePerSession" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="basicHttpSingleTon" address="http://localhost:8080/Fish/WCFServiceSingleTon" binding="basicHttpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
      <endpoint name="tcpSingleTon" address="net.tcp://localhost:808/Fish/WCFServiceSingleTon" binding="netTcpBinding" contract="Fish.ServiceInterfaces.IInstanceContextMode" ></endpoint>
    </client>
      執行的結果如下:

相關軟件

文章評論

本類推薦文章

關于本站 | 網站幫助 | 廣告合作 | 下載聲明 | 友情連接 | 網站地圖
Copyright © 20098-2010 uzzf下載站. All Rights Reserved .
免费体验120秒视频_榴莲榴莲榴莲榴莲官网_2021国产麻豆剧果冻传媒入口_一二三四视频社区在线
主站蜘蛛池模板: 国产不卡免费视频| 亚洲码欧美码一区二区三区| 久久网免费视频| 精品无码av一区二区三区 | 精品无码久久久久久久久水蜜桃| 国内精品久久久久精品| 69久久夜色精品国产69| 99久久综合狠狠综合久久| 成人精品国产亚洲欧洲| 欧美激情福利| 国产成人cao在线| 亚洲视频一区在线播放| 日产乱码卡一卡2卡3视频| 国产热热| 男人j进女人p一进一出视频| √天堂8资源中文在线| 色翁荡息又大又硬又粗又爽| 国产网站免费看| 亚洲av永久综合在线观看尤物| 久久这里精品国产99丫e6| 乱中年女人伦av一区二区| 亚洲第一综合天堂另类专| 亚洲国产美女精品久久久久| 久久99精品久久久久久噜噜| 国产成人av三级在线观看| 亚洲精品动漫免费二区| 欧美最猛黑人xxxx| 青楼18春一级毛片| 亚洲精品综合久久中文字幕| 欧男同同性videos免费| 777成影片免费观看| 青青草国产免费| 在线免费亚洲| 喜欢老头吃我奶躁我的动图| 中文国产成人精品久久久| 免费看曰批女人爽的视频网址| 午夜两性色视频免费网站| 日本一区二区三区日本免费| 扫出来是很污的二维码2021| 强3d不知火舞视频无掩挡网站| 欧洲vodafonewifi14|