WCF學習:Instance context model(實例模型) 與 Session(會話) 的關系
在WCF中對Session是默認支持的,但是和ASP.NET中的支持完全不同,說到Session,那么肯定有服務端(Service)和客戶端(Client),客戶端通過代理(Proxy)來訪問服務端,所以Session的周期和Proxy的周期綁定。對分布式的程序而言,根據(jù)業(yè)務的要求,我們會有三種需求:
第一:服務端不用保存客戶端的狀態(tài),每次客戶端的訪問都是獨立的;
第二:服務端需要保持客戶端的狀態(tài),每次客服端的請求會用到同一個Session;
第三:在多用戶中共享同一個實例(其實這里類似ASP.NET中的Application,但完全不一樣)。
下面我們就針對上述的三種要求,分享一下再WCF中的實現(xiàn)方式. 以及各種實現(xiàn)方式的優(yōu)勢和缺點。對上述三種使用場景,WCF已經(jīng)提供了良好的支持,這就是Instancing Management。具體是實現(xiàn)方式是:Instance context model?梢赃@么說,Instance Context Mode決定著不同的Session表現(xiàn)。WCF中有三種 Instance Context Mode,他們分別是:
>> Per-Call:每次的客戶端請求分配一個新的服務實例。 類似于Net Remoting的SingleCall模式, 在這種方式下,程序的擴展性是最強的,在事務編程與隊列服務中優(yōu)勢更為明顯。但是由于頻繁地創(chuàng)建與銷毀實例,會對性能造成一定的影響。當然網(wǎng)上的牛人,已經(jīng)考慮到使用線程池的方式來減少頻繁地創(chuàng)建與銷毀實例(參考地址:http://www.cnblogs.com/artech/archive/2008/08/05/1260594.html)
>> PerSession: 服務端需要保持客戶端的狀態(tài),為每次客戶端連接分配一個服務實例,客戶端的每次調(diào)用,會使用到同一個實例,如果實例銷毀,客戶端的調(diào)用會拋出異常。類似于Net Remoting的客戶端激活模式;這是wcf的默認支持方式. 由于每個客戶端都需要維護一個會話,需要占用較多的資源來保存服務會話狀態(tài)。如果存在多個獨立的客戶端,則創(chuàng)建專門的服務實例的代價太大。
>> Singleton: 所有客戶端而言,都只有一個服務實例,當服務端被host的時候,就會創(chuàng)建,有且僅有一個服務實例來響應客戶端服務調(diào)用的請求,在多個客戶端請求下,服務端只會處理一個客戶端的請求,其他的排隊等候處理。因此在系統(tǒng)的吞吐量、相應效率、系統(tǒng)服務性能上都存在嚴重的瓶頸
好處是,可以共享數(shù)據(jù).
特別的強調(diào)兩點:
第一:上述三種 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結(jié)合使用。在特定的條件下我們完全可以在客戶端的Message中包括特定的用戶****來,請求PerCall,來代替每次請求PerSession。當然這是在特定的情況下。
下面來看具體的執(zhí)行代碼:
1、建立服務契約
public interface IInstanceContextMode
{
//操作契約
[OperationContract]
void SayHello();
}
#region PerCall
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]public class WCFServicePerCall : IInstanceContextMode, IDisposable
{
//服務實例計數(shù)
private int mCcount = 0;
//構造函數(shù)
public WCFServicePerCall()
{
Console.WriteLine("WCFServicePerCall Instance is Created ");
}
//實現(xiàn)接口定義的方法
public void SayHello()
{
mCcount++;
Console.WriteLine("WCFServicePerCall Instance Count is: {0} ", mCcount);
}
//實現(xiàn)接口定義的方法Dispose
public void Dispose()
{
Console.WriteLine("WCFServicePerCall Instance is disposed ");
}
}
#endregion
#region PerSession
//3.服務類.會話服務
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class WCFServicePerSession : IInstanceContextMode
{
//服務實例計數(shù)
private int mCcount = 0;
//構造函數(shù)
public WCFServicePerSession()
{
Console.WriteLine("WCFServicePerSession Instance is Created ");
}
//實現(xiàn)接口定義的方法
public void SayHello()
{
mCcount++;
Console.WriteLine("WCFServicePerSession Instance Count is: {0} ", mCcount);
}
//實現(xiàn)接口定義的方法Dispose
public void Dispose()
{
Console.WriteLine("WCFServicePerSession Instance is disposed ");
}
}
#endregion
#region Single
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class WCFServiceSingleTon : IInstanceContextMode
{
//服務實例計數(shù)
private int mCcount = 0;
//構造函數(shù)
public WCFServiceSingleTon()
{
Console.WriteLine("WCFServiceSingleTon Instance is Created ");
}
//實現(xiàn)接口定義的方法
public void SayHello()
{
mCcount++;
Console.WriteLine("WCFServiceSingleTon Instance Count is: {0} ", mCcount);
}
//實現(xiàn)接口定義的方法Dispose
public void Dispose()
{
Console.WriteLine("WCFServiceSingleTon Instance is disposed ");
}
}
#endregion
第三:服務端的配置文件
<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
}
<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>