在运行时查找应用程序的入口点(启动项目)

时间:2019-11-17 22:54:45

标签: c# visual-studio web-config app-config

我有一个包含几个可执行项目(一个Web项目和3个Windows Service项目)的解决方案。该Web项目具有一个web.config文件,而Windows服务项目具有一个app.config文件。

我在上述所有配置文件中都定义了一个名为EnvironmentName的变量:

<appSettings>
    <add key="EnvironmentName" value="Dev"></add> // could also be Test or Prod
</appSettings>

我有一个Common项目,该项目读取web.config / app.config并确定环境(可从Web Project和Windows Service项目访问该环境):

共同项目:

public static class EnvironmentConfig
{
    public static string EnvironmentName => ConfigurationManager.AppSettings["EnvironmentName"];
}

现在,我想从同一文件中获取应用程序的入口点,所以像这样:

public static class EnvironmentConfig
{
    public static string EnvironmentName => ConfigurationManager.AppSettings["EnvironmentName"];

    public static string EntryPointName => // return WebProject, Service1, Service2 or Service3
}

我想到的一种解决方案是在所有配置文件中定义EntryPointName,但我想知道是否有更好的解决方案?


我尝试了以下建议的in this question

Assembly asm = Assembly.LoadFile(Assembly.GetExecutingAssembly().Location);
MethodInfo myMethod = asm.EntryPoint;

程序集将返回Common项目程序集,但是EntryPoint返回null ...

0 个答案:

没有答案