使用Roslyn编译解决方案时核心类型错误

时间:2018-08-08 21:28:05

标签: c# .net msbuild roslyn

我正在尝试使用Roslyn(Microsoft.CodeAnalysis.CSharp 2.8.2和朋友)用几个针对.NET Framework 4.6的C#项目编译一个解决方案。

有问题的解决方案在VS2017中可以很好地构建,但是当我尝试通过Roslyn进行编译时,遇到了诸如“找不到类型或名称空间名称'AssemblyTitleAttribute'之类的基本核心类型”的错误。

编辑:也许更重要的是,一旦打开解决方案,MSBuildWorkspace本身就会发出诊断信息。他们看起来像这样:

  

Msbuild在处理文件时失败   'C:\ dev \ MySolution \ My.Assembly.Name \ My.Assembly.Name.csproj'与   消息:C:\ Program Files(x86)\ Microsoft Visual   Studio \ 2017 \ Professional \ MSBuild \ 15.0 \ Bin \ Microsoft.Common.CurrentVersion.targets:   (1603,5):无法执行“ GetReferenceNearestTargetFrameworkTask”任务   从程序集“ C:\ Program Files(x86)\ Microsoft实例化   视觉效果   Studio \ 2017 \ Professional \ Common7 \ IDE \ CommonExtensions \ Microsoft \ NuGet \ NuGet.Build.Tasks.dll”。   请验证任务程序集是使用相同版本构建的   Microsoft.Build.Framework程序集中的一个   计算机,并且您的主机应用程序没有缺少绑定   重定向到Microsoft.Build.Framework。无法转换类型的对象   键入'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask'   'Microsoft.Build.Framework.ITask'。 C:\ Program Files(x86)\ Microsoft   视觉效果   Studio \ 2017 \ Professional \ MSBuild \ 15.0 \ Bin \ Microsoft.Common.CurrentVersion.targets:   (1603,5):已执行“ GetReferenceNearestTargetFrameworkTask”任务   声明或使用不正确,或在构建过程中失败。检查   任务名称和程序集名称的拼写。

这是我正在使用的代码:

var solution = await MSBuildWorkspace.Create().OpenSolutionAsync(@"C:\path\to\solution.sln");
var project = solution.Products.Single(p => p.Name == "ProjectName");
var compilation = await project.GetCompilationAsync();
var diagnostics = compilation.GetDiagnostics()
    .Where(d => !d.IsSuppressed && d.Severity == DiagnosticSeverity.Error)
    .ToArray();
foreach (var diagnostic in diagnostics) { /* print */ }

我在做什么错了?

到目前为止我已经尝试过的事情:

  • 添加外观DLL元数据引用:

        var metadataReferenceDlls = Directory.GetFiles(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\Facades\", "*.dll");
        var metadataReferences = metadataReferenceDlls.Select(f => MetadataReference.CreateFromFile(f)).ToArray();
        foreach (var projectId in solution.ProjectIds.ToArray())
        {
            solution = solution.AddMetadataReferences(projectId, metadataReferences);
        }
    
  • 添加当前的运行时元数据引用(与上面相同,但使用Path.GetDirectoryName(typeof(object).Assembly.Location)作为搜索路径)。 这可行!。但是,由于我仍然必须手动解析所有引用的DLL,因此感觉并不理想。

  • 安装Microsoft.Build.Locator并提前致电MSBuildLocator.RegisterDefaults();

也发布在罗斯林的GitHub上:https://github.com/dotnet/roslyn/issues/29170

编辑:根据要求,这是csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="..\packages\GitLink.3.1.0\build\GitLink.props" Condition="Exists('..\packages\GitLink.3.1.0\build\GitLink.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{C768D1C6-3C45-4170-A4DB-B05B7F833181}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>My.Assembly.Name</RootNamespace>
    <AssemblyName>My.Assembly.Name</AssemblyName>
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <RestorePackages>true</RestorePackages>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
    <TargetFrameworkProfile />
    <CodeAnalysisRuleSet>$(SolutionDir)\StyleCop.ruleset</CodeAnalysisRuleSet>
    <GitLinkEnabled>true</GitLinkEnabled>
    <GitLinkGitRemoteUrl>http://gitlink/{revision}/{filename}</GitLinkGitRemoteUrl>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <DocumentationFile>bin\Debug\My.Assembly.Name.XML</DocumentationFile>
    <NoWarn>1570, 1571, 1572, 1573, 1574, 1580, 1581, 1584, 1587, 1589, 1590, 1591, 1592, 1598, 1710, 1711, 1712, 1723</NoWarn>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="RazorEngine, Version=3.5.0.0, Culture=neutral, PublicKeyToken=9ee697374c7e744a, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\RazorEngine.3.4.2\lib\net45\RazorEngine.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.Entity.Design" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\Microsoft.AspNet.Razor.3.1.1\lib\net45\System.Web.Razor.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <!-- long list of files -->
    <Compile Include="File1.cs" />
    <Compile Include="File2.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config">
      <SubType>Designer</SubType>
    </None>
    <None Include="packages.config">
      <SubType>Designer</SubType>
    </None>
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\My.OtherProject.Name\My.OtherProject.Name.csproj">
      <Project>{96b3bcc3-9052-403e-97ca-44eef392ee15}</Project>
      <Name>My.OtherProject.Name</Name>
    </ProjectReference>
  </ItemGroup>
  <ItemGroup>
    <Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta007\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
    <Analyzer Include="..\packages\StyleCop.Analyzers.1.1.0-beta007\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\GitLink.3.1.0\build\GitLink.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitLink.3.1.0\build\GitLink.props'))" />
    <Error Condition="!Exists('..\packages\GitLink.3.1.0\build\GitLink.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitLink.3.1.0\build\GitLink.targets'))" />
  </Target>
  <Import Project="..\packages\GitLink.3.1.0\build\GitLink.targets" Condition="Exists('..\packages\GitLink.3.1.0\build\GitLink.targets')" />
</Project>

0 个答案:

没有答案