<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引頁]</a>
化零為整WCF(10) - 執行個體模型(InstanceContextMode)
介紹
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();
}
/// 示範執行個體模型的類(PerCall)
/// <remarks>
/// InstanceContextMode - 擷取或設定訓示新服務對象何時建立的值。
/// InstanceContextMode.PerCall - 新的 System.ServiceModel.InstanceContext 對象在每次調用前建立,在調用後回收。
/// </remarks>
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class PerCallMode : IPerCallMode
private int _counter;
public int Counter()
{
_counter++;
return _counter;
}
}
PerSessionMode.cs
/// 示範執行個體模型的接口(PerSession)
[ServiceContract()]
public interface IPerSessionMode
/// InstanceContextMode.PerSession - 為每個會話建立一個新的 System.ServiceModel.InstanceContext 對象。
/// InstanceContextMode 的預設值為 InstanceContextMode.PerSession
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class PerSessionMode : IPerSessionMode
SingleMode.cs
/// 示範執行個體模型的接口(Single)
public interface ISingleMode
/// InstanceContextMode.Single - 隻有一個 System.ServiceModel.InstanceContext 對象用于所有傳入呼叫,并且在調用後不回收。如果服務對象不存在,則建立一個。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class SingleMode : ISingleMode
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 name="WCF.ServiceLib.InstanceMode.SingleMode" behaviorConfiguration="InstanceModeBehavior">
<endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.InstanceMode.ISingleMode" />
</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"
/>
<asp:Button ID="btnPerSessionMode" runat="server" Text="PerSessionMode"
<asp:Button ID="btnSingleMode" runat="server" Text="SingleMode"
</asp:Content>
Hello.aspx.cs
using System.Collections;
using System.Configuration;
using System.Data;
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();
string.Format("alert('計數器:{0}')", (Session["proxy"] as InstanceModeSvc.PerSessionMode.PerSessionModeClient).Counter()),
protected void btnSingleMode_Click(object sender, EventArgs e)
var proxy = new InstanceModeSvc.SingleMode.SingleModeClient();
<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>
運作結果:
單擊"btnPerCallMode"按鈕,每次單擊,計數器都傳回1
單擊"btnPerSessionMode"按鈕,每次單擊并且會話相同,計數器會累加
單擊"btnSingleMode"按鈕,每次單擊,計數器都累加
OK
<a href="http://down.51cto.com/data/100781" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344149,如需轉載請自行聯系原作者