简体   繁体   English

在C#中,如何引用在app.exe.config中配置的端口号?

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

In our server, we configure the port in app.config as follows: 在我们的服务器中,我们在app.config中配置端口,如下所示:

<configuration>
   <system.runtime.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: 然后,我们使用以下C#代码配置服务器:

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: 调用RemotingConfiguration.Configure(string,bool)之后,我运行以下方法:

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. 这给了我所需的TcpChannel信息,这使我可以获取ChannelUri并获取端口。

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). 这意味着您无法通过代码访问已配置的详细信息(无需自己传递xml文件)。

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: 我刚刚看过ConfigurationManager来帮助您获取所需的值...不幸的是,它看起来好像没有system.runtime.remoting的sectionGroup:即,此调用失败:

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

在此处输入图片说明

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. 我不确定为什么这个sectionGroup在代码中不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM