开发者

In C# how do I reference the port number configured in app.exe.config?

开发者 https://www.devze.com 2023-04-06 13:49 出处:网络
In our server, we configure the port in app.config as follows: <configuration> <system.runtim开发者_如何学Goe.remoting>

In our server, we configure the port in app.config as follows:

<configuration>
   <system.runtim开发者_如何学Goe.remoting>
      <application>
         <channels>
            <channel ref="tcp" port="1234" />
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>

We then proceed to configure the server with the following C# code:

RemotingConfiguration.Configure(string.Format("{0}{1}", appFolder, "app.exe.config"), false);

How do I reference the port number after it has been configured short of parsing the file by hand?


Looks like it is possible after all. After calling RemotingConfiguration.Configure(string, bool), I run the following method:

private string GetPortAsString()
          {
             // Parsing
             System.Runtime.Remoting.Channels.IChannel[] channels = System.Runtime.Remoting.Channels.ChannelServices.RegisteredChannels;
             foreach (System.Runtime.Remoting.Channels.IChannel c in channels)
             {
                System.Runtime.Remoting.Channels.Tcp.TcpChannel tcp = c as System.Runtime.Remoting.Channels.Tcp.TcpChannel;
                if (tcp != null)
                {
                   System.Runtime.Remoting.Channels.ChannelDataStore store = tcp.ChannelData as System.Runtime.Remoting.Channels.ChannelDataStore;
                   if (store != null)
                   {
                      foreach (string s in store.ChannelUris)
                      {
                         Uri uri = new Uri(s);
                         return uri.Port.ToString(); // There should only be one, and regardless the port should be the same even if there are others in this list.
                      }
                   }
                }
             }

             return string.Empty;
          }

This gives me the TcpChannel info I need, which allows me to grab the ChannelUri and get the port.

GRAIT SUCCESS!


You can only configure through code or configuration, you can't do both. This means you can't access the configured details via code, (without passing the xml file yourself).


I've just taken a look at the ConfigurationManager to help get the values you need...unfortunately it doesnt look like there is a sectionGroup for system.runtime.remoting: ie, this call fails:

var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var sectionGroup = cfg.GetSectionGroup("system.runtime.remoting");

In C# how do I reference the port number configured in app.exe.config?

So it doesn't appear to me you can use anything existing in the framework to extract it nicely. I'm unsure why this sectionGroup doesnt exist in-code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号