How to find exact version selected by nuget for a PackageReference? - .net-core

I can refer NuGet packages in .NET Core using
<PackageReference Include="ExamplePackage" Version="6.*" />
How to find the exact version of ExamplePackage used during the build?

Related

Error running a brand new Blazor App with .NET Core 6.0

In Visual Studio 2022, I create a new Blazor WebAssembly App. VS does its thing and scaffolds the project with a .sln file. I hit the Run button and I'm met with error CS0246: The type or namespace name 'MyBlazorApp' could not be found (are you missing a using directive or an assembly reference?)
Interestingly, this doesn't show up in my Error List window, just as a build output. Googling is only showing me some people having a similar problem with older versions of .NET Core, and the solution in most cases is to update VS2019. Is it possible this is the same bug cropping up again in a new version of VS?
This is not the answer per se, but as usual I need more than the comment space available:
Check what version of dotnet you are running run : dotnet --version.
I have:
PS C:\Users\shaun\Documents\GitHub\Blazr\Blazr.Demo Sites\MyBlazorApp> dotnet --version
6.0.101
PS C:\User
Downgrade the packages in the project file and check whether the project builds and runs.
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
</ItemGroup>
</Project>

How to force Swashbuckle.Aspnetcore.Cli to use netcoreapp3.1?

I'm upgrading a project to use .Net Core 3.1 from 2.2, and am struggling with getting my tools working.
I have this section in my .csproj file which generates a swagger.json file at publish time - it gets executed in our build pipeline and published as an artifact.
<ItemGroup>
<DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="5.2.1" />
</ItemGroup>
<Target Name="Swagger" AfterTargets="PrepareForPublish" DependsOnTargets="Build" Outputs="$(PublishDir)swagger.json">
<Exec Command="dotnet swagger tofile --output $(PublishDir)swagger.json $(OutputPath)\$(AssemblyName).dll v1" />
</Target>
But I'm getting these errors:
Package Swashbuckle.AspNetCore.Cli 5.2.1 is not compatible with netcoreapp2.2 (.NETCoreApp,Version=v2.2). Package Swashbuckle.AspNetCore.Cli 5.2.1 supports:
- netcoreapp2.1 (.NETCoreApp,Version=v2.1) / any
- netcoreapp3.0 (.NETCoreApp,Version=v3.0) / any
and:
Invalid project-package combination for Swashbuckle.AspNetCore.Cli 5.2.1. DotnetToolReference project style can only contain references of the DotnetTool type
The weird thing is that my project is using 3.1 - I did a ctrl+f on both "netcoreapp2.2" and "2.2" and couldn't find anywhere that was being specified. How can I force my CLI tool to use the correct .NET Core version?
I had the same problem and resolved it by following the installation steps described here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#retrieve-swagger-directly-from-a-startup-assembly, and by removing the ItemGroup of the project file:
<ItemGroup>
<DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="5.2.1" />
</ItemGroup>
I had the same issue when trying to use dotnet swagger. I switched to using just swagger and that fixed the issue. And as #Mike said, remove the ItemGroup from the project file and install the cli globally as outlined here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#retrieve-swagger-directly-from-a-startup-assembly

Shipping projects as NuGet packages while depending on each other

I have three projects I'm building
FooBar.Abstractions
FooBar.AspNetCore
FooBar.AspNetCore.IntegrationTesting
The FooBar.AspNetCore and the FooBar.AspNetCore.IntegrationTesting projects both have references to FooBar.Abstractions. I want to package and ship all three of these as individual NuGet packages.
I started with a NuGet.config file that looks like this locally:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="aspnetcore_abstractions" value="./src/FooBar.Abstractions/bin/Debug/" />
</packageSources>
</configuration>
Then I add the package to my projects
<ItemGroup>
<PackageReference Include="FooBar.Abstractions" Version="2.0.0-preview1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
</ItemGroup>
This really sucks though as each time I make a change in the FooBar.Abstractions project I have to go into my C:\.nuget\packages folder and delete the cache before my FooBar.AspNetCore project can restore the newly compiled version from my solution.
If I just add FooBar.Abstractions as a project reference, and then I ship the two packages to NuGet.org, how does that affect users that install the two packages across different projects in their solutions. Does NuGet and .Net figure it all out, knowing that they're the same referenced assembly? I assume in this case the FooBar.AspNetCore project will ship with the FooBar.Abstractions.dll in it if I add it as a project reference.
I don't know if that causes conflicts knowing that the package ships that .dll, then a customer installs the Abstractions package explicitly that contains the same .dll.
How do you handle this with NuGet packaging with the newest versions of NuGet? How do I constrain FooBar.AspNetCore to use the same FooBar.Abstractions.dll version between the package reference and the NuGet package others will install? I can't force PackageReference Include="FooBar.Abstractions" Version="2.2" if I'm adding it as a project reference instead can I?
When you pack a project with at project reference, NuGet converts the project reference into a NuGet dependency. It figures out the dependency version based on what version that project would be if it were packed. There is no need to use PackageReference when packing. As you discovered/explained, doing so makes local development much more difficult.
Therefore the solution to your problem is to just use ProjectReference when the projects are in the same source code repository.

use Swagger in dot net core api framework 2

How to use Swagger in asp.net core API.
i used core framework-2.
i try
Swashbuckle.AspNetCore
package, but it's not supported in framework-2.
I had the same issue and could be able to fix it by installing Microsoft.AspNetCore.StaticFiles Nuget package.
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/438

How do I specify dependencies in a .NET Standard Class Library?

A .NET Standard Class Library doesn't use a .nuspec file; there is a "Package" tab in the project settings where you enter all the metadata for the NuGet package. These go in the .csproj file.
I want to create a solution with projects A and B, where project B depends on project A. Both need to go on NuGet as separate packages. But if someone installs the NuGet package for B, it should also install the package for A.
How do you specify this simple dependency for a .NET Standard Class Library? I'm not assuming that adding a reference will be enough to do this.
NET Standard .csproj files use PackageReference and ProjectReference MSBuild items to control dependencies on packages loaded from NuGet or your solution, respectively. They both support metadata tags PrivateAssets, IncludeAssets, ExcludeAssets, which control what exactly and how your project depends on in those dependencies. By default, all dependencies are auto-generated by the Visual Studio with PrivateAssets, which means those dependencies are consumed, but they're not marked as dependencies for consumers of your project (which is gonna be a package in NuGet).
So, you just need to replace the PrivateAssets metadata with the IncludeAssets (maybe with proper values).
For example, my SuperPackage.csproj file looks like below:
..................
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
<IncludeAssets>compile</IncludeAssets>
</ProjectReference>
</ItemGroup>
</Project>
So, I have the PackageReference to the 'Newtonsoft.Json' package from NuGet marked as a dependency, and the ProjectReference to the ClassLibrary1 project within the single solution.
When the SuperPackage project is built with checked 'Generate NuGet package on build', I get the following SuperPackage.nupkg:
Check this doc for more details: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files
From what I have found it is not yet supported in .csproj files and you should use .nuspec to specify dependencies. Have a look how they do it in xUnit, for example.

Resources