I have an ASP.NET Core application. The application needs to be started by windows service. When the service runs the application, I'm having the following error:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
EnsureSuccessful
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
However, if I run the application by clicking on the exe-file, everything seems to be normal. I double checked, the service had enough permissions, and the views are in the right place.
BUT! I had a situation when the service was looking for another file somewhere in win32 folder, because I had made a mistake and used Directory.GetCurrentDirectory()
instead of Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
to find the current folder. Is it possible that the similar mistake had been made?
The current problem was indeed similar to the former. As it turned out, I should have used the same Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
in Startup.cs:Main
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseContentRoot(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))
.UseStartup<Startup>()
.Build();
where by default Directory.GetCurrentDirectory()
was used as an argument for UseContentRoot(.)
. Also the same operation must be performed several lines of code earlier, when ConfigurationBuilder
is called.
The root of the problem is that the windows service is being called from win32 folder, so Directory.GetCurrentDirectory()
is giving win32 folder instead of folder of executable file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With