介紹
WCF(Windows Communication Foundation) - 實例模型:
ServiceBehavior
·InstanceContextMode.PerCall - 新的 System.ServiceModel.InstanceContext 對象在每次調用前創建,在調用後回收。
·InstanceContextMode.PerSession - 為每個會話創建一個新的 System.ServiceModel.InstanceContext 對象。
·InstanceContextMode.Single - 只有一個 System.ServiceModel.InstanceContext 對象用於所有傳入呼叫,並且在調用後不回收。如果服務對象不存在,則創建一個。
示例
1、服務
PerCallMode.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WCF.ServiceLib.InstanceMode { /**//// <summary> /// 演示實例模型的接口(PerCall) /// </summary> [ServiceContract] public interface IPerCallMode { /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> [OperationContract] int Counter(); } /**//// <summary> /// 演示實例模型的類(PerCall) /// </summary> /// <remarks> /// InstanceContextMode - 獲取或設置指示新服務對象何時創建的值。 /// InstanceContextMode.PerCall - 新的 System.ServiceModel.InstanceContext 對象在每次調用前創建,在調用後回收。 /// </remarks> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class PerCallMode : IPerCallMode { private int _counter; /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> public int Counter() { _counter++; return _counter; } } }
PerSessionMode.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WCF.ServiceLib.InstanceMode { /**//// <summary> /// 演示實例模型的接口(PerSession) /// </summary> [ServiceContract()] public interface IPerSessionMode { /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> [OperationContract] int Counter(); } /**//// <summary> /// 演示實例模型的類(PerCall) /// </summary> /// <remarks> /// InstanceContextMode - 獲取或設置指示新服務對象何時創建的值。 /// InstanceContextMode.PerSession - 為每個會話創建一個新的 System.ServiceModel.InstanceContext 對象。 /// InstanceContextMode 的默認值為 InstanceContextMode.PerSession /// </remarks> [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class PerSessionMode : IPerSessionMode { private int _counter; /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> public int Counter() { _counter++; return _counter; } } }
SingleMode.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WCF.ServiceLib.InstanceMode { /**//// <summary> /// 演示實例模型的接口(Single) /// </summary> [ServiceContract] public interface ISingleMode { /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> [OperationContract] int Counter(); } /**//// <summary> /// 演示實例模型的接口(Single) /// </summary> /// <remarks> /// InstanceContextMode - 獲取或設置指示新服務對象何時創建的值。 /// InstanceContextMode.Single - 只有一個 System.ServiceModel.InstanceContext 對象用於所有傳入呼叫,並且在調用後不回收。如果服務對象不存在,則創建一個。 /// </remarks> [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class SingleMode : ISingleMode { private int _counter; /**//// <summary> /// 獲取計數器結果 /// </summary> /// <returns></returns> public int Counter() { _counter++; return _counter; } } }
2、宿主
PerCallMode.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.InstanceMode.PerCallMode" %>
PerSessionMode.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.InstanceMode.PerSessionMode" %>
SingleMode.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.InstanceMode.SingleMode" %>
Web.config
<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="InstanceModeBehavior"> <!--httpGetEnabled - 使用get方式提供服務--> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <!--name - 提供服務的類名--> <!--behaviorConfiguration - 指定相關的行為配置--> <service name="WCF.ServiceLib.InstanceMode.PerCallMode" behaviorConfiguration="InstanceModeBehavior"> <!--address - 服務地址--> <!--binding - 通信方式--> <!--contract - 服務契約--> <endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.InstanceMode.IPerCallMode" /> </service> <service name="WCF.ServiceLib.InstanceMode.PerSessionMode" behaviorConfiguration="InstanceModeBehavior"> <!--bindingConfiguration - 指定相關的綁定配置--> <endpoint address="" binding="wsHttpBinding" contract="WCF.ServiceLib.InstanceMode.IPerSessionMode" bindingConfiguration="PerSessionModeBindingConfiguration"/> </service> <service name="WCF.ServiceLib.InstanceMode.SingleMode" behaviorConfiguration="InstanceModeBehavior"> <endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.InstanceMode.ISingleMode" /> </service> </services> <bindings> <wsHttpBinding> <!--wsHttpBinding 可提供 安全會話 和 可靠會話--> <binding name="PerSessionModeBindingConfiguration"> <!--指示是否在通道終結點之間建立 WS-RM (WS-ReliableMessaging) 可靠會話。默認值為 false。--> <reliableSession enabled="true"/> <security> <!--此屬性控制安全上下文令牌是否通過客戶端與服務之間的 WS-SecureConversation 交換建立。將它設置為 true 要求遠程方支持 WS-SecureConversation。--> <message establishSecurityContext="true"/> </security> </binding> </wsHttpBinding> </bindings> </system.serviceModel> </configuration>
3、客戶端
Hello.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs" Inherits="InstanceMode_Hello" Title="實例模型(InstanceContextMode)" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <asp:Button ID="btnPerCallMode" runat="server" Text="PerCallMode" onclick="btnPerCallMode_Click" /> <asp:Button ID="btnPerSessionMode" runat="server" Text="PerSessionMode" onclick="btnPerSessionMode_Click" /> <asp:Button ID="btnSingleMode" runat="server" Text="SingleMode" onclick="btnSingleMode_Click" /> </asp:Content>
Hello.aspx.cs
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class InstanceMode_Hello : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnPerCallMode_Click(object sender, EventArgs e) { var proxy = new InstanceModeSvc.PerCallMode.PerCallModeClient(); Page.ClientScript.RegisterStartupScript( this.GetType(), "js", string.Format("alert('計數器:{0}')", proxy.Counter()), true); proxy.Close(); } protected void btnPerSessionMode_Click(object sender, EventArgs e) { if (Session["proxy"] == null) Session["proxy"] = new InstanceModeSvc.PerSessionMode.PerSessionModeClient(); Page.ClientScript.RegisterStartupScript( this.GetType(), "js", string.Format("alert('計數器:{0}')", (Session["proxy"] as InstanceModeSvc.PerSessionMode.PerSessionModeClient).Counter()), true); } protected void btnSingleMode_Click(object sender, EventArgs e) { var proxy = new InstanceModeSvc.SingleMode.SingleModeClient(); Page.ClientScript.RegisterStartupScript( this.GetType(), "js", string.Format("alert('計數器:{0}')", proxy.Counter()), true); proxy.Close(); } }
Web.config
<?xml version="1.0"?> <configuration> <system.serviceModel> <client> <!--address - 服務地址--> <!--binding - 通信方式--> <!--contract - 服務契約--> <endpoint address="http://localhost:3502/ServiceHost/InstanceMode/PerCallMode.svc" binding="basicHttpBinding" contract="InstanceModeSvc.PerCallMode.IPerCallMode" /> <!--bindingConfiguration - 指定相關的綁定配置--> <endpoint address="http://localhost:3502/ServiceHost/InstanceMode/PerSessionMode.svc" binding="wsHttpBinding" contract="InstanceModeSvc.PerSessionMode.IPerSessionMode" bindingConfiguration="PerSessionModeBindingConfiguration" /> <endpoint address="http://localhost:3502/ServiceHost/InstanceMode/SingleMode.svc" binding="basicHttpBinding" contract="InstanceModeSvc.SingleMode.ISingleMode" /> </client> <bindings> <wsHttpBinding> <binding name="PerSessionModeBindingConfiguration"> <!--指示是否在通道終結點之間建立 WS-RM (WS-ReliableMessaging) 可靠會話。默認值為 false。--> <reliableSession enabled="true"/> <security> <!--此屬性控制安全上下文令牌是否通過客戶端與服務之間的 WS-SecureConversation 交換建立。將它設置為 true 要求遠程方支持 WS-SecureConversation。--> <message establishSecurityContext="true"/> </security> </binding> </wsHttpBinding> </bindings> </system.serviceModel> </configuration>
運行結果:
單擊"btnPerCallMode"按鈕,每次單擊,計數器都返回1
單擊"btnPerSessionMode"按鈕,每次單擊並且會話相同,計數器會累加
單擊"btnSingleMode"按鈕,每次單擊,計數器都累加
OK