博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
化零为整WCF(10) - 实例模型(InstanceContextMode)
阅读量:5915 次
发布时间:2019-06-19

本文共 8373 字,大约阅读时间需要 27 分钟。


 



化零为整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
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.InstanceMode 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的接口(PerCall) 

InBlock.gif        
/// </summary> 

InBlock.gif        [ServiceContract] 

InBlock.gif        
public 
interface IPerCallMode 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                [OperationContract] 

InBlock.gif                
int Counter(); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的类(PerCall) 

InBlock.gif        
/// </summary> 

InBlock.gif        
/// <remarks> 
InBlock.gif        /// InstanceContextMode - 获取或设置指示新服务对象何时创建的值。 
InBlock.gif        /// InstanceContextMode.PerCall - 新的 System.ServiceModel.InstanceContext 对象在每次调用前创建,在调用后回收。 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 

InBlock.gif        
public 
class PerCallMode : IPerCallMode 

InBlock.gif        { 

InBlock.gif                
private 
int _counter; 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public 
int Counter() 

InBlock.gif                { 

InBlock.gif                        _counter++; 

InBlock.gif 

InBlock.gif                        
return _counter; 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
PerSessionMode.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.InstanceMode 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的接口(PerSession) 

InBlock.gif        
/// </summary> 

InBlock.gif        [ServiceContract()] 

InBlock.gif        
public 
interface IPerSessionMode 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                [OperationContract] 

InBlock.gif                
int Counter(); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的类(PerCall) 

InBlock.gif        
/// </summary> 

InBlock.gif        
/// <remarks> 
InBlock.gif        /// InstanceContextMode - 获取或设置指示新服务对象何时创建的值。 
InBlock.gif        /// InstanceContextMode.PerSession - 为每个会话创建一个新的 System.ServiceModel.InstanceContext 对象。 
InBlock.gif        /// InstanceContextMode 的默认值为 InstanceContextMode.PerSession 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] 

InBlock.gif        
public 
class PerSessionMode : IPerSessionMode 

InBlock.gif        { 

InBlock.gif                
private 
int _counter; 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public 
int Counter() 

InBlock.gif                { 

InBlock.gif                        _counter++; 

InBlock.gif 

InBlock.gif                        
return _counter; 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
SingleMode.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections.Generic; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Text; 

InBlock.gif 

InBlock.gif
using System.ServiceModel; 

InBlock.gif 

InBlock.gif
namespace WCF.ServiceLib.InstanceMode 

InBlock.gif

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的接口(Single) 

InBlock.gif        
/// </summary> 

InBlock.gif        [ServiceContract] 

InBlock.gif        
public 
interface ISingleMode 

InBlock.gif        { 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                [OperationContract] 

InBlock.gif                
int Counter(); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
/// <summary> 

InBlock.gif        
/// 演示实例模型的接口(Single) 

InBlock.gif        
/// </summary> 

InBlock.gif        
/// <remarks> 
InBlock.gif        /// InstanceContextMode - 获取或设置指示新服务对象何时创建的值。 
InBlock.gif        /// InstanceContextMode.Single - 只有一个 System.ServiceModel.InstanceContext 对象用于所有传入呼叫,并且在调用后不回收。如果服务对象不存在,则创建一个。 
InBlock.gif        /// </remarks> 

InBlock.gif        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

InBlock.gif        
public 
class SingleMode : ISingleMode 

InBlock.gif        { 

InBlock.gif                
private 
int _counter; 

InBlock.gif 

InBlock.gif                
/// <summary> 

InBlock.gif                
/// 获取计数器结果 

InBlock.gif                
/// </summary> 

InBlock.gif                
/// <returns></returns> 

InBlock.gif                
public 
int Counter() 

InBlock.gif                { 

InBlock.gif                        _counter++; 

InBlock.gif 

InBlock.gif                        
return _counter; 

InBlock.gif                } 

InBlock.gif        } 

InBlock.gif}
 
 
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"    

                 /> 

          

        <asp:Button ID="btnPerSessionMode" runat="server" Text="PerSessionMode"    

                 /> 

          

        <asp:Button ID="btnSingleMode" runat="server" Text="SingleMode"    

                 /> 

</asp:Content>
 
Hello.aspx.cs
InBlock.gif
using System; 

InBlock.gif
using System.Collections; 

InBlock.gif
using System.Configuration; 

InBlock.gif
using System.Data; 

InBlock.gif
using System.Linq; 

InBlock.gif
using System.Web; 

InBlock.gif
using System.Web.Security; 

InBlock.gif
using System.Web.UI; 

InBlock.gif
using System.Web.UI.HtmlControls; 

InBlock.gif
using System.Web.UI.WebControls; 

InBlock.gif
using System.Web.UI.WebControls.WebParts; 

InBlock.gif
using System.Xml.Linq; 

InBlock.gif 

InBlock.gif
public partial 
class InstanceMode_Hello : System.Web.UI.Page 

InBlock.gif

InBlock.gif        
protected 
void Page_Load(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
protected 
void btnPerCallMode_Click(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif 

InBlock.gif                var proxy = 
new InstanceModeSvc.PerCallMode.PerCallModeClient(); 

InBlock.gif 

InBlock.gif                Page.ClientScript.RegisterStartupScript( 

InBlock.gif                        
this.GetType(), 

InBlock.gif                        
"js"

InBlock.gif                        
string.Format(
"alert('计数器:{0}')", proxy.Counter()), 

InBlock.gif                        
true); 

InBlock.gif 

InBlock.gif                proxy.Close(); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
protected 
void btnPerSessionMode_Click(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif                
if (Session[
"proxy"] == 
null

InBlock.gif                        Session[
"proxy"] = 
new InstanceModeSvc.PerSessionMode.PerSessionModeClient(); 

InBlock.gif 

InBlock.gif                Page.ClientScript.RegisterStartupScript( 

InBlock.gif                        
this.GetType(), 

InBlock.gif                        
"js"

InBlock.gif                        
string.Format(
"alert('计数器:{0}')", (Session[
"proxy"
as InstanceModeSvc.PerSessionMode.PerSessionModeClient).Counter()), 

InBlock.gif                        
true); 

InBlock.gif        } 

InBlock.gif 

InBlock.gif        
protected 
void btnSingleMode_Click(
object sender, EventArgs e) 

InBlock.gif        { 

InBlock.gif                var proxy = 
new InstanceModeSvc.SingleMode.SingleModeClient(); 

InBlock.gif 

InBlock.gif                Page.ClientScript.RegisterStartupScript( 

InBlock.gif                        
this.GetType(), 

InBlock.gif                        
"js"

InBlock.gif                        
string.Format(
"alert('计数器:{0}')", proxy.Counter()), 

InBlock.gif                        
true); 

InBlock.gif 

InBlock.gif                proxy.Close(); 

InBlock.gif        } 

InBlock.gif}
 
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
 
     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344149
,如需转载请自行联系原作者
你可能感兴趣的文章
音视频入门之音频采集、编码、播放
查看>>
浅析badjs源码(前端监控方案)
查看>>
Runtime系列 2 -- 成员变量与属性
查看>>
javascript this小结
查看>>
2019面试题总结
查看>>
设计模式——策略模式
查看>>
iOS Runtime 初识与应用
查看>>
微信小程序BLE踩坑记录
查看>>
iOS 动画十五:Presentation Controller & Orientation Animations
查看>>
初学者怎么才能快速学会Python?
查看>>
浅谈async/await
查看>>
在 Windows 上搭建 React Native IOS 开发环境
查看>>
ViewPager的那些事
查看>>
android.support.v7.widget.SwitchCompat
查看>>
白话composer的简单使用
查看>>
SPI机制与策略模式
查看>>
使用 Docker 和 Traefik 搭建 GitLab (前篇)
查看>>
GMQ Group一家让投资更简单的数字资产交易所
查看>>
javascript 实现自定义继承
查看>>
python 比特币冷钱包升级完毕,内置exin 闪兑交易所,内置经典console 界面
查看>>