RemotingConfiguration.Unconfigure() ???

I was playing around with an old poker server that I had written and decided to make some changes. The way the client/server works is that client scrapes the screen and turns the current screen state into a message. This message is then dispatched to the server using MSMQ and the response to the message is the action to be taken by the client (do nothing, click a button, sit out, close the window etc…).

My idea was to switch this process over to remoting, no reason really other than to do it.  I decided to use the RemotingConfiguration.Configure method to configure the remoting infrastructure. It’s pretty easy to do and allows for a lot of flexibility in the setup. One thing you usually want to do with a server is to start it and then…. stop it and restart it. This is useful for testing load balancing, or fail-over or rolling out changes.

Logically, you would think you should call something like RemotingConfiguration.Unconfigure() or some such method. After some google searching I learned that the method I was looking for does not exist. The only solution is to not use the Configure method, instead you can do it programatically.

Oddly enough, this was the way my first implementation worked. However, as I added more and more options to the config file for my poker server it started to feel like I was… reinventing the wheel… so to speak. Microsoft already made a decent remoting config file, why should I redo all that work. The simple answer is because Microsoft decided you did not need to unconfigure the configuration.

If you look at almost any Microsoft Server product that runs a service you can see the stop/start/pause of the service. From this it’s pretty obvious that Microsoft gets the need for a server to stop. So it seems pretty silly that they overlooked the Unconfigure.

Well, being lazy I refused to believe that I had to roll my own configuration file for remoting. I was determined to use the RemotingConfiguration.Configure method. Being lazy has a price though, it took me quite some time to figure out a solution.

So here it is. Recall that the Configure method really is just an automated way to bind the channels to a specific port. So, what you need to do is Unconfigure the configured channels.

Here is my Configure line.

public void Start()
{
RemotingConfiguration.Configure(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath, false);
}
Nothing fancy here…

Now what we need is a list of all channels in use and then we can “unconfigure” them…

public void Stop()
{
IChannel[] channels = ChannelServices.RegisteredChannels;

for (int i = 0; i <= channels.GetUpperBound(0); i++)
ChannelServices.UnregisterChannel(channels[i]);
}

So there it is, I have not tested it real thoroughly but it seems to work. Calling Start, Stop, Start, Stop does not throw any errors. My client can connect when the start is called and can’t after stop is called. All my NUnit tests pass as well.

Hopefully this can help someone else but I still wonder why Microsoft did not provide a obvious solution for this or throw some stuff into the MSDN site to explain this.  It’s not hard, it’s just not documented.  If there is a downside to this I hope someone will let me know.
Thats not

One Response to “RemotingConfiguration.Unconfigure() ???”

  1. Shankar Ram Says:

    How do I Stop .Net remoting service gracefully. If someone stops the service, .Net remoting should not accept any new connections from the client AND wait for current connections to complete(saving to a database) and then stop the service

Leave a Reply