为什么InstanceContextMode.PerSession表现得像PerCall使用的wsHttpBinding什么时候? [英] Why InstanceContextMode.PerSession behave like PerCall when using wsHttpBinding?

查看:353
本文介绍了为什么InstanceContextMode.PerSession表现得像PerCall使用的wsHttpBinding什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WCF服务消费使用SOAP 1.2 AJAX客户端

I have WCF service consumed by AJAX client using SOAP 1.2

Web.config文件:

<endpoint address="" binding="wsHttpBinding" 
contract="WcfService1.IService1" bindingConfiguration="wsHttpBin">

<wsHttpBinding>
  <binding name="wsHttpBin">
    <security mode="None"/>          
  </binding>
</wsHttpBinding>

这是我有什么阅读,我必须使用&LT;安全模式=无/&GT; ,因为暴露了的wsHttpBinding结合的WS-实现了WS-安全服务*家族的web服务规范。作为绑定使用的安全性,该请求将自AJAX的被拒绝不支持安全上下文

From what I have read, I have to use <security mode="None"/> since a service exposed with "wsHttpBinding" binding implements WS-Security of WS-* family of web service specifications. As the binding uses security, the request will be rejected since AJAX doesn't support the security context.

我的WCF服务行为定义的 InstanceContextMode.PerSession

My WCF service behavior is defined with InstanceContextMode.PerSession:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, 
                 InstanceContextMode = InstanceContextMode.PerSession)]

但是当我使用它的服务表现得像PerCall和每一个电话开始,而不是使用当前实例的新WCF实例。

but when I consume it, the service behave as PerCall and every call starts a new WCF instance instead of using the current instance.

为什么InstanceContextMode.PerSession表现得像PerCall使用的wsHttpBinding什么时候?

Why InstanceContextMode.PerSession behave like PerCall when using wsHttpBinding?

我该怎么办?

推荐答案

会话,在HTTP上使用时,使用安全的会话或可靠的会话时,仅受WCF。如果不能使用,那么你必须自己实现一个会话机制。如果控制在客户端和服务器端,这将是很容易做到这一点。具体方法如下:

Sessions, when used over HTTP, are only supported by WCF when using security sessions or reliable sessions. If you can't use either then you have to implement a session mechanism by yourself. If you control both the client and the server side, it would be quite easy to do it. Here's how:

创建一个包含所有你需要存储在会话数据的类(姑且称之为 SessionData ),外加的DateTime 的时候,会是最后一次使用。然后添加到您的服务类(或任何其他类),一个静态 ConcurrentDictionary&LT;字符串,SessionData&GT;

Create a class that holds all the session data you need stored (let's call it SessionData), plus an additional DateTime for when the session was last used. Then add to your service class (or any other class) a static ConcurrentDictionary<string, SessionData>.

当客户端调用服务,要求其通过标识会话(它可以在客户端随机生成)的唯一字符串。每当客户端调用你的服务,查找届字符串中的字典和检索会话数据(并根据需要更新其内容)。如果它不存在,则创建在字典中的新条目。此外,每次访问 SessionData 对象,更新上次使用的DateTime 为当前时间。后台任务应该定期清除掉已经没有了,而使用的旧的会话。

When a client makes a call to your service, require it to pass a unique string that identifies the session (it can be randomly generated on the client side). Whenever a client calls you service, look up the session string in the dictionary and retrieve the session data (and update its content as needed). If it doesn't exist, create a new entry in the dictionary. Also, every time you access the SessionData object, update the 'last used' DateTime to the current time. A background task should periodically clear out old sessions that haven't been used in a while.

这就是它 - 你已经实现了你自己的会话。现在,您可以使用 InstanceContextMode.Single ,而不是担心WCF正确创建每个会话服务类的实例。

That's it - you've implemented sessions on your own. You can now useInstanceContextMode.Single and not worry about WCF correctly creating instances of your service class per session.

修改:如果你写你的WCF服务,.NET 4.5和你的web应用程序只针对现代浏览器,你可以使用 NetHttpBinding 在服务器侧和WebSocket的在客户端。 NetHttpBinding 支持会话(指定 SessionMode.Required 时)。

EDIT: If you're writing your WCF service with .NET 4.5 and you web application only targets modern browsers, you can use NetHttpBinding on the server side and WebSocket on the client side. NetHttpBinding supports session (when specifying SessionMode.Required).

这篇关于为什么InstanceContextMode.PerSession表现得像PerCall使用的wsHttpBinding什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆