简体   繁体   English

每个实例在WCF应用程序中保留一个变量

[英]Persist a variable in WCF application per instance

I am creating a WFC Restful service and there is a need to persist a variable that will be persist per user, is there a way I can achieve this without having to pass the variable to all my calls? 我正在创建WFC Restful服务,并且需要持久保存一个将对每个用户持久保存的变量,是否有一种方法可以实现而无需将该变量传递给我的所有调用?

I am using trying to log the process of the user throughout the process, weather their request has failed or succeed, IP address, when they requested the action, failure time, etc. Please note I am new to WCF, thanks in advance. 我正在尝试在整个过程中记录用户的过程,天气,他们的请求失败或成功,IP地址,他们请求操作的时间,失败时间等。请注意,我是WCF的新手,谢谢。

I recently worked on this (except it wasn't RESTFUL). 我最近对此进行了研究(除非它不是RESTFUL)。 You could transmit information through HTTP headers and extract that information on the service-side. 您可以通过HTTP标头传输信息,并在服务端提取该信息。 See http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/ 参见http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/

For the client ID itself I can suggest two places to put it. 对于客户端ID本身,我可以建议放置两个位置。 One is OperationContext.Current.IncomingMessageProperties . 一种是OperationContext.Current.IncomingMessageProperties Another is CorrelationManager.StartLogicalOperation which allows you to define a logical operation - that could be the service request, beginning to end - or multiple operations - and retrieve a unique ID for each operation. 另一个是CorrelationManager.StartLogicalOperation ,它允许您定义一个逻辑操作-可能是服务请求,从头到尾-或多个操作-并为每个操作检索唯一的ID。

I would lean toward the latter because it's part of System.Diagnostics and can prevent dependencies on System.ServiceModel . 我倾向于后者,因为它是System.Diagnostics的一部分,可以防止对System.ServiceModel依赖。 (The name CorrelationManager even describes what you're trying to do.) (名称CorrelationManager甚至描述了您要执行的操作。)

In either case I would look at interception. 无论哪种情况,我都会看一下拦截。 That's the ideal way to read the value (wherever you store it) without having to pollute the individual methods with knowledge of logging and client IDs. 这是读取值(无论在何处存储值)的理想方法,而不必知道日志和客户端ID便会污染单个方法。 (I saw from your message that you're trying to avoid that direct dependency on client IDs.) (我从您的消息中看到,您正在尝试避免直接依赖于客户端ID。)

Here's some documentation on adding Windsor to your WCF service. 以下是有关将Windsor添加到WCF服务的一些文档 (At some point I'll add some end-to-end documentation on my blog.) Then, when you're using Windsor to instantiate your services, you can also use it to instantiate the dependencies and put interceptors around them that will perform your logging before or after those dependencies do their work. (有时,我会在博客上添加一些端到端文档。)然后,当您使用Windsor实例化服务时,还可以使用它实例化依赖项并将拦截器放置在将要执行的依赖项周围这些依赖项执行工作之前或之后的日志记录。 Within those interceptors you can access or modify that stack of logical operations. 在这些拦截器中,您可以访问或修改该逻辑操作堆栈。

I'm not doing Windsor justice by throwing out a few links. 我不是通过抛出一些链接来使温莎公义。 I'd like to flesh it out with some blog posts. 我想补充一些博客文章。 But I recommend looking into it. 但我建议您调查一下。 It's beneficial for lots of reasons - interception just one. 出于多种原因,这是有益的-拦截只是其中之一。 It helps with the way we compose services and dependencies. 它有助于我们组合服务和依赖项的方式。

Update - I added a blog post - how to add Windsor to a WCF service in five minutes. 更新-我添加了一篇博客文章 -如何在五分钟内将Windsor添加到WCF服务。

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

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