0

We have a WCF service hosted in IIS which has a netMsmqBinding. Before the message is dispatched to our service, we would like to log the MSMQ Lookup ID. Where I can I find this information?

We get it in an IErrorHandler because an MsmqPoisonMessageException has a MessageLookupId property. We need to log it at the start of a request so we can correlate the exception with the message.

I thought an IDispatchMessageInspector would be the right place for this, but I can't seem to find any property in the AfterReceiveRequest method that would give me the Lookup ID.

Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66

1 Answers1

0

So far the only solution I've found is to use reflection on the Message parameter of AfterReceiveRequest. It works, it just seems strange this isn't surfaced as a public property.

class MsmqLookupIdBehavior : IDispatchMessageInspector
{
    static PropertyInfo lookupIdPropertyInfo;

    static MsmqLookupIdBehavior()
    {
        try
        {
            var type = typeof(MsmqMessageProperty);
            lookupIdPropertyInfo = type.GetProperty("LookupId", BindingFlags.NonPublic | BindingFlags.Instance);
        }
        catch { }
    }

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        if (lookupIdPropertyInfo != null)
        {
            var lookupIds =
                request.Properties.Values
                .Where(p => p is MsmqMessageProperty)
                .Select(p => lookupIdPropertyInfo.GetValue(p))
                .Where(v => v is long)
                .Select(v => (long)v);

            foreach (var lookupId in lookupIds)
            {
                // Use lookupId here
            }
        }
        return null;
    }

    // The rest of IDispatchMessageInspector here, not relevant for this behavior
}
Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66