c# - 具有进程内托管的 Dotnet Core 多个启动类

标签 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;
}

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

HTTP Error 500.30 - ANCM In-Process Start Failure

Common causes of this issue:

  • The application failed to start
  • The application started but then stopped
  • The application started but threw an exception during startup

Troubleshooting steps:

  • Check the system event log for error messages
  • Enable logging the application process' stdout messages
  • Attach a debugger to the application process and inspect

For more information visit: 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>

我阅读了所有 dotnet core in-hosting migrate 2.1 -> 2.2,无论我能找到什么 MSDN 文章,并尝试了一堆不同的设置,但除了使用默认设置外找不到任何解决方案:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>();

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

编辑: 我从@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);

关键部分再次是对 GetCurrentDirectory() 的调用,因此,为了解决这个问题,我根据接受的答案中的建议将上面的内容更改为:

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

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

最佳答案

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

GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's directory (for example, C:\Windows\System32\inetsrv for w3wp.exe).

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

Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

另外,在项目文件中 (例如 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>

关于c# - 具有进程内托管的 Dotnet Core 多个启动类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53783260/

相关文章:

iis-express - JetBrains Rider 能否与 IISExpress 一起正常工作?

c# - 在 ASP.NET MVC 3 中注入(inject) IPrincipal - 我做错了什么?

c# - 使用 mvvm 在 wpf 数据网格中绑定(bind)级联组合框项目源

java - Java 中的 .Net Guid.ToByteArray() 相当于什么

c# - 如何让命名空间别名 operator::在 C# 下工作?

c# - 无法加载文件或程序集 Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0

visual-studio-code - vscode omnisharp 不适用于解决方案,但适用于项目

c# - Asp.net core 2.0 Configure.Services<model>() 绑定(bind)不适用于用户 secret

c# - 如何增加请求长度以在 ASP.NET Core 中上传大文件?