How do i get the invoked operation name within a WCF Message Inspector
Asked Answered
T

5

18

I'm doing a message inspector in WCF:

public class LogMessageInspector :
    IDispatchMessageInspector, IClientMessageInspector

which implements the method:

public object AfterReceiveRequest(ref Message request,
    IClientChannel channel, InstanceContext instanceContext)

I can get the name of the invoked service with:

instanceContext.GetServiceInstance().GetType().Name

But how do I get the name of the invoked operation?

Tbar answered 19/3, 2010 at 12:19 Comment(0)
L
12

It's not pretty, but this is what I did to get the operation name:

var action = OperationContext.Current.IncomingMessageHeaders.Action;
var operationName = action.Substring(action.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
Laconism answered 19/3, 2010 at 12:30 Comment(1)
Operation.Current is null in my case, it works if you retrieve the action through the Message object from the parameter as: System.ServiceModel.Channels.Message request ... request.Headers.Action.Aluminiferous
B
8
var operationName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
Bricabrac answered 1/3, 2011 at 3:31 Comment(1)
Throws an ArgumentException for meLearned
P
4

This approach is similar to others presented here, but uses Path.GetFileName:

Path.GetFileName(OperationContext.Current.IncomingMessageHeaders.Action);

The return value of this method and the format of the path string work quite harmoniously in this scenario:

The characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns String.Empty. If path is null, this method returns null.

Pearlstein answered 9/10, 2014 at 20:12 Comment(3)
Came up as an empty string for me, while Michael's answer worked.Burma
@jk7: That is due to the fact that you are making RESTful requests (using WebHttpBinding of WCF). The solution above will work for SOAP requests (any standard bindings besides WebHttpBinding) while @Michael's solution will work for RESTful requests.Pearlstein
Related question here: #853360Pearlstein
M
2
OperationContext.Current.IncomingMessageHeaders.Action.Split('/').ToList().Last();
Mcdaniels answered 8/10, 2012 at 3:0 Comment(2)
The ToList() isn't necessary, is it?Adara
@Nuzzolilo, Not Required.Mcdaniels
V
1

Little late to the party but I had to dig a little deeper than existing answers on this question because they seem to involve getting the action name and not the operation name. (Frequently they are the same so getting the action name does, in fact, get the operation name.)

Microsoft's Application Insights SDK Labs' WCF library makes this concerted effort:

private string DiscoverOperationName(OperationContext operationContext)
{
    var runtime = operationContext.EndpointDispatcher.DispatchRuntime;
    string action = operationContext.IncomingMessageHeaders.Action;
    if (!string.IsNullOrEmpty(action))
    {
        foreach (var op in runtime.Operations)
        {
            if (op.Action == action)
            {
                return op.Name;
            }
        }
    }
    else
    {
        // WebHttpDispatchOperationSelector will stick the
        // selected operation name into a message property
        return this.GetWebHttpOperationName(operationContext);
    }

    var catchAll = runtime.UnhandledDispatchOperation;
    if (catchAll != null)
    {
        return catchAll.Name;
    }

    return "*";
}

private string GetWebHttpOperationName(OperationContext operationContext)
{
    var name = WebHttpDispatchOperationSelector.HttpOperationNamePropertyName;
    if (this.HasIncomingMessageProperty(name))
    {
        return this.GetIncomingMessageProperty(name) as string;
    }

    return "<unknown>";
}
Vaientina answered 1/11, 2018 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.