In an ASP.NET Core 2.0 project targeting .NET Framework 4.7.1, we encountered the following exception at startup on developer workstations:
System.InvalidOperationException: Cannot find reference assembly 'System.AppContext.dll' file for package System.AppContext.Reference
at Microsoft.Extensions.DependencyModel.Resolution.ReferenceAssemblyPathResolver.TryResolveAssemblyPaths(CompilationLibrary library, List`1 assemblies)
at Microsoft.Extensions.DependencyModel.Resolution.CompositeCompilationAssemblyResolver.TryResolveAssemblyPaths(CompilationLibrary library, List`1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List`1 assemblies)
at Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths()
at Microsoft.AspNetCore.Mvc.ApplicationParts.AssemblyPart.<>c.<GetReferencePaths>b__8_0(CompilationLibrary library)
at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
at Microsoft.AspNetCore.Mvc.Razor.Compilation.MetadataReferenceFeatureProvider.PopulateFeature(IEnumerable`1 parts, MetadataReferenceFeature feature)
at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateFeature[TFeature](TFeature feature)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.GetCompilationReferences()
at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
at Microsoft.AspNetCore.Mvc.Razor.Internal.DefaultRazorReferenceManager.get_CompilationReferences()
at Microsoft.AspNetCore.Mvc.Razor.Internal.LazyMetadataReferenceFeature.get_References()
at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
at Microsoft.AspNetCore.Razor.Language.RazorTemplateEngine.GenerateCode(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CreateCacheEntry(String normalizedPath)
The site worked as expected when targeting net461 and net47. Interestingly, the site worked as expected targeting net471 when deployed on a web server (runtime vs SDK?)
The resolution was to include a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation, like so:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
(Because we target full framework, we cannot reference the Microsoft.AspNetCore.All metapackage.)
The issue was resolved by including a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation. Posting for anyone else who happens to encounter this issue.
The issue is reproducible even if I add a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation but set MvcRazorCompileOnPublish to false.
Same problem. When I publish the MvcRazorCompileOnPublish does the compilation based on true/false but when the setting exists in csproj I get an exception when running locally.
Why is this closed?
I closed the issue because I found and posted a solution and it seemed to me as though the problem was caused by not reading the documentation carefully enough. I will reopen since it seems more complex than suggested by just my own experience.
@pranavkm, can you please look into this?
This is a dup of https://github.com/dotnet/sdk/issues/1738. I'm following up with the SDK team to figure out if there's a reasonable workaround for this.
Most helpful comment
The issue was resolved by including a reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation. Posting for anyone else who happens to encounter this issue.