Reputation: 1662
I have a very, very similar problem to this one.
Hi.
I have an application (Service) targeting .NET 4.61 and trying to move to AspNetCore self host with WebListener.
I am trying to investigate the requirements by testing it out in a console application.
The project i crated is a normal Windows console application, i referenced all the required packages with nuget and created required Startup class and create and start the host with WebHostBuilder.
(I don't have a normal Windows console application, but a dotnet console application on my ubuntu laptop)
We've differed a little bit in what we've tried and tested before hand.
Up until now I just made a HomeController
and had Index
return a string
with "Hello world"
. Ran the application, went to localhost and got the response. Perfect.
Now all I've added was that Index
return an IActionResult by returning View()
. Then I made an Index.cshtml
file in /Views/Home/
and now I get this:
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0] An unhandled exception has occurred: One or more compilation failures occurred:
followed by 150 lines like this:
gee1fd0w.wuj(4,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(5,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
gee1fd0w.wuj(6,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
...
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Object' is not defined or imported
gee1fd0w.wuj(37,13): error CS0518: Predefined type 'System.Void' is not defined or imported
ending with:
gee1fd0w.wuj(33,36): error CS0161: '_Views_Home_Index_cshtml.ExecuteAsync()': not all code paths return a value
at Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationResult.EnsureSuccessful()
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.CreateCacheEntry(String relativePath, String normalizedPath, Func'2 compile)
--- End of stack trace from previous location where exception was thrown ---
Followed by this:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Razor.Internal.CompilerCache.GetOrAdd(String relativePath, Func'2 compile)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
Followed by a series of shorter stack traces:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResultExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.d__6.MoveNext()
Now I'm not sure that I fully understand the answer the previous poster got:
Mvc's runtime compilation relies on DependencyContext (deps file) to look up compilation references which is only produced when you compile using dotnet CLI. You would have to add compilation references as part of the application startup for view compilation to work.
I did compile through dotnet cli, but is there something else that I'm missing here? When I try to replicate my project by running dotnet new mvc
and deleting everything but the things that are in my console-project. It runs right as rain and I get hello world from my Index.cshtml.
Instinct tells me that the problem lies in some file-difference, but I've diffed all of the files and I can't find any difference but app-name. I could have missed something but I'm wondering if it could perhaps be something automagical that I'm not aware of?
Have found the issue, looks like it's a difference in their generated bin/debug/netcoreapp1.1/<appname>.deps.json
. Quite a few differences, going to look into it now. But if I copy paste that file from the mvc-project to the console app, everything runs ok.
Unfortunately the console applications deps.json
-file is 1300+ lines long and the mvc applications version is 4300+ lines long so I'm not entirely sure it will be feasible to go through it and identifying why one works and the other don't.
It mostly seems to be a whole lot of dependency scopes that differ, a whole lot of the mvc ones contain dependencies to "NETStandard.Library": "1.6.1"
, but many others as well. There's also a bunch of compile-scopes that the console-application don't have.
In fact this seems to be the only difference. The mvc-application's deps.json
-file seem to have ~3000 more lines of dependencies and compile-scopes. So I think I'll give up the idea of figuring out what's causing the problem and and my plan to learn how to "build a console app into an mvc-app" :)
I'll leave the question here in case anyone find themselves in a similar situation.
Upvotes: 7
Views: 11924
Reputation: 4408
With the introduction of implicit usings in C# 10, this may happen because of implicit usings in your .csproj:
<Using Include="MyNamspace" />
Your razor files can be compiled well in the compile time, but failing during the runtime compilation because of missing namespace MyNamspace
. Solution is to add the using in your _ViewImports.cshtml
.
Upvotes: 1
Reputation: 5692
I had this issue with a (ASP).NET Core project. The root cause was because the .csproj file was automagically updated after I mistakenly deleted the backing class of my Razor page (the .cshtml.cs file I deleted). The .csproj file captured an update that explicitly kept the PageModel class out of the build hence the error.
Background info: I mistakenly deleted a PageModel file (.cshtml.cs) but quickly undid the delete via my Git repo. I did not realize the .csproj file was also updated.
Upvotes: 0
Reputation: 63
I had the same problem when I moved some of my classes to subdirectory and changed their namespaces. In my case deleting obj
and bin
directories, as well as running dotnet restore
wasn't enough.
I had to change all the references to the old folder in all cshtml files(_ViewImports.cshtml for example) if you used full path as reference to models. So from using ProjectName.Models
to using ProjectName.Models.NewDirectory
and so on. And then deleting obj bin
and running the dotnet command.
Upvotes: 4
Reputation: 4064
I have had this happen to me before.
Do a dotnet restore
and delete your obj
and bin
directories.
I may not be technically correct but I think what's happening here is your old build is creating conflicts with your new build that has new (or different) references.
Upvotes: 15
Reputation: 31
This should be fixed by using dotnet restore.
EDIT: Never mind, I see a comment already answered it.
Upvotes: 2