[源码下载]
化零为整WCF(10) - 实例模型(InstanceContextMode)
作者:webabcd
介绍
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;
}
}
}
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;
}
}
}
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;
}
}
}
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>
<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>
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();
}
}
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>
<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
[源码下载]