How to assign the version of a specific PackageReference to a property - msbuild

I am trying to get the version of a specific package that is referenced through the PackageReference, so that I can reference a tool that comes inside one of those packages. I am able to write the version using a Message task:
<Message Importance="High" Text="#(PackageReference->%(PackageReference.Version))" Condition="'%(PackageReference.Identity)' == 'Google.Protobuf.Tools'" />
How can I retrieve that value inside a property?
The relevant parts from my csproj looks like this:
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.5.0" />
<PackageReference Include="Google.Protobuf.Tools" Version="3.5.0" />
<PackageReference Include="Grpc" Version="1.8.0" />
<PackageReference Include="Grpc.Tools" Version="1.8.0" />
</ItemGroup>
<PropertyGroup>
<ProtobufCompiler>$(UserProfile)/.nuget/packages/Google.Protobuf.Tools/3.5.0/tools/windows_x64/protoc.exe</ProtobufCompiler>
<GrpcCSharpPlugin>$(UserProfile)/.nuget/packages/Grpc.Tools/1.8.0/tools/windows_x64/grpc_csharp_plugin.exe</GrpcCSharpPlugin>
</PropertyGroup>

I found that I can use the CreateProperty task for that:
<CreateProperty Value="%(PackageReference.Version)" Condition="%(PackageReference.Identity) == 'Google.Protobuf.Tools'">
<Output TaskParameter="Value" PropertyName="GoogleProtobufToolsVersion" />
</CreateProperty>

Latest Grps.Tools calls proto compiler during the build.
<ItemGroup>
<PackageReference Include="Grpc.Tools" Version="2.31.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Protobuf Include="my.proto" GrpcServices="None">
</Protobuf>

Related

Why I am getting there was an error running the selected code generator method not found?

I am new to razor pages.
I am fowlling the tutorial
https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio
When I try to add Right-click on the Pages/Movies folder > Add > New Scaffolded Item.
I am getting the following error :
There was an error running the selected code generator Method not found void ?
Here are my packages refrences :
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="cloudscribe.Core.Storage.EFCore.MSSQL" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-preview.7.22376.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration" Version="6.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages\Movies\" />
</ItemGroup>
</Project>
I implemented the soultion in the there was an error running the selected code generator in .net6
Now I am getting another error:
There was an error runinig the selected code generator scafolding faild in type microsoft.CodeAnalysis.CSharp ..

How to exclude folders from Visual Studio 2022 WebDeploy?

I don't manage to exclude certain folders from Visual Studio 2022 WebDeploy (publish to IIS).
There is a bunch of folders that I do not want to be published to my server (e.g. jquery, bootstrap-icons etc.).
I followed this post and ended up with a new xml file. The file is called [MyProjectName].wpp.targets.xml and it is located in the same folder as my [MyProjectName].csproj.
The file looks like this:
<Project ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ExcludeFromPackageFolders
Include="wwwroot\lib\jquery;
wwwroot\lib\jquery-validate;
wwwroot\lib\jquery-validation-unobtrusive;
wwwroot\lib\jquery-ajax-unobtrusive;
wwwroot\lib\popper.js;
wwwroot\lib\bootstrap-icons">
</ExcludeFromPackageFolders>
</ItemGroup>
On my next Publish, however, all those folders (and included files) are still being copied to my server.
What am I doing wrong?
I am using Visual Studio Community 2022, V.17.1.5, with a .NET 6 application.
Edit your {project_name}.csproj file and add the following lines...
<ItemGroup>
<Folder Include="wwwroot\Files\" Exclude="Files\**\*" />
</ItemGroup>
With "wwwroot\Files" being the location of the folder with the files you want to exclude.
This can be anywhere inside your top level declaration.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.32" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.32" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
<PackageReference Include="System.Linq" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PCS.BusinessServices\PCS.BusinessServices.csproj" />
<ProjectReference Include="..\PCS.Common\PCS.Common.csproj" />
<ProjectReference Include="..\PCS.Data.Services\PCS.Data.Services.csproj" />
<ProjectReference Include="..\PCS.Data\PCS.Data.csproj" />
<ProjectReference Include="..\PCS.Encryption\PCS.Encryption.csproj" />
<ProjectReference Include="..\PCS.PdfEngine\PCS.PdfEngine.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\Files\" Exclude="Files\**\*" />
</ItemGroup>
</Project>

IWebHostEnvironment .Net 5 not found

I am using .Net 5 class library project and here are my refrences. I am using
using Microsoft.AspNetCore.Hosting; But I cannot find IWebHostEnvironment. What wrong am I doing ?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.12.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Microsoft.Graph" Version="3.19.0" />
<PackageReference Include="Microsoft.Graph.Core" Version="1.22.0" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="5.2.8" />
<PackageReference Include="Microsoft.ReportViewer.WebForms" Version="10.0.40219.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
<PackageReference Include="System.ServiceModel.Http" Version="4.8.1" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.8.1" />
<PackageReference Include="WindowsAzure.Storage" Version="9.3.3" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ServiceModel" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>
On .Net5.0, IWebHostEnvironment has been replaced by IHostEnvironment
Make sure you have the package Microsoft.Extensions.Hosting installed and the following import on the top of the file.
using Microsoft.Extensions.Hosting;
More information can be seen at aspnetcore github discussion
IWebHostEnvironment is included in the Microsoft.AspNetCore.Hosting package, you simply need to add it as a reference to your project by right clicking on your project file and selecting 'Manage Nuget Packages' then searching for Microsoft.AspNetCore.Hosting and add it.
If you've already added it and it still isn't working, try cleaning your project.

MSBuild not assigning version number

I am using the following command in azure pipelines to apply a PackageVersion to the outputted DLLs. The PackageVersion is not coming through and I can't tell if it's because another configuration is overwritting it or something else. The PackageVersion (the AssemblyInfoVersion in c# is always the default 1.0.0)
- task: MSBuild#1
displayName: 'Build solution eCommerceAPI.sln'
inputs:
solution: eCommerceAPI.sln
msbuildArchitecture: x64
configuration: Release
msbuildArguments: '/p:DebugType=none /p:DebugSymbols=false /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:DeployAsIisApp=False /p:DeployIisAppPath="Default Web Site/eCommerceAPI" /p:PackageLocation=$(Build.ArtifactStagingDirectory)"\eCommerceAPI.zip" /p:PackageVersion=$(Build.BuildNumber)'
Including the csproj as requested:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.5" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
<PackageReference Include="JWT" Version="6.1.4" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.5" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DocumentationFile>$(MSBuildThisFileName).xml</DocumentationFile>
<UserSecretsId>11111111-1111-1111-1111-111111111111</UserSecretsId>
</PropertyGroup>
</Project>
The PackageVersion does not update the versions associated with the assemblies but the Version parameter will assign the version to all assemblies.

How to exclude Microsoft.CodeAnalysis.* DLLs from publishing

I've written .NET Standard 2.1 library component that relies on Microsoft.AspNetCore.Mvc v2.2.0 package, which indirectly references Microsoft.CodeAnalysis.CSharp.dll, MicrosoftCode.Analysis.dll, and Microsoft.CodeAnalysis.Razor.dll. When I reference my component from .NET Core 3.1 app site and publish the site, Microsoft.CodeAnalysis.* dlls are also published. Is there a way to exclude Microsoft.CodeAnalysis.* dlls from being published? I don't need them for production.
Here are my references from .NET Standard 2.1 CSPROJ file:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="3.1.8" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
Thanks in advance!
It looks like the issue comes from referencing the AspNetCore packages directly.
If you can change your class library to target netcoreapp3.1 instead of netstandard, you can replace those by using a FrameworkReference instead. Note that FrameworkReference is only valid for assemblies targetting Net Core 3.x and up. Reference
:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
Alternatively, if you need to support both netstandard and netcoreapp, you can use conditional references.
<!-- Framework reference for netcoreapp -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<!-- Common dependencies -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<!-- netstandard dependencies -->
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="3.1.8" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.1.8" />
</ItemGroup>
Note that I had to guess which package references your package library need, so you'll need to adapt as needed