WCF 中 使用 SessionWord文档下载推荐.docx

上传人:b****5 文档编号:16634691 上传时间:2022-11-25 格式:DOCX 页数:14 大小:19.25KB
下载 相关 举报
WCF 中 使用 SessionWord文档下载推荐.docx_第1页
第1页 / 共14页
WCF 中 使用 SessionWord文档下载推荐.docx_第2页
第2页 / 共14页
WCF 中 使用 SessionWord文档下载推荐.docx_第3页
第3页 / 共14页
WCF 中 使用 SessionWord文档下载推荐.docx_第4页
第4页 / 共14页
WCF 中 使用 SessionWord文档下载推荐.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

WCF 中 使用 SessionWord文档下载推荐.docx

《WCF 中 使用 SessionWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《WCF 中 使用 SessionWord文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。

WCF 中 使用 SessionWord文档下载推荐.docx

!

--WCFSiverlight配置-->

behaviors>

serviceBehaviors>

behaviorname="

.Service.ServiceBehavior"

serviceMetadatahttpGetEnabled="

/>

serviceDebugincludeExceptionDetailInFaults="

false"

/behavior>

/serviceBehaviors>

/behaviors>

bindings>

customBinding>

bindingname="

customBinding0"

binaryMessageEncoding/>

httpTransport>

extendedProtectionPolicypolicyEnforcement="

Never"

/httpTransport>

/binding>

/customBinding>

/bindings>

--这里配置-->

services>

servicebehaviorConfiguration="

name="

.Service.Service"

endpointaddress="

"

binding="

customBinding"

bindingConfiguration="

contract="

mex"

mexHttpBinding"

IMetadataExchange"

/service>

/services>

/system.serviceModel>

网络资源:

WCF状态保存分为两步:

(1)使用SessionMode来使Session有效化

[ServiceContract(SessionMode

SessionMode=SessionMode.Required)]

publicinterfaceICalculator

{

[OperationContract(IsOneWay=true)]

voidAdds(doublex);

[OperationContract]

doubleGetResult();

}

(2)ServiceBehavior里面利用参数InstanceContextMode设定到底使用哪一种Session方式

[ServiceBehavior(InstanceContextMode

InstanceContextMode=InstanceContextMode.PerCall)]

publicclassCalculatorService:

ICalculator

{}

WCF状态保存SessionMode有三种值:

(1)Allowed默认选值,允许但不强制使用Session

(2)Required强制使用Session

(3)NotAllowed不允许使用Session

WCF状态保存InstanceContextMode有三种值:

(1)Percall为user的每一次调用生成一个SessionID

生命周期:

调用开始---->

调用结束,这种情况和不使用Session是一样的

(2)PerSession为每个用户生成一个SessionID

客户端代理生成--->

客户端代理关闭和最原先的Session是一样的

(3)Seigle生成唯一的SessionID,所有的用户共享从host创建---->

host关闭,和Application一样

在Silverlight中使用SESSION

首先Session是运行在服务器上的,而Silverlight运行在客户端。

因此在Silverlight中使用SESSION的说法并不准确,

只因大家经常这样搜索才起这个名字。

有两种方法实现Silverlight与Session的关联:

方法一、通过WCF使用ASP.NET中的Session[因BasicHttpBinding不支持WCF中的Session,如使用WCF会话将报错]

  首先:

在web.config中<

system.serviceModel>

下添加:

    <

/>

  然后:

在服务类[不是接口]下添加如下属性:

    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]

  接下来就可以使用Session记得添加System.Web的引用

    HttpContext.Current.Session["

YourName"

]=something;

    objectsomething=HttpContext.Current.Session["

];

方法二、在客户端新建一个静态类模拟Session

  如要保存登陆信息,可在验证了用户名、密码之后在客户端保存相关信息。

namespaceSessionDemo

publicstaticclassSessionManager

privatestaticDictionary<

string,object>

session=newDictionary<

();

publicstaticDictionary<

Session

get{returnSessionManager.session;

set{SessionManager.session=value;

使用方法:

赋值:

SessionManager.Session["

uname"

]="

kunal"

;

取值:

txbUname.Text=SessionManager.Session["

].ToString();

介绍

WCF(WindowsCommunicationFoundation)-会话状态:

ServiceContract

·

SessionMode.Allowed-指定当传入绑定支持会话时,协定也支持会话(默认值)

SessionMode.Required-指定协定需要会话绑定。

如果绑定并未配置为支持会话,则将引发异常

SessionMode.NotAllowed-指定协定永不支持启动会话的绑定

OperationContract

IsInitiating-获取或设置一个值,该值指示方法是否实现可在服务器上启动会话(如果存在会话)的操作。

IsTerminating-获取或设置一个值,该值指示服务操作在发送答复消息(如果存在)后,是否会导致服务器关闭会话。

示例

1、服务

IHello.cs

namespaceWCF.ServiceLib.SessionManagement

/**////<

summary>

///演示会话状态的接口

///<

/summary>

NotAllowed

remarks>

///SessionMode-获取或设置是否允许、不允许或要求会话

///SessionMode.Allowed-指定当传入绑定支持会话时,协定也支持会话(默认值)

///SessionMode.Required-指定协定需要会话绑定。

///SessionMode.NotAllowed-指定协定永不支持启动会话的绑定

/remarks>

publicinterfaceIHello

///初始化Session

///IsInitiating-获取或设置一个值,该值指示方法是否实现可在服务器上启动会话(如果存在会话)的操作。

///IsTerminating-获取或设置一个值,该值指示服务操作在发送答复消息(如果存在)后,是否会导致服务器关闭会话。

[OperationContract(IsInitiating=true,IsTerminating=false)]

voidStartSession();

///结束Session

[OperationContract(IsInitiating=false,IsTerminating=true)]

voidStopSession();

///获取计数器结果

returns>

/returns>

[OperationContract(IsInitiating=false,IsTerminating=false)]

intCounter();

///获取SessionId

stringGetSessionId();

Hello.cs

///InstanceContextMode-获取或设置指示新服务对象何时创建的值。

///InstanceContextMode.PerSession-为每个会话创建一个新的System.ServiceModel.InstanceContext对象。

///InstanceContextMode的默认值为InstanceContextMode.PerSession

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]

publicclassHello:

IHello

privateint_counter;

publicvoidStartSession()

_counter=0;

publicvoidStopSession()

publicintCounter()

_counter++;

return_counter;

publicstringGetSessionId()

returnOperationContext.Current.SessionId;

2、宿主

Hello.svc

%@ServiceHostLanguage="

C#"

Debug="

Service="

WCF.ServiceLib.SessionManagement.Hello"

%>

Web.config

?

xmlversion="

1.0"

configuration>

SessionManagementBehavior"

--httpGetEnabled-使用get方式提供服务-->

--name-提供服务的类名-->

--behaviorConfiguration-指定相关的行为配置-->

servicename="

behaviorConfiguration="

--address-服务地址-->

--binding-通信方式-->

--contract-服务契约-->

--bindingConfiguration-指定相关的绑定配置-->

wsHttpBinding"

WCF.ServiceLib.SessionManagement.IHello"

SessionManagementBindingConfiguration"

wsHttpBinding>

--wsHttpBinding可提供安全会话和可靠会话-->

--receiveTimeout-在传输引发异常之前可用于完成读取操作的时间间隔(此处可认为是Session过期时间)-->

receiveTimeout="

00:

10"

--指示是否在通道终结点之间建立WS-RM(WS-ReliableMessaging)可靠会话。

默认值为false。

-->

reliableSessionenabled="

security>

--此属性控制安全上下文令牌是否通过客户端与服务之间的WS-SecureConversation交换建立。

将它设置为true要求远程方支持WS-SecureConversation。

messageestablishSecurityContext="

/security>

/wsHttpBinding>

/configuration>

3、客户端

Hello.aspx

%@PageLanguage="

MasterPageFile="

~/Site.master"

AutoEventWireup="

CodeFile="

Hello.aspx.cs"

Inherits="

InstanceMode_Hello"

Title="

会话状态(Session)"

asp:

ContentID="

Content1"

ContentPlaceHolderID="

head"

runat="

Server"

/asp:

Content>

Content2"

ContentPlaceHolder1"

ButtonID="

btnStartSession"

server"

Text="

StartSession"

OnClick="

btnStartSession_Click"

btnCounter"

Counter"

btnCounter_Click"

btnGetSessionId"

GetSessionId"

btnGetSessionId_Click"

btnStopSession"

StopSession"

btnStopSession_Click"

Hello.aspx.cs

usingSystem.Collections;

usingSystem.Configuration;

usingSystem.Data;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.HtmlControls;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Xml.Linq;

publicpartialclassInstanceMode_Hello:

System.Web.UI.Page

SessionManagemenSvc.HelloClient_proxy=null;

protectedvoidPage_Load(objectsender,EventArgse)

if(Session["

proxy"

]==null)

Session["

]=newSessionManagemenSvc.HelloClient();

_proxy=Session["

]asSessionManagemenSvc.HelloClient;

protectedvoidbtnStartSession_Click(objectsender,EventArgse)

_proxy.StartSession();

protectedvoidbtnCounter_Click(objectsender,EventArgse)

Page.ClientScript.RegisterStartupScript(

this.GetType(),

"

js"

string.Forma

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 哲学历史

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1