Prepending some text to a WCF response message bod

2019-08-17 15:29发布

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.

标签: xml wcf xslt
2条回答
贪生不怕死
2楼-- · 2019-08-17 15:42

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

查看更多
放我归山
3楼-- · 2019-08-17 16:00

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);
查看更多
登录 后发表回答