【问题标题】:Where can I find the MSMQ Lookup ID before WCF dispatches a message?在 WCF 分派消息之前,我在哪里可以找到 MSMQ 查找 ID?
【发布时间】:2015-03-06 02:06:51
【问题描述】:

我们有一个托管在 IIS 中的 WCF 服务,它有一个netMsmqBinding。在将消息发送到我们的服务之前,我们想记录MSMQ Lookup ID。我在哪里可以找到这些信息?

我们在IErrorHandler 中得到它,因为MsmqPoisonMessageException 具有MessageLookupId 属性。我们需要在请求开始时记录它,以便将异常与消息关联起来。

我认为IDispatchMessageInspector 是处理此问题的正确位置,但我似乎无法在AfterReceiveRequest 方法中找到任何可以为我提供查找 ID 的属性。

【问题讨论】:

    标签: wcf msmq netmsmqbinding


    【解决方案1】:

    到目前为止,我发现的唯一解决方案是对 AfterReceiveRequest 的 Message 参数使用反射。它有效,只是它没有作为公共财产浮出水面似乎很奇怪。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2014-07-16
      • 2012-05-30
      • 2013-07-13
      相关资源
      最近更新 更多