omatase
omatase

Reputation: 1711

WCF - Intercepting a Message and Sending a Custom Response

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!

Upvotes: 3

Views: 1744

Answers (2)

Mike Goodwin
Mike Goodwin

Reputation: 8880

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.

Upvotes: 1

Jennifer Zouak
Jennifer Zouak

Reputation: 1348

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

Upvotes: 1

Related Questions