Prepending some text to a WCF response message bod

2019-08-17 15:26发布

问题:

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:

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


回答2:

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



标签: xml wcf xslt