WCF - 拦截消息并发送自定义响应

发布于 2025-01-08 11:05:28 字数 678 浏览 0 评论 0原文

我需要我创建的 WCF 服务能够进入“只读”模式。在此状态下,它不会服务对数据库进行更改的调用请求(例如创建订单),而只会服务只读请求(例如验证用户身份)。如果它收到一个不会提供服务的请求,它需要传回一条友好的消息和代码(例如代码:100,描述:“服务处于只读模式,无法服务此请求”)。

我有近 100 个不同的调用,并且希望实现一个可以在一个地方完成的解决方案(如 IDispatchMessageInspector),但我没有找到一种方法来为任何拦截的 WCF 消息提供响应。我有一个标准响应类型,我会在所有请求上发回该响应类型,并且也希望在这些情况下发回该响应类型。该类型名为 ServiceResponse 并且具有消息集合。这是一类代码和描述。

public class ServiceResponse
{
    public List<Message> Messages {get; set;}
}

public class Message
{
    public string Code {get; set;}
    public string Description {get; set;}
}

有人可以为我提供如何执行此操作的指导吗?我已经用尽了所有搜索选项。

即使 WCF 中只有一种方法可以手动创建我要发回的 SOAP,只要我不必在每个服务调用中处理只读检查/响应,就可以了。

谢谢!

I have a requirement for a WCF service I've created to be able to go into "read only" mode. While in this state it will not service requests for calls that would make changes to the database (like creating an order) but will only service read-only requests (like authenticate user). If it receives a request that it will not service, it needs to pass back a friendly message and code (e.g. CODE: 100, DESCRIPTION: "Service is in read only mode and cannot service this request").

I have close to 100 different calls and would like to implement a solution that can be made in one place (like an IDispatchMessageInspector) but I don't see a way to provide a response to any intercepted WCF messages. I have a standard response type that I send back on ALL requests and would like to send that back in these situations as well. The type is named ServiceResponse and has a messages collection. which is a class of Codes and Descriptions.

public class ServiceResponse
{
    public List<Message> Messages {get; set;}
}

public class Message
{
    public string Code {get; set;}
    public string Description {get; set;}
}

Can someone provide me with a direction on how to do this? I have exhausted all of my searching options.

Even if there is just a way in WCF to hand create the SOAP I'm sending back, that's fine as long as I don't have to handle the read-only check / response in every single service call.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2025-01-15 11:05:28

我看到有几个选择。 1) 将功能添加到服务实现中(其中 100 个),2) 找到一个面向方面的工具来在运行时拦截服务调用并执行不同的代码,或者 3) 使用自定义 HTTPHandler 拦截所有 wcf 请求。在处理程序中,您可以处理请求并向客户端返回响应。以下是一些可能有帮助的链接

http://blogs.msdn.com/b/wenlong/archive/2007/09/18/how-to-use-asmx-extension-to-handle-wcf-requests.aspx

用于挂钩 *.svc 请求的 HttpHandler

I see a couple of choices. 1) add the functionality into the service implementations (100 of them), 2) find an aspect-oriented tool to intercept the service invocation at runtime and execute different code, or 3) intercept all the wcf requests using a custom HTTPHandler. In the handler you can process the request and return a response to the client. Here are some links which might help

http://blogs.msdn.com/b/wenlong/archive/2007/09/18/how-to-use-asmx-extension-to-handle-wcf-requests.aspx

HttpHandler to hook the *.svc requests

夜雨飘雪 2025-01-15 11:05:28

您可以使用自定义 ServiceAuthorizationManager 来实现此目的。

这个想法是

  • 向每个要在只读模式下禁用的类添加一个自定义属性。
  • 在自定义 ServiceAuthorizationManager 中,检查服务是否处于只读模式。如果是,请检查您尝试调用的方法上的自定义属性。该方法由请求消息操作标头指示。服务的类型可以从 OperationContext.InstanceContext 中找到(我想 - 我记不清到底是什么属性)
  • 如果找到该属性,请抛出带有正确错误消息的错误异常。

You could achieve this with a custom ServiceAuthorizationManager.

The idea would be

  • Add a custom attribute to each class that you want to disable when in read only mode
  • In the custom ServiceAuthorizationManager, check to see if the service is in read only mode. If it is, check for the custom attribute on the method you are trying to call. The method is indicated by the request message action header. The type of the service can be found from the OperationContext.InstanceContext (I think - I can't remember exactly what property)
  • If you find the attribute, throw a fault exception with the right error message.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文