简体   繁体   English

在.net Web服务中全局启用会话

[英]Enable Session globally in .net webservice

Is there a way to enable the session globally (ie in the web.config) rather than on each method in a web service? 有没有一种方法可以全局(而不是在web.config中)启用会话,而不是在Web服务中的每种方法上启用会话?

We have a huge (wcf) webservice with many many methods and I would rather not have to go in and add the attribute to every method..... 我们拥有一个庞大的(wcf)网络服务,其中包含许多方法,我宁愿不必将属性添加到每个方法中.....

A quick search on google will tell you that you need to specify attributes on ServiceContract and not at method level. 在Google上进行快速搜索将告诉您,您需要在ServiceContract而不是方法级别上指定属性。

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples", SessionMode=SessionMode.Required)]
public interface ICalculatorSession

Moreover WCF sessions are represented as service instances (as CLR objects) and the states are part of each service instance. 此外,WCF会话表示为服务实例(作为CLR对象),并且状态是每个服务实例的一部分。 The sessions are initiated by the calling WCF client. 会话由调用WCF客户端启动。 WCF relies on special context to provide correlation for the sessions: sessionful channels, secure conversations, reliable-messaging, etc. WCF依靠特殊的上下文为会话提供相关性:会话通道,安全对话,可靠消息等。

WCF sessions are not same as ASP.NET Session. WCF会话与ASP.NET会话不同。 To use ASP.NET session you would need to enable ASP.NET compatibility mode on the WCF service. 要使用ASP.NET会话,您需要在WCF服务上启用ASP.NET兼容模式。

[AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Required)]
    public class CalculatorService : ICalculatorSession

Useful links: 有用的链接:

Using Sessions 使用会议

ASP.NET Compatibility ASP.NET兼容性

Ok, I found a better way. 好的,我找到了更好的方法。 I am now using MemoryCache which is part of System.Runtime.Caching 我现在正在使用MemoryCache,它是System.Runtime.Caching的一部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM