如何在每个服务请求上运行代码? [英] How to run code on every service request?

查看:38
本文介绍了如何在每个服务请求上运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次请求进入 WCF 服务时,我都需要运行一些类似于 HTTP 模块的代码.代码的一些示例可能是身份验证、限制、日志记录等.每次发出请求时执行模块的最佳方法是什么?

I need to run some code similar to an HTTP module every time a request comes into a WCF service. 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?

编辑以澄清

我们将做一些事情.首先,我们需要对所有请求进行身份验证.每个请求都需要用户传递一些凭据,比如 API 密钥.在允许请求通过之前,我们需要验证密钥是否正确.

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. We need validate the key is correct before allowing the request to go through.

至于限制,我们需要限制特定用户可以发出的请求数量.假设每小时 100 次或类似情况.

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.

推荐答案

WCF 中有几个扩展点可用于您想要的.不过,您可能需要为不同的目的使用不同的扩展点.

There are several extension points in WCF that can be used for what you want. You will likely need to use different extension points for different purposes, though.

例如,对于日志记录,您可以使用 IDispatchMessageInspectorIParameterInspector.对于错误处理,您可以使用 IErrorHandler.

For example, for logging you could use IDispatchMessageInspector or IParameterInspector. For Error handling you could use 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.

这篇关于如何在每个服务请求上运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆