如何限制的逻辑呼叫的上下文范围内 [英] How to limit the scope of a logical call context

查看:120
本文介绍了如何限制的逻辑呼叫的上下文范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经把在呼叫上下文的一些数据( CallContext.SetData(键,数据)),其中数据是一种实现 ILogicalThreadAffinative 。它实现ILogicalThreadAffinative的原因是,它必须在多个线程在当前应用程序共享。

I have placed some data on the call context (CallContext.SetData(key,data)) where data is of a type that implements ILogicalThreadAffinative. The reason that it implements ILogicalThreadAffinative is that it must be shared across multiple threads in the current application.

但是,应用程序也使得远程调用其他服务,而这正是问题的原因。我ILogicalThreadAffinative实现不是可序列化,不应该。即使我将其标记为可序列化,远程应用程序无法访问在该类型声明,大会因此它不能够反序列化。

However, the application also makes remote calls to another service, and this is where the problem comes in. My ILogicalThreadAffinative implementation is not serializeable and should not be. Even if I were to mark it serializable, the remote application does not have access to the assembly in which the type is declared so it would not be able to deserialize it.

那么,如何在我的应用程序(应用程序域),而与每一个外部应用程序碰巧需要跟共享调用上下文的数据?

So how do I share call context data within my application (AppDomain) but not with every external application it happens to need to talk to?

推荐答案

最后,我通过实现自定义IMessageSink我在远程调用的客户端的格式化程序接收器插入之前解决了这个。水槽剥离该呼叫上下文数据之前,它会通过线路。以下是相关的方法。

Ultimately I solved this by implementing a custom IMessageSink which I inserted before the formatter sink on the client side of the remoting call. The sink strips out the call context data before it goes across the wire. Below is the relevant method.

private static void SanitizeCallContext(IMessage msg)
    {
        var callContext = msg.Properties["__CallContext"] as LogicalCallContext;

        if (callContext == null) return;

        var sanitizedContext = (LogicalCallContext) callContext.Clone();
        var prop = typeof (LogicalCallContext).GetProperty("Datastore",
                                                           BindingFlags.Instance | BindingFlags.NonPublic);
        var dataStore = (Hashtable) prop.GetValue(sanitizedContext, null);
        foreach (var key in dataStore.Keys.Cast<string>().ToArray())
            sanitizedContext.FreeNamedDataSlot(key);

        msg.Properties["__CallContext"] = sanitizedContext;
    }

我不特别喜欢这种解决方案。它似乎多一点的hackish,但它是最好的解决办法我已经能够拿出。

I don't especially like this solution. It seems more than a little hackish, but it is the best solution I have been able to come up with.

这篇关于如何限制的逻辑呼叫的上下文范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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