按照此doc,我在Windows服务中有一个ASP.NET核心自主应用程序。项目TargetFramework
是.NET framework 4.7.1,它可以按预期提供http请求。
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net471</TargetFramework>
<RuntimeIdentifier>win81-x64</RuntimeIdentifier>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>
唯一的问题是来自Razor模板引擎,当应用程序正在编译剃刀视图时,会抛出以下异常。
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
widmeur4.h03(4,62): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(4,81): error CS0518: Predefined type 'System.String' is not defined or imported
widmeur4.h03(4,112): error CS0518: Predefined type 'System.Type' is not defined or imported
widmeur4.h03(4,11): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(8,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
widmeur4.h03(9,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
widmeur4.h03(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
widmeur4.h03(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
widmeur4.h03(14,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
widmeur4.h03(15,96): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
widmeur4.h03(15,96): error CS0518: Predefined type 'System.Boolean' is not defined or imported
widmeur4.h03(15,47): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(18,67): error CS1983: The return type of an async method must be void, Task or Task<T>
widmeur4.h03(18,39): error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
widmeur4.h03(29,49): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
widmeur4.h03(29,118): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(31,49): error CS0234: The type or namespace name 'IUrlHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
widmeur4.h03(31,71): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(33,49): error CS0234: The type or namespace name 'IViewComponentHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
widmeur4.h03(33,87): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(35,59): error CS0234: The type or namespace name 'IJsonHelper' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering' (are you missing an assembly reference?)
widmeur4.h03(35,83): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(37,59): error CS0234: The type or namespace name 'IHtmlHelper<>' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.Rendering' (are you missing an assembly reference?)
widmeur4.h03(37,71): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(37,71): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(37,71): error CS1980: Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
widmeur4.h03(37,71): error CS0518: Predefined type 'System.Boolean' is not defined or imported
widmeur4.h03(37,92): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(18,67): error CS0508: '_Views_Status_Index_cshtml.ExecuteAsync()': return type must be 'Task' to match overridden member 'RazorPageBase.ExecuteAsync()'
widmeur4.h03(18,67): error CS0012: The type 'Task' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(28,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(28,10): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(30,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(30,10): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(32,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(32,10): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(34,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(34,10): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(36,58): error CS0012: The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(36,10): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(20,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(20,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(20,26): error CS0518: Predefined type 'System.Int32' is not defined or imported
widmeur4.h03(20,31): error CS0518: Predefined type 'System.Int32' is not defined or imported
widmeur4.h03(20,34): error CS0518: Predefined type 'System.Boolean' is not defined or imported
widmeur4.h03(20,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(21,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(21,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(21,26): error CS0518: Predefined type 'System.String' is not defined or imported
widmeur4.h03(21,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(22,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(22,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(22,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(23,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(23,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(23,26): error CS0518: Predefined type 'System.Int32' is not defined or imported
widmeur4.h03(23,31): error CS0518: Predefined type 'System.Int32' is not defined or imported
widmeur4.h03(23,34): error CS0518: Predefined type 'System.Boolean' is not defined or imported
widmeur4.h03(23,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(24,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(24,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(24,26): error CS0518: Predefined type 'System.String' is not defined or imported
widmeur4.h03(24,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(25,13): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
widmeur4.h03(25,13): error CS0518: Predefined type 'System.Object' is not defined or imported
widmeur4.h03(25,13): error CS0518: Predefined type 'System.Void' is not defined or imported
widmeur4.h03(18,67): error CS0161: '_Views_Status_Index_cshtml.ExecuteAsync()': not all code paths return a value
widmeur4.h03(15,18): error CS0518: Predefined type 'System.Void' is not defined or imported
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(RazorCodeDocument codeDocument, String generatedCode)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorViewCompiler.CreateCacheEntry(String normalizedPath)
我的Visual Studio 2017版本是15.3。 已安装.NET Core SDK 2.0。
有谁知道如何解决这个问题?
更新:
我尝试根据此issue修改csproj文件。
<Project Sdk="Microsoft.NET.Sdk.Web">
<!--
Work around https://github.com/NuGet/Home/issues/4412. MVC uses DependencyContext.Load() which looks next to a .dll
for a .deps.json. Information isn't available elsewhere. Need the .deps.json file for all web site applications.
-->
<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>
<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
</Target>
</Project>
同样的问题仍然存在