10

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?

2
  • 2
    Try to set current directory on service start up. The default is System32 I think Commented Aug 10, 2016 at 6:51
  • 1
    @ Aleksey L. Thanks, I just figured it out! :D Commented Aug 10, 2016 at 6:53

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.