问题
My application calls RemotingConfiguration.Configure()
to set up .NET Remoting. But since I need the portName attribute of the channels element to be different each time the application is run, I have to configure this particular setting programatically.
It sounds very simple (and probably is - but I've been searching Google all day for an answer).
So far I have:
RemotingConfiguration.Configure(Program.ConfigFilePath, false);
IChannel[] regChans = ChannelServices.RegisteredChannels;
IpcChannel ipcChannel = (IpcChannel)ChannelServices.GetChannel(regChans[0].ChannelName);
The debugger shows me that ipcChannel._serverChannel._prop
would be the hash table to which I need to add an entry such as ["portName"] = uniquePortName
, but I simply cannot see how to access and modify this.
I know I could always scrap the *.config file and do the whole thing programatically, but I really don't want to throw away the benefits of having most of the settings easily editable.
Is it too late to modify an IpcClientChannel
object once RemotingConfiguration.Configure()
has returned? Obviously I could probably write my own version of RemotingConfiguration.Configure()
, but that doesn't seem like the Right Way to do things either.
回答1:
I think I've established that what I wanted to do is not possible.
You can either use RemotingConfiguration.Configure()
or something like:
IDictionary channelProperties = new Hashtable();
channelProperties.Add("authorizedGroup", "Everyone");
channelProperties.Add("portName", "Client-" + Guid.NewGuid().ToString()); // Unique port name
IpcChannel channel = new IpcChannel(channelProperties, null, null);
ChannelServices.RegisterChannel(channel);
You can't mix and match. At least, that seems to be the case.
(Of course you can still read any of these settings from the *.config file, but the point is you have to explicitly code for all of the options you might want to support.)
来源:https://stackoverflow.com/questions/4215664/modify-settings-after-calling-remoteconfiguration-configureconfigfile