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.
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?
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.
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.