tags:

views:

68

answers:

2

I want to prepend the following text to the response body of a WCF operation:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="transform.xslt" type="text/xsl" ?>

What is the best way to do this?

An additional requirement is that the XSLT filename should be spec'd using an attribute on the operation method.

I am trying to do this using a IDispatchMesssageInspector, but I do not know how to get access to the MethodInfo for the operation so that I can read the filename from the attribute.

+1  A: 

Here's how to get the current operation method:

var context = OperationContext.Current;
string action = context.IncomingMessageHeaders.Action;
var operation = context.EndpointDispatcher.DispatchRuntime.Operations
    .First(o => o.Action == action);
Type hostType = context.Host.Description.ServiceType;
MethodInfo method = hostType.GetMethod(operation.Name);
Darin Dimitrov
And the operation context will be available in IDispatchMessageInspector.BeforeSendReply?
Ries
This certainly got me closer, but I find that action == null. The only header present is the To header. This gives me the Uri that was called, now I just need a map of Uri to Operation...
Ries
Yes, at least it was available when I tested this code.
Darin Dimitrov
What binding do you use?
Darin Dimitrov
WebHttpBinding for REST
Ries
With WebHttpBehaviour2 from the REST tookit
Ries
A: 

In the end I had to use a custom MessageEncoder with its own MessageEncodingBindingElement.

Ries