简体   繁体   English

如何在每个服务请求上运行代码?

[英]How to run code on every service request?

I need to run some code similar to an HTTP module every time a request comes into a WCF service. 每当请求进入WCF服务时,我都需要运行一些类似于HTTP模块的代码。 Some examples of the code might be authentication, throttling, logging, etc. What is the best way to go about executing a module each time a request is made? 代码的一些示例可能是身份验证,限制,日志记录等。每次发出请求时执行模块的最佳方法是什么?

Edit For Clarification 编辑澄清

We'll be doing a couple of things. 我们会做一些事情。 First of all, we need to authenticate all requests. 首先,我们需要验证所有请求。 Each request will require the user to pass some set of credentials, say an API key. 每个请求都要求用户传递一组凭据,例如API密钥。 We need validate the key is correct before allowing the request to go through. 在允许请求通过之前,我们需要验证密钥是否正确。

As for throttling, we'll need limit the number of requests a specific user can make. 至于节流,我们需要限制特定用户可以发出的请求数量。 Let's say 100 per hour or something similar. 假设每小时100或类似的水平。

There are several extension points in WCF that can be used for what you want. WCF中有几个扩展点可用于您想要的内容。 You will likely need to use different extension points for different purposes, though. 但是,您可能需要为不同目的使用不同的扩展点。

For example, for logging you could use IDispatchMessageInspector or IParameterInspector . 例如,对于日志记录,您可以使用IDispatchMessageInspectorIParameterInspector For Error handling you could use IErrorHandler . 对于错误处理,您可以使用IErrorHandler

Throttling is not something you typically use extensions for, as in many cases the built-in throttling settings might be enough. 节流不是您通常使用扩展程序的原因,因为在许多情况下,内置节流设置可能就足够了。

As for authentication, well, security has it's own set of extension points and there are several options, so perhaps you might want to clarify (or post a new question) with specifics so that we can recommend the appropriate mechanism. 至于身份验证,安全性有它自己的一组扩展点,有几个选项,所以也许你可能想澄清(或发布一个新问题)的具体细节,以便我们可以推荐适当的机制。

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

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