我正在使用代理和拦截进行日志记录。我想记录的属性之一是来自Rabbit MQ的消息ID。
我们使用以下对象:
namespace MassTransit
{
public interface ConsumeContext<out T> : ConsumeContext, MessageContext, PipeContext, IPublishEndpoint, IPublishObserverConnector, ISendEndpointProvider where T : class
{
T Message { get; }
/// <summary>Notify that the message has been consumed</summary>
/// <param name="duration"></param>
/// <param name="consumerType">The consumer type</param>
Task NotifyConsumed(TimeSpan duration, string consumerType);
/// <summary>
/// Notify that a fault occurred during message consumption
/// </summary>
/// <param name="duration"></param>
/// <param name="consumerType"></param>
/// <param name="exception"></param>
Task NotifyFaulted(TimeSpan duration, string consumerType, Exception exception);
}
}
这是我需要在拦截中掌握的通用消息。
我可以成功地将它投射到一个物体上说:
ConsumeContext<AuthenticationDataRequest>
在visual studio中,一旦我投射它,就会弹出Message对象(没有强制转换,没有MessageObject)。
要投射我正在使用以下通用方法:
public Guid? RunMessageRetrieve(dynamic obj, Type castTo)
{
MethodInfo castMethod = GetType().GetMethod("GetMessageIdFromContext").MakeGenericMethod(castTo);
return castMethod.Invoke(null, new object[] { obj }) as Guid?;
}
public static Guid? GetMessageIdFromContext<T>(dynamic context) where T : class
{
Guid? messageId = null;
try
{
var contextCasted = (T)context;
Type contextType = contextCasted.GetType();
var message = contextCasted.GetType().GetProperty("Message");
if (message != null)
{
messageId = message.GetType().GetProperty("MessageId").GetValue(message) as Guid?;
}
}
catch (InvalidCastException castException)
{
Console.WriteLine("Could not retrieve message Id from context message as the cast failed");
}
catch (NullException nullException)
{
Console.WriteLine("Could not retrieve message Id from context as the message Id did not exist");
}
return messageId;
}
在这里你可以在visual studio中看到消息,在其中我可以得到消息ID:
但是我试图使用反射来获取实际的消息属性,因为我当然不知道编译时的类型,我似乎无法解决它。以下是null,因为它当然是泛型类型:
var message = contextCasted.GetType().GetProperty("Message");
这必须是可行的,因为在拦截之后调用实际方法时,它具有带消息的正确对象。
答案 0 :(得分:0)
只需转换为具有所需属性的界面,然后使用它。不需要反思。
public static Guid? GetMessageId<T>(ConsumeContext<T> input) where T : class
{
var casted = input as ConsumeContext<IMessage>;
if (casted == null) return null;
return casted.Message.MessageId;
}
示例程序:
public class Program
{
public static Guid? GetMessageId<T>(ConsumeContext<T> input) where T : class
{
var casted = input as ConsumeContext<IMessage>;
if (casted == null) return null;
return casted.Message.MessageId;
}
public static void Main()
{
var exampleObject = new MyClass<Message>();
exampleObject.Message = new Message { MessageId = new Guid("00000001-0002-0003-0004-000000000005") };
var messageId = GetMessageId(exampleObject);
Console.WriteLine(messageId.Value);
}
}
输出:
00000001-0002-0003-0004-000000000005
如果你必须向下转换它也有效,即使T
是特定类型的消息,你仍然可以将它转换为暴露消息的接口或基类。
public static void Main()
{
var exampleObject = new MyClass<SpecificMessage>();
exampleObject.Message = new SpecificMessage { MessageId = new Guid("00000001-0002-0003-0004-000000000005") };
var messageId = GetMessageId(exampleObject);
Console.WriteLine(messageId.Value);
}
答案 1 :(得分:0)
我最后选择了以下内容,做了一个款待,我没想到会立即直接使用Type。这是动态参数搞砸了事情:
public static Guid? GetMessageIdFromContext(object context, Type contextType)
{
Guid? messageId = null;
try
{
var contextProp = contextType.GetProperty("Message");
if (contextProp != null)
{
var message = contextProp.GetValue(context);
if (message != null)
{
messageId = message.GetType().GetProperty("UniqueId").GetValue(message) as Guid?;
}
}
}
catch (NullException nullException)
{
Console.WriteLine("Could not retrieve message Id from context as the message Id did not exist");
}
return messageId;
}