C# Project Reference Warning Type confilict - c#

I have a main asp.net core project which is called "API" and inside of it I have another project called "API.Tests" for xunit tests. So when I'm referencing main project (<ProjectReference Include="..\API.csproj" />), to get access to main's project classes, I'm also referencing everything that's inside of a "API.Tests" project since it's inside of a main project. That's why this warning appears when I'm trying to inherit one class from another in test project:
This is API.csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateProgramFile>false</GenerateProgramFile>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
This is API.Tests.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Nullable>enable</Nullable>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<IsPackable>false</IsPackable>
<TargetFramework>net6.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2"></PackageReference>
<ProjectReference Include="..\API.csproj" />
</ItemGroup>
</Project>
Please help, I don't know how to fix this.

The only thing you can and should do is put the production code in a sub-project as well and reference that sub-project from API.Tests instead of the main project.
i.e.
API
\API.Application
\API.Tests
with API.Tests referencing API.Application.

Related

Cannot read the external namespaces (Microsoft.AspNetCore.MVC...) in Roslyn (in another project it is working)

I have set up one Roslyn analyzer project that is reading properly the (external and internal) namespaces from Symbol (open solution in workspace -> read documents -> read classes -> read class symbol); however, when I created another xUnit project for adding some unit tests, I was not able to read anymore the namespaces that are external to the project (e.g. Microsoft.AspNetCore.Mvc.****).
Does someone know why is this happening? I also tried to run this same project (that was working) in a different machine and it was not reading the external namespaces neither.
I have included the exact same packages in both projects but it is simply not working and showing ErrorTypeSymbol for symbols that have external namespaces.
Note: I open the Solution in the Workspace in the same way on both projects...
Edit: The error message is "This program location is thought to be unreachable.".
Edit 2: It seems like Microsoft.NET.Test.Sdk package is causing the problem and after checking in the Workspace.Diagnostics it shows the following error:
[Failure] Msbuild failed when processing the file '/home/username/RiderProjects/SampleApi/SampleApi/SampleApi.csproj' with message: /usr/share/dotnet/sdk/5.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets: (89, 5): The "ProcessFrameworkReferences" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.Frameworks, Version=5.11.1.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'NuGet.Frameworks, Version=5.11.1.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at Microsoft.NET.Build.Tasks.ProcessFrameworkReferences.ExecuteCore()
at Microsoft.NET.Build.Tasks.TaskBase.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
This is the .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Downgrade to 16.2.0 works fine. But it is not a good "solution".
I created an issue: https://github.com/microsoft/vstest/issues/4212

Unable to create an object of type 'ApplicationDbContext' in asp .net core 5

I made a project with 5 Class libraries in asp .net core 5.
'ApplicationDbContext' class is in the 'Data' Class library and ny models are in the 'Model' class library. when I run 'add-migration' in Package Manager Console, it works properly but when I run 'dotnet ef migrations add' in console, it shows me this error: 'Unable to create an object of type 'ApplicationDbContext''.
Here are the Nuget packages needed:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
1.Open the package management console: Tools->Nuget Package Manager->Package Manager Console
2.cd {path where your csproj file resides}
3.Now type dotnet ef migrations add InitialMigration

C# EF Migrations don't create table

It seems like I kinda broke my project. When I use e.g. add-migration initial to add a new migration, it generates a migration file in the project folder which looks correct, but I dont see it in my solution (in VS 22). When I try to update the database, it just generates the __EFMigrationsHistory -table, but everything else is missing and the command ends with No migrations were applied. The database is already up to date.
It feels like i broke the whole migration thing. Does someone has an idea on this?
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>

Coverage always at 0% on sonarqube even i added test unit

I created a test unit for a class and it passed well on my local, on sonarqube it is shown as 0% for Coverage, I found post advice to add coverlet.msbuild I added but still no news:
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Any idea?
Try adding the following to your .csproj
<PropertyGroup>
<DebugType>Full</DebugType>
</PropertyGroup>
<PackageReference Include="Microsoft.CodeCoverage" Version="16.9.4" />
Can you try coverlet.collector instead of coverlet.msbuild? I have personally used coverlet collector several times successfully.
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
According to this GitHub issue.
Collectors are pretty new(last arrived) and are the best way to do coverage because are strictly integrated with vstest platform(dotnet test) and due to that is the default choice for every .NET core xunit project by Microsoft.
So if you create xunit project Microsoft xunit template inject by design our coverage engine as first class coverage tool running with dotnet test --collect:"XPlat Code Coverage"
It may be possible that you are running into this known issue.

Is there a way to create a .Net Core class library that reference ASP.NEt core without beeing a netcoreapp

We are making a app template that make use of a shared API library with API controllers. (Avaiable from Nuget)
Since API controllers take use of Microsoft.AspNetCore we are forced to make the library target netcoreapp30.
That again forces us to create startup.cs and program.cs in that library to make it compile.
Is there a way to make a class library refernce needed aspnetcore stuff without beeing required to create the startup.cs and program.cs? (compiler complains about missing start point without them)
Startup.cs and program.cs we have in the app using the nuget.
Our project file for the library
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>
</Project>

Categories