简体   繁体   中英

VS2017 MSBuildWorkspace fails opening solution with converted ASP.Net Core Web application

I've set up some solution sanity tests using the Microsoft.CodeAnalysis packages and everything went smooth util I installed VS2017 and coverted my ASP.Net Core Web application. Due to the csproj file simplification ( https://blogs.msdn.microsoft.com/dotnet/2016/12/12/updating-visual-studio-2017-rc-net-core-tooling-improvements/ ) it seems that the solution can't be opened anymore. The code I use is the following:

var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(slnPath).Result;

and this is the exception I'm getting:

Microsoft.Build.Exceptions.InvalidProjectFileException
The default XML namespace of the project must be the MSBuild XML namespace. If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.
   at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject(String errorSubCategoryResourceName, IElementLocation elementLocation, String resourceName, Object[] args)
   at Microsoft.Build.Construction.ProjectParser.Parse()
   at Microsoft.Build.Construction.ProjectParser.Parse(XmlDocumentWithLocation document, ProjectRootElement projectRootElement)
   at Microsoft.Build.Construction.ProjectRootElement.Create(XmlReader xmlReader, ProjectCollection projectCollection)
   at Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.<LoadProjectAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.<LoadProjectFileAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadProjectAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<GetOrLoadProjectAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadSolutionInfoAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenSolutionAsync>d__18.MoveNext()

It seems to me that the new csproj structure is not fully supported. A possible solution may be creating a separate solution without the converted project, but it will be really a workaround. Any ideas?

Edit:

I've tried to follow the suggestion in the error message, namely adding the xmlns attribute like so:

<Project Sdk="Microsoft.NET.Sdk.Web" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <TargetFramework>net452</TargetFramework>
    <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <AssemblyName>Arcmedia.Esl.WebApp.Api</AssemblyName>
    <OutputType>Exe</OutputType>
    <PackageId>Arcmedia.Esl.WebApp.Api</PackageId>
  </PropertyGroup>
  <ItemGroup>
    <None Include="App.config" />
    <Content Update="wwwroot;web.config">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
    <Reference Include="System" />
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\xyz\abc.def.csproj" />
  </ItemGroup>
</Project>

but that didn't solve the problem, because right now I get another exception:

Microsoft.Build.Exceptions.InvalidProjectFileException
The attribute "Update" in element <Content> is unrecognized.
   at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject(String errorSubCategoryResourceName, IElementLocation elementLocation, String resourceName, Object[] args)
   at Microsoft.Build.Internal.ProjectXmlUtilities.ThrowProjectInvalidAttribute(XmlAttributeWithLocation attribute)
   at Microsoft.Build.Internal.ProjectXmlUtilities.VerifyThrowProjectAttributes(XmlElementWithLocation element, String[] validAttributes)
   at Microsoft.Build.Construction.ProjectParser.ParseProjectItemElement(XmlElementWithLocation element, ProjectItemGroupElement parent)
   at Microsoft.Build.Construction.ProjectParser.ParseProjectItemGroupElement(XmlElementWithLocation element, ProjectElementContainer parent)
   at Microsoft.Build.Construction.ProjectParser.ParseProjectRootElementChildren(XmlElementWithLocation element)
   at Microsoft.Build.Construction.ProjectParser.Parse()
   at Microsoft.Build.Construction.ProjectParser.Parse(XmlDocumentWithLocation document, ProjectRootElement projectRootElement)
   at Microsoft.Build.Construction.ProjectRootElement.Create(XmlReader xmlReader, ProjectCollection projectCollection)
   at Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.<LoadProjectAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.<LoadProjectFileAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadProjectAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<GetOrLoadProjectAsync>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadSolutionInfoAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenSolutionAsync>d__18.MoveNext()

So, back to my original conclusion, it seems that the new simplified csproj file structure is not properly supported while trying to use Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.OpenSolutionAsync

The problem is occurring because the project file is using a new schema format for VS2017 csproj, but msbuild is being run from an older install (possibly VS2015) on your machine. This happens because the Roslyn MSBuildWorkspace that you have is built against the prior version of MSBuild API that doesn't understand the new format and is using that version instead of the newer version on your machine. If you didn't have the older version on the machine MSBuildWorkspace would have failed immediately with a type load exception.

MSBuildWorkspace has been update to the newer VS2017 version, but it is not yet on nuget. You can get if from the Roslyn myget feed.

Check out this github issue for more details: https://github.com/dotnet/roslyn/issues/16886

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM