Web Service是否可以配置类似页面的EnableSessionState

ycg_893 2012-01-17 02:19:28
配置时

[WebMethod(EnableSession = true)] //启用会话
public string UserList1()
{
System.Threading.Thread.Sleep(1000 * 60 * 5); //模似计算5分钟
return "abc1";
}

[WebMethod(EnableSession = true)] //启用会话
public string UserList2()
{
return "abc2";
}

在我的程序中只有登陆与退出才会对会话进行写,其他时候都是只读(主要是权限),
因此在页面 EnableSessionState="ReadOnly" 这样配置,而如果是实现IHttpHandler接口的对象则再加这个
System.Web.SessionState.IReadOnlySessionState接口,都能很好的解决问题.

但在WEB服务方面,就不知道如何解决了

同一会话(不同实例,不同模块,异步等)调用以上两个方法
先调用1再调用2,2会被阻塞到1完成才返回,但放在页面上就没有问题(只要配置EnableSessionState).

解决过的朋友帮助一下!
...全文
202 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycg_893 2012-01-17
  • 打赏
  • 举报
回复
自己顶一下
ycg_893 2012-01-17
  • 打赏
  • 举报
回复
难道除了禁用会话外,没有什么其他办法了吗?
宝_爸 2012-01-17
  • 打赏
  • 举报
回复
好像web service是不行的:

State Management

Web services state can be specific to a user or to an application. Web services use ASP.NET session state to manage per-user data and application state to manage application-wide data. You access session state from a Web service in the same way you do from an ASP.NET application — by using the Session object or System.Web.HttpContext.Current. You access application state using the Application object, and the System.Web.HttpApplicationState class provides the functionality.

Maintaining session state has an impact on concurrency. If you keep data in session state, Web services calls made by one client are serialized by the ASP.NET runtime. Two concurrent requests from the same client are queued up on the same thread in the Web service — the second request waits until the first request is processed. If you do not use session data in a Web method, you should disable sessions for that method.

Maintaining state also affects scalability. First, keeping per-client state in-process or in a state service consumes memory and limits the number of clients your Web service can serve. Second, maintaining in-process state limits your options because in-process state is not shared by servers in a Web farm.

If your Web service needs to maintain state between client requests, you need to choose a design strategy that offers optimum performance and at the same time does not adversely affect the ability of your Web service to scale. The following guidelines help you to ensure efficient state management:

* Use session state only where it is needed.
* Avoid server affinity.

Use Session State Only Where It Is Needed

To maintain state between requests, you can use session state in your Web services by setting the EnableSession property of the WebMethod attribute to true, as shown in the following code snippet. By default, session state is disabled.

[WebMethod(EnableSession=true)]
YourWebMethod() { ... }
Since you can enable session state at the Web method level, apply this attribute only to those Web methods that need it.

Note Enabling session state pins each session to one thread (to protect session data). Concurrent calls from the same session are serialized once they reach the server, so they have to wait for each other, regardless of the number of CPUs.
Avoid Server Affinity

If you do use session state, in-process session state offers the best performance, but it prevents you from scaling out your solution and operating your Web services in a Web farm. If you need to scale out your Web services, use a remote session state store that can be accessed by all Web servers in the farm.

以上来自msdn:
http://msdn.microsoft.com/en-us/library/ff647786.aspx
宝_爸 2012-01-17
  • 打赏
  • 举报
回复
这是wcf的设置

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class MultipleCachingHttpFetcher : IHttpFetcher
宝_爸 2012-01-17
  • 打赏
  • 举报
回复
好像EnableSessionState只能用到Page上。

我也不知道web service能不能设置,但是wcf是可以设置Concurrency的
ycg_893 2012-01-17
  • 打赏
  • 举报
回复
感谢findcaiyzh 一早为我答题,看来只能换一种思路了.
Johnyin 2012-01-17
  • 打赏
  • 举报
回复
WEB Service的方法也可以如上进行配置,启用Session
Johnyin 2012-01-17
  • 打赏
  • 举报
回复
[WebMethod(EnableSession = true)]
public object[] MethodInvoke(object[] objArr)
宝_爸 2012-01-17
  • 打赏
  • 举报
回复
我三楼的文章转自msdn,不是说了吗,用了Session就没法concurrency了。

看这段,忘加颜色了。

Maintaining session state has an impact on concurrency. If you keep data in session state, Web services calls made by one client are serialized by the ASP.NET runtime. Two concurrent requests from the same client are queued up on the same thread in the Web service — the second request waits until the first request is processed. If you do not use session data in a Web method, you should disable sessions for that method.

可以使用WCF.
ycg_893 2012-01-17
  • 打赏
  • 举报
回复
去掉会话,改动太大(因为对象都传入用户,而用户而是保存在会话)
段传涛 2012-01-17
  • 打赏
  • 举报
回复
好像不行,没有用过。
我测试一下。

62,077

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧