Dotnet Core Multiple Startup Classes with In-Process Hosting

不打扰是莪最后的温柔 提交于 2019-12-04 00:53:03
cilerler

According to aspnet-core-module article it says

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).

which means config loader will not be able to find appsettings.* files, or any other files such as custom config files, that depend on a GetCurrentDirectory call. In order to solve it in your Program.cs right after public static void Main(string[] args) { add the following line

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

Also, in project file (e.g. MyProject.csproj) make sure that you have the following lines and appsettings.* exists in output folder.

<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>

I had the same issue, different story.

Visual Studio 2017 - asp.net core mvc (sdk 2.2.104).

  1. Run the application. (yes it will give you the error again)
  2. in VS go to Exception Settings and enable C++ Exceptions, CLR Exceptions, Win32 Exceptions
  3. Restart your debugging progress and see if an exception is thrown.

In my case: I registered a service in Startup.cs via .AddScoped where the concrete serviceType parameter was actually an abstract class (forgot to change this after refactoring).

Else go to Windows Event Viewer and try to find a clue in the logs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!