您的位置:首页 > 其它

化零为整WCF(10) - 实例模型(InstanceContextMode)

2008-05-08 18:45 393 查看
[索引页]

[源码下载]

[align=center]化零为整WCF(10) - 实例模型(InstanceContextMode)[/align]

作者: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

PerSessionMode.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.InstanceMode

SingleMode.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.InstanceMode

2、宿主

PerCallMode.svc

<?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

<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

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

[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: