Obtain a reference of Microsoft.CodeAnalysis.Project loaded from ProjectFile in netstandard - roslyn

I try to port some code to netstandard so it can run on Linux.
Previosly I used MSBuildWorkspace to optain a workspace and load a Project.
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
Microsoft.CodeAnalysis.Project project = await workspace.OpenProjectAsync(projectPath);
When I switched to netstandard there was no longer a MSBuildWorkspace. How do I get a Project instance file from a .csproj file.
EDIT
That's the project file with all packages I reference.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.5.180" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.6.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.6.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.6.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="2.6.1" />
<PackageReference Include="Microsoft.DotNet.ProjectModel" Version="1.0.0-rc3-1-003177" />
</ItemGroup>
</Project>

MSBuildWorkspace will be included in a seperate package. See this issue

Related

Can ItemGroups in new VisualStudio 2017 csproj files be merged?

For example, I have a .csproj file with the following structure:
...
<ItemGroup>
<Compile Include="...">
<Link>...</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="..." Version="..." />
</ItemGroup>
... there may even be some other tags in between ItemGroups ...
<ItemGroup>
<ProjectReference Include="..." />
</ItemGroup>
<ItemGroup>
<Reference Include="...">
<HintPath>...</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="...">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
... There may be even more additional ItemGroups with other content ...
...
Can I merge them all into one, something like this?
...
<ItemGroup>
<PackageReference Include="..." Version="..." />
<ProjectReference Include="..." />
<Compile Include="...">
<Link>...</Link>
</Compile>
<Reference Include="...">
<HintPath>...</HintPath>
</Reference>
<None Update="...">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
...
I've done it, there seem to be no problem from what I observe at the moment, but I don't want me or other developers to run into them later, not knowing they may be linked to this action.
Merging them is no problem, it is equivalent from the MSBuild API.
However, some tooling may look for item groups containing only specific things (e.g. NuGet wants to add to item groups that already contain package references), but that means that in the worst case, tools may add additional item groups.

Package reference to local when not in release mode

I have a solution W92.Externals with 3 projects inside:
W92.Externals.Domain (netstandard 2.0)
W92.Externals.FunctionApps (netstandard 2.0)
W92.Externals.FunctionApps.Servicebus (netstandard 2.0)
Each of the projects is deployed to NuGet and is used by many other microservices/projects via NuGet.
Now in the solution W92.Externals I decided to use internal dependencies, meaning W92.Externals.FunctionApps will have dependency to W92.Externals.Domain.
The W92.Externals.FunctionApp.csproj looks like this:
<PackageReference Include="W92.Externals.Domain" Version="2019.7.16.139486" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.26" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
Now my goal is to:
1.When I work on my local - take dependencies from W92.Externals.Domain as project dependency, something like this:
<ItemGroup>
<ProjectReference Include="..\..\..\Common\src\W92.Externals\W92.Externals.Domain.csproj" />
</ItemGroup>
2.When changes are deployed (in release mode) the package should be taken from nuget..as it has been shown above:
<PackageReference Include="W92.Externals.Domain" Version="2019.7.16.139486" />
Is it possible somehow to achieve?
In root folder, I have nuget.config file. Maybe somehow can I use it to solve the problem?
I have a project in VS 2017. Version 15.9.12
The projects are in "Microsoft.NET.Sdk".
You should keep the normal ItemGroup to define the package references to normal third-party nuget packages:
<ItemGroup>
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="3.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="3.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.26" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
Then use msbuild conditions in xx.csproj file to manage the dependency to your own W92.Externals.Domain:
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="..\..\..\Common\src\W92.Externals\W92.Externals.Domain.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="W92.Externals.Domain" Version="2019.7.16.139486" />
</ItemGroup>
In this way, I suggest you reload the project every time after you switch between the Debug and Release mode to refresh the UI in solution explorer and avoid mess up the setting.
When you start VS to open this solution, it will read the content of project file and load corresponding references for you.
For example, if you open the solution in debug mode by default, then it will use project reference instead of package reference. It's what you want, but when you switch to release mode, you need to unload the project and reload it in solution explorer to load settings for release mode.

Force project references to be included in netstandard nuget package

I have a netstandard project which includes two project references. Visual studio 2017 is being used to build the nukpg. When the project is built the produced nupkg only contains the assembly produced by that project and lists the two project references as nuget dependencies. Is there a way to force the packaging to include those assemblies as lib files?
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<RootNamespace>Verifier.Observations.DevOps.Health</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<VersionPrefix>1.0.1</VersionPrefix>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Verifier.Observations.Aspects\Verifier.Observations.Aspects.csproj" />
<ProjectReference Include="..\Verifier.Observations\Verifier.Observations.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.Composition"/>
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
Update
Based upon feedback from #alexgiondea-msft the package is now created as desired using the following
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.0.1</VersionPrefix>
<TargetFramework>net462</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<NuspecFile>Verifier.Observations.DevOps.Health.Nuspec</NuspecFile>
<NuspecProperties>version=$(VersionPrefix);id=$(MSBuildProjectName);author=$(Authors);copy=$(Copyright);iconUrl=$(PackageIconUrl)</NuspecProperties>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Verifier.Observations.Aspects\Verifier.Observations.Aspects.csproj" />
<ProjectReference Include="..\Verifier.Observations\Verifier.Observations.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
nuspec
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<iconUrl>$iconUrl$</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Inspect automation service to ensure it is up and operational</description>
<releaseNotes></releaseNotes>
<copyright>$copy$</copyright>
<tags>verifier-observation-plugin automation</tags>
<dependencies>
<group targetFramework="net462" />
</dependencies>
<references>
<group targetFramework="net462">
<reference file="Verifier.Observations.DevOps.Automation.dll" />
</group>
</references>
</metadata>
<files>
<file src="bin\*\net462\*.dll" target="lib\net462" />
<file src="bin\*\net462\*.pdb" target="lib\net462" />
</files>
</package>
You can control where assemblies are deployed in the nuget package using an item in an itemgroup, similar to this:
<ItemGroup>
<None Include="!!path_to_assembly!!">
<PackagePath>lib\net462</PackagePath>
<Pack>true</Pack>
<Visible>false</Visible>
</None>
</ItemGroup>
That should include the specified assembly in the package.
You can add the following target to your .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net47</TargetFrameworks>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" PrivateAssets="all" />
<ProjectReference Include="..\ClassLibrary3\ClassLibrary3.csproj" Condition="'$(TargetFramework)' == 'net47'" PrivateAssets="all" />
</ItemGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
</ItemGroup>
</Target>
</Project>
Source 1
Source 2
Reference: Advanced extension points to create customized package

dotnet core, VS2017 15.5.4 is not publishing static html/js files from Ui folder

dotnet core 2.x, VS2017 15.5.4 is not publishing static html/js files from Ui folder.
We've combined out dotnet core Web Api and our Client UI, Aurelia into the same project. When I publish the Aurelia files are not being copied out, only the files in the Aurelia that are json. I thought I could right click the files and folders and set the Build Action property to Content but I get an error, see last image.
Here is my .csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Web</AssemblyName>
<RootNamespace>Web</RootNamespace>
<TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Graph" Version="1.6.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" PrivateAssets="All" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.00" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\Lms\Lms.Service.csproj" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
Error when right clicking and click Properties from the menu I get this

Unable to Build/Run Multiple Frameworks Using VS 2017's Csproj

I'm using the JavaScript Services Angular infrastructure to build an Angular project that utilizes Entity Framework. I was able to produce a similar project in the past by modifying the project.json file but project settings are now set in the csproj file in VS2017. Below is my code based on Microsoft's recommendations for adapting project.json files to csproj:
<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework> <-- Tried with and without ths line -->
<TargetFrameworks>netcoreapp1.1;net452</TargetFrameworks>
<RuntimeIdentifiers>win7-x64</RuntimeIdentifiers>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<PackageReference Include="EntityFramework" Version="6.1.3" />
<PackageReference Include="Microsoft.CSharp" Version="1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp1.1'">
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
...
On Build I get the following error referencing several files in my project:
Error CS0012 The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
With the <TargetFramework> tag removed, no netcoreapp1.1 references were found.