2

I have an WCF Interceptor which gets called on every request:

public class WebServiceInterceptor : IDispatchMessageInspector
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var action = OperationContext.Current.IncomingMessageHeaders.Action;
        var name = instanceContext.GetServiceInstance().GetType().Name;
        if (action != null)
        {
            var operationName = action.Substring(action.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
        }
        return null;
    }

    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }
}

how can I get the name of the Method of the Service class that will be invoked? I want to get an attribute from this method.

jansepke
  • 1,933
  • 2
  • 18
  • 30

2 Answers2

1

All that string processing is a hack and unreliable. Learn to use the meta data that WCF provides you. You don't need to parse anything out of type names.

When you register this IDispatchMessageInspector you probably have the necessary meta data available (e.g. an OperationDescription). Pass that information to your class constructor and store it in instance fields. That way, AfterReceiveRequest can later use that information.

usr
  • 168,620
  • 35
  • 240
  • 369
  • my company uses not many features of WCF so I need to hack.. why is my solution unreliable? – jansepke Jul 27 '14 at 11:42
  • The first thing that comes to mind is that the method and type names do not necessarily match the contract and operation names. Besides that, it is hard to reason about this code and conclude that it is correct. Who knows what corner cases there are. – usr Jul 27 '14 at 12:11
  • we have no contract... I will test and see if there are corner cases – jansepke Jul 27 '14 at 13:22
0

I found the answer here: https://stackoverflow.com/a/5150194/1453662

string methodName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
MethodInfo info = instanceContext.GetServiceInstance().GetType().GetMethod(methodName);

This will NOT work for SOAP requests (we only use http)

jansepke
  • 1,933
  • 2
  • 18
  • 30
  • This will fail horribly if that particular header isn't set, which it won't be if this service is being invoked via SOAP. – Ian Kemp Jun 03 '17 at 22:05