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?

Vaccano
  • 78,325
  • 149
  • 468
  • 850

5 Answers5

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);
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 2
    `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`. – EzLo Feb 20 '20 at 07:23
8
var operationName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
bonh
  • 2,863
  • 2
  • 33
  • 37
Michael
  • 91
  • 1
  • 2
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.

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • Came up as an empty string for me, while Michael's answer worked. – jk7 Jan 29 '16 at 01:24
  • @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. – Derek W Jan 29 '16 at 13:22
  • Related question here: https://stackoverflow.com/questions/852860/wcf-retrieving-methodinfo-from-operationcontext – Derek W Jan 29 '16 at 13:27
2
OperationContext.Current.IncomingMessageHeaders.Action.Split('/').ToList().Last();
Prasad Kanaparthi
  • 6,423
  • 4
  • 35
  • 62
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>";
}
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96