如何限制的逻辑呼叫的上下文范围内范围内、上下文、逻辑

由网友(梦想起航我起床)分享简介:我已经把在呼叫上下文的一些数据( CallContext.SetData(键,数据)),其中数据是一种实现 ILogicalThreadAffinative 。它实现ILogicalThreadAffinative的原因是,它必须在多个线程在当前应用程序共享。 I have placed some data on...

我已经把在呼叫上下文的一些数据( 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.

阅读全文

相关推荐

最新文章