InvocationContext不坚持内容 [英] InvocationContext not persisting contents

查看:545
本文介绍了InvocationContext不坚持内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个值到 InvocationContext 词典,但它不会持续到下一个电话。即多数民众赞成在截获返回false的 InvocationContext.ContainsKey链中的每个电话(跟踪-ID)

I am added a value to the InvocationContext dictionary, however it doesn't persist to the next call. i.e. each call in the chain that's intercepted is returning false for InvocationContext.ContainsKey("tracing-id").

public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    Guid tracingId;

    if (!input.InvocationContext.ContainsKey(TRACING_ID))
    {
        tracingId = _tracingIdProvider.NewTracingId();
        input.InvocationContext.Add(TRACING_ID, tracingId);
    }
    else
    {
        tracingId = (Guid)input.InvocationContext[TRACING_ID];
    }

    var methodReturn = getNext()(input, getNext);

    return methodReturn;
}

我可以添加一个条目 InvocationContext ,然而,当 GETNEXT()(输入,GETNEXT)是调用,并且在下次调用被拦截的 InvocationContext 是空的。

I can add an entry to the InvocationContext, however, when getNext()(input, getNext) is invoked and the next call is intercepted the InvocationContext is empty.

我误解了如何 InvocationContext 用?如果是这样,什么是坚持的东西像一个ID从一个方法调用到下一个正确的方法是什么?

Am I misunderstanding how InvocationContext is used? If so, what's the correct way to persist something like an ID from one method call to the next?

推荐答案

我的测试显示, InvocationContext 属性的内容是一个呼叫时只有坚持。这意味着你的code将工作只在有登记的许多行为。他们将被调用逐一进行相同的方法调用,他们可以使用 InvocationContext 共享数据。然而,当你观察到的,下一次调用 InvocationContext 将被清除。

My tests shows that the content of InvocationContext property is persisted only during one call. It means that your code will work only when there are many behaviors registered. They will be invoked one by one for the same method call and they can use InvocationContext to share data. However, as you observed, for the next call InvocationContext will be cleared.

我没有太多的经验,团结,所以我不知道什么是推荐的解决办法。您可以考虑创建自己的调用上下文并执行它作为一个单身。感谢所有的呼叫都将有机会获得同样的实例,TRACING_ID将通过调用持久化。它应该工作。

I don't have much experience with Unity so I don't know what is the recommended workaround. You may consider to create your own invocation context and implement it as a singleton. Thanks to that all calls will have access to the same instance and TRACING_ID will be persisted across calls. It should work.

然而,问题是什么时候调用上下文应该被清除。如果你不这样做,那么所有呼叫都将具有相同的TRACING_ID。它不具有意义。例如,您可能需要重置TRACING_ID执行负责启动处理(业务场景),一个特定的方法时。

However, the question is when the invocation context should be cleared. If you don't do that, then all calls will have the same TRACING_ID. It doesn't have sense. For example, you may want to reset TRACING_ID when a particular method responsible for starting processing (a business scenario) is executed.

这篇关于InvocationContext不坚持内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆