Select a specific channel in Activator.GetObject

Suppose you register two TcpChannels in .NET Remoting.

Then I try to get the proxy server using Activator.GetObject using the tcp://... URL tcp://...

Can I choose which channel to use?

Thanks in advance.

+6
source share
1 answer

Have you decided this?

I had the same problem last week. (A small unpublished side effect of nunit is that it launches the "tcp" channel by default when loading your DLLs to run unit tests (my problem) ... then I created my own instance of TcpClientChannel with user receivers to talk to our server software software ... and our receivers didn’t shoot when I instantiated the server object)

There are 3 solutions:

  • If you want it to constantly override another, just pass the priority property to the IDictionary constructor on your TcpClientChannel. The default value, if not set, is 1, so if you want it to override, register the default channel registration to "tcp", then set the value above 1. NB, the "name" property must also be set, but it can be string .Empty if required (and then you can have as much as you want)

  • Name your channels when registering, and then write a good IDisposable wrapper to invoke the using construct, which calls ChannelServices.Unregister (...) on channels that you don’t want to activate when Activator.GetObject (...) is called. Then, when your “used” block ends (that is, it calls Dispose ()), just reload the channels you have not registered ... make sure that you use the “lock” for some general reference to the object if it is multithreading in your application. .. it can create a bottleneck! (This is an approach that I took because my unit tests used a pre-written library that created a connection to the server: the risk of instability in the production software for my unit testing was too high)

  • Temporarily raise the priority of the target channel using deep reflection (for example, using private FieldInfos ... namely private int_channelPriority (I think) ... use a reflector to double check) before you call Activator. Getobject This is also open to threading issues, and also not to the card protected version (so I avoided this)

+5
source

Source: https://habr.com/ru/post/886868/


All Articles