在 WCF REST 4.0 中使用 StandardEndpoints 时如何配置 MessageInspector
我正在尝试创建和配置消息检查器来执行 WCF Rest HTTP 请求的某些身份验证。我正在使用 4.0,因此试图避开 WCF Starter Kit,尽管我已经设法让旧的 RequestInterceptor 按我想要的方式工作。使用RequestInterceptor的问题是我丢失了WebHttpBehavior提供的automaticFormatSelectionEnabled功能,我真的很想保留它。
所以我的问题是如何以仍然使用 WebHttpBehavior 并保留其功能的方式配置消息检查器。
我的 web.config 看起来像这样
<standardEndpoints>
<webHttpEndpoint>
<!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
<!-- Disable the help page for the directory end point-->
<standardEndpoint name="DirectoryEndpoint"/>
</webHttpEndpoint>
</standardEndpoints>
I'm trying create and configure a Message Inspector to perform some authentication of a WCF Rest HTTP request. I'm using 4.0 so trying to steer clear of the WCF Starter Kit although I have managed to get an old RequestInterceptor working in the way I want. The problem with using RequestInterceptor is that I lost the automaticFormatSelectionEnabled features provided by WebHttpBehavior which I really want to keep.
So my question is how do I configure the Message Inspector in a way that I still use the WebHttpBehavior and keep it's features.
My web.config looks like this
<standardEndpoints>
<webHttpEndpoint>
<!-- the "" standard endpoint is used by WebServiceHost for auto creating a web endpoint. -->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
<!-- Disable the help page for the directory end point-->
<standardEndpoint name="DirectoryEndpoint"/>
</webHttpEndpoint>
</standardEndpoints>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理这个问题的一种方法是创建三个对象。
request/response
pipeline
web.config
首先通过实现 IDispatchMessageInspector 创建消息检查器
并将验证代码放入 AfterReceiveRequest 方法中:
在读取消息时创建消息的缓冲副本非常重要。消息只能打开一次,不创建副本将导致管道出现问题。如果失败,我的 ValidateHmac 实现将引发异常。这会阻止调用实际的服务。
其次,为检查员创建一种行为。我们将使用该行为将检查器注入到 WCF 运行时中。要创建行为,请从 IEndpointBehavior 派生一个类,使其看起来像这样
注意:我创建了检查器 (
HmacVerificationInspector
) 的新实例,并以编程方式将其注入运行时。最后一步是创建配置部分。我们可以使用它来应用 Web 配置中的行为(从而能够通过配置打开和关闭它)。创建一个新类并继承BehaviorExtensionElement和IServiceBehavior:
现在,要使用检查器,请将以下内容添加到您的web.config中(您可以将扩展名设置为您想要的任何名称)
有几件事,首先您注册配置部分在行为扩展中。接下来,您使用该配置作为端点行为,然后该行为将自动注入检查器,并且对该端点的所有请求都将通过您的检查器运行。如果您想关闭检查器,请删除标签或选择不同的端点行为。还要注意 webHttp 行为的使用(这将允许您保持automaticFormatSelectionEnabled。
希望这有帮助
One way you can handle this is to create three objects.
request/response
pipeline
web.config
First create the message inspector by implementing IDispatchMessageInspector
and putting your validation code in the AfterReceiveRequest method:
It's important to create a buffered copy of the message when reading it. Messages can only be opened once and not creating a copy will lead to problems down the pipe. My implementation of ValidateHmac throws an exception if it fails. This prevents the actual service from being called.
Second, create a behavior for your inspector. We'll use the behavior to inject the inspector into the WCF runtime. To create the behavior, derive a class from IEndpointBehavior so it looks like this
Notice I create a new instance of my inspector (
HmacVerificationInspector
) and inject it programmatically into the runtime.Finally, the last step is to create a configuration section. We can use this to apply the behavior in the web config (thus being able to turn it on and off via configuration). Create a new class and inherit from BehaviorExtensionElement and IServiceBehavior:
Now, to use the inspector, add the following to your web.config (you can set the name for your extension to whatever you want)
Couple of things, first you register the configuration section in the behavior extensions. Next, you use that configuration as an endpoint behavior which will then automatically inject the inspector and all requests to that endpoint will run through your inspector. If you want to turn off the inspector, remove the tag or select a different endpoint behavior. Note the use of the webHttp behavior also (which will allow you to keep automaticFormatSelectionEnabled.
Hope this helps