具有进程内托管的Dotnet核心多个启动类

Osk*_*erg 13 c# .net-core iis-express-10

我有一个dotnet核心v.2.1应用程序,它利用"startup-class-by-environment-name-convention" Startup为不同的环境使用不同的类,例如开发,登台和生产.该Program.Main.CreateWebHost方法看起来类似于:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    var startupAssembly = Assembly.GetExecutingAssembly();
    var webHostBuilder = WebHost.CreateDefaultBuilder(args)
                                .UseStartup(startupAssembly.FullName);
    return webHostBuilder;
}
Run Code Online (Sandbox Code Playgroud)

但是,在升级到dotnet core v.2.2(并且启动切换仍然很好)之后,我想尝试进程内托管功能.切换到进程内托管模型,并在Visual Studio 2017更新和IIS Express本地运行时,运行应用程序时出现此错误:

HTTP错误500.30 - ANCM进程内启动失败

这个问题的常见原因:

  • 应用程序无法启动
  • 应用程序启动但随后停止
  • 应用程序启动但在启动期间抛出异常

故障排除步骤

  • 检查系统事件日志以获取错误消息
  • 启用日志记录应用程序进程的stdout消息
  • 将调试器附加到应用程序进程并进行检查

有关更多信息,请访问:https://go.microsoft.com/fwlink/?LinkID = 2028265

我检查了所有日志,我能找到的就是:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name="IIS Express AspNetCore Module V2" /> 
    <EventID Qualifiers="0">1007</EventID> 
    <Level>2</Level> 
    <Task>0</Task> 
    <Keywords>0x80000000000000</Keywords> 
    <TimeCreated SystemTime="2018-12-14T10:37:48.327935100Z" /> 
    <EventRecordID>3693</EventRecordID> 
    <Channel>Application</Channel> 
    <Computer>[whatever]</Computer> 
    <Security /> 
  </System>
  <EventData>
    <Data>Application '/LM/W3SVC/2/ROOT' with physical root '[whatever again]' failed to load clr and managed application. CLR worker thread exited prematurely</Data> 
    <Data>Process Id: 29836.</Data> 
    <Data>File Version: 12.2.18316.0. Description: IIS ASP.NET Core Module V2 Request Handler. Commit: ce8cf65589734f82b0536c543aba5bd60d0a5a98</Data> 
  </EventData>
</Event>
Run Code Online (Sandbox Code Playgroud)

我阅读了所有的dotnet核心托管迁移2.1 - > 2.2我能找到的任何MSDN文章,并尝试了一堆不同的设置,但除了使用默认设置之外没有找到任何解决方案:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>();
Run Code Online (Sandbox Code Playgroud)

...哪些不会 - 我想将启动切换与进程内托管一起使用.有谁知道如何实现这一点,或有任何关于如何进行故障排除的建议?

编辑:我从@cilerler得到了我需要的答案.为了完整起见,这是我的案例中发生的事情:

加载我的自定义配置文件失败,因为此过程依赖于调用Directory.GetCurrentDirectory(),其结果在切换到进程内托管时发生更改.这是我对该部分的原始代码(为简洁起见缩写):

var basePath = $"{Directory.GetCurrentDirectory()}\\ConfigurationFiles";
builder.SetBasePath(basePath);

builder.AddJsonFile("some.config.json", false, true);
builder.AddJsonFile($"some.config.{context.HostingEnvironment.EnvironmentName}.json", true, true);
Run Code Online (Sandbox Code Playgroud)

关键部分 - 再次 - 调用GetCurrentDirectory(),以便解决问题,我根据接受的答案中的建议改变了上述内容,以:

var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var basePath = $"{currentDirectory}\\ConfigurationFiles";
builder.SetBasePath(basePath);
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅接受的答案;)

cil*_*ler 10

根据aspnet-core-module文章,它说

GetCurrentDirectory返回由IIS启动的进程的worker目录,而不是应用程序的目录(例如,对于w3wp.exe,为C:\ Windows\System32\inetsrv).

这意味着配置加载程序将无法查找依赖于调用的appsettings.*文件或任何其他文件(如自定义配置文件)GetCurrentDirectory.为了在您的Program.cs中public static void Main(string[] args) {添加以下行后立即解决它

Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Run Code Online (Sandbox Code Playgroud)

此外,在项目文件(例如MyProject.csproj)中,请确保您具有以下行appsettings.*并存在于输出文件夹中.

<ItemGroup>
  <Content Update="appsettings.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Update="appsettings.Development.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Update="appsettings.Production.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)