积极答复者
请教一个问题,wcf中的OperationContext.Current是怎么实现的呢? 可能是一个OOP的问题

问题
-
在wcf中,自己写的services方法里可以通过调用OperationContext这个类的静态属性Current来获得当前执行的上下文信息
这明明是一个静态属性,可是在不同的类实例中却有不同的表现,也就是一种多态.
请问这是怎么设计的呢?有没有高手能讲解一下??- 已移动 Sheng Jiang 蒋晟Moderator 2010年11月10日 17:46 (发件人:.NET Framework 一般性问题讨论区)
答案
-
我把你的问题发到WCF英文论坛了,Steven 给了回复。我看有点意思。
Hi Frank,
I think providing a Current static member is to simplify the access of the OperationContext associated with the current operation processing pipeline/context. Just like the HttpContext.Current property used in ASP.NET web application, through this property is static, the actually implementation code of this property will look into the context storage(such as the TLS of current thread) to acquire the correct OperationContext/HttpContext associated with the current thread/context. Therefore, such kind of "static" member is still thread safe when being called in different threads. For example, if you call this property in a background thread where the OperationContext or HttpContext is not associated, this property will return null.
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
- 已标记为答案 Mog Liang 2010年11月17日 6:59
全部回复
-
不可能是单例吧
怎么会是单例呢
有多个客户端连我的服务,就肯定会产生多个上下文实例,为啥直接用operationcontext.current就能直接获取了呢?照oop和c#语言的定义,current是一个静态属性,不应该出现这种情况啊
你好像在很多地方问道。我对这个问题也很好奇。希望能得到WCF设计人员的回答。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
我把你的问题发到WCF英文论坛了,Steven 给了回复。我看有点意思。
Hi Frank,
I think providing a Current static member is to simplify the access of the OperationContext associated with the current operation processing pipeline/context. Just like the HttpContext.Current property used in ASP.NET web application, through this property is static, the actually implementation code of this property will look into the context storage(such as the TLS of current thread) to acquire the correct OperationContext/HttpContext associated with the current thread/context. Therefore, such kind of "static" member is still thread safe when being called in different threads. For example, if you call this property in a background thread where the OperationContext or HttpContext is not associated, this property will return null.
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
- 已标记为答案 Mog Liang 2010年11月17日 6:59