Ries
Ries

Reputation: 2866

Prepending some text to a WCF response message body

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.

Upvotes: 0

Views: 383

Answers (2)

Ries
Ries

Reputation: 2866

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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);

Upvotes: 2

Related Questions