Enable all rules with .NET analyzers in .NET standard projects - c#

I am moving my project to the new .NET Analyzers using Rosyln, previously I was using the nuget package.
I'm trying to do the process described here:
Migrate from FxCop analyzers to .NET analyzers
Now my csproj looks like:
<Project>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
</PropertyGroup>
</Project>
If TargetFramework is net5.0, all rules are return an error, but I cannot make it work when it's set to netstandard2.0.
I cannot move to net5.0 because this dll is referenced by a .NET 4.8 project.

If you add <AnalysisLevel>5</AnalysisLevel> to your project file, you can instruct projects which do not target .NET 5.0 to use the same default rules as a project targeting net5.0 would do by default.
You can find more information about this here.

Since moving to .Net 5 is not an option (where the analyzer is enabled automatically), I would suggest using the nuget packageMicrosoft.CodeAnalysis.CSharp that perform the analysis.

Related

How do I make NuGet aware of a specific version of a package while referencing a multi-targeted class library?

We have three projects:
WebApp.csproj, ASP.NET WebForms app compiled against .NET Framework 2.0
Api.csproj, ASP.NET WebApi 2 compiled against .NET Framework 4.5
Lib.csproj, compiled against .NET Framework 2.0 and referenced by both of the projects above.
Lib.csproj has been recreated in the new .NET project format (the one used by .NET Core) and made to target both .NET Fw 2.0 and 4.5 with the <TargetFrameworks>net20;net45</TargetFrameworks> technique.
So far so good, I can reference different NuGet packages by writing the appropriate sections in Lib.csproj like so:
<ItemGroup Condition="'$(TargetFramework)' == 'net20'">
<PackageReference Include="NLog" Version="2.1.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<PackageReference Include="NLog" Version="4.7.8.0" />
</ItemGroup>
and I can point Api.csproj to the version of Lib.csproj targeting net45, like so:
<ProjectReference Include="..\..\Lib\Lib.csproj" AdditionalProperties="TargetFramework=net45">
<Project>{32e77bcf-152c-4b64-be37-a13f49cdcab6}</Project>
<Name>Lib</Name>
</ProjectReference>
The net20 version references NLog 2.1.0, the net45 one NLog 4.7.8.
The problem: now I want to use NLog 4.7.8 in my Api.csproj, but when I click on Manage Packages for Solution on the solution that includes Api.csproj and Lib.csproj, the referenced version appears to be 2.1.0.
I tried selecting both projects and installing it, however I rightly get an error because NLog 4.7.8 is not compatible with net20.
So really, the problem is that NuGet does not seem to be aware that the project I'm referencing is a multi-target one, and only sees the version of NLog installed in the net20 version even though Api.csproj is targeting the right "flavor" of Lib.csproj (and if I go and check the binaries, the version of NLog.dll that gets copied to the output folders is the right one for each of my targets).
I tried looking on the interwebs, but to no avail.
To be fair: the version of NLog that gets copied to Api.csproj's bin folder is the right one, so it's not a showstopper. But it is mildly annoying, because NuGet shows the wrong version of my dependency, and then it's hard to tell what version we're actually using.
For a bit of context: this app is obviously ancient, at least WebApp.csproj. Due to performance reasons, we are making Api.csproj use async/await, and since that project references code that could be made async in Lib.csproj but obviously wouldn't compile under .NET 2, we chose to make Lib.csproj multi-targeted.
The AdditionalProperties here shouldn't be necessary:
<ProjectReference Include="..\..\Lib\Lib.csproj" AdditionalProperties="TargetFramework=net45">
<Project>{32e77bcf-152c-4b64-be37-a13f49cdcab6}</Project>
<Name>Lib</Name>
</ProjectReference>
I strongly recommend updating all your projects to use SDK-style csproj format if possible. This isn't possible for legacy (pre-Core) web projects, which is the case for WebApp.csproj and Api.csproj. However, even those projects can use some parts of the newer csproj format.
Specifically, they can use PackageReference. I assume your existing web projects are using nuget.config, but they can be changed to use PackageReference without actually making them SDK-style csprojs. NuGet has some issues with transitive dependencies when nuget.config is used, so I just recommend using PackageReference everywhere.

How do I create an SDK style project for a .net framework 4.7.2 project in using visual studio?

I am running the latest version of VS2019 16.6.1
Yet the new project wizard for a windows class library still creates the old style project.
I want to use this method to research this problem
I found some help on converting framework projects here but not on creating new ones from scratch.
The simplest approach in my experience is to create a .NET Core/Standard project for whatever type of application you want (e.g. console or class library) and then edit the csproj file by hand. (You can edit the csproj file by just double clicking on the project in Visual Studio, if it's an SDK-style project.)
So as a concrete example, to create a .NET 4.7.2 class library, I'd choose "Class Library (.NET Standard)" from the list of project templates. That creates a project file like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
Just edit the project to specify the target framework you actually want - so for .NET 4.7.2 you'd use:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
</Project>
(I tend to tidy up the XML at the same time removing the blank lines like that, but you don't have to of course :)

"Unable to find package Microsoft.NETCore.App.Host.osx-x64" after upgrading to .NET Core 3.0

Today, .NET Core 3.0 was released. It became available as an update in my Visual Studio for Mac, and decided to upgrade an ASP.NET Core project from .NET Core 2.2.3 to .NET Core 3.0. I thought it would be as simple as changing the Target framework in the Project Options:
and updating NuGet packages, but when trying to build the project I got the following error:
Unable to find package Microsoft.NETCore.App.Host.osx-x64 with version (= 2.2.3)
Cleaning the solution and restarting Visual Studio didn't help; is there somewhere else I have to change the .NET Core version?
It's not part of the default setup, but during the lifetime of the project I had to add a custom RuntimeFrameworkVersion setting in the .csproj file in order to make a specific package/component work. That setting was still pointing to version 2.2.3. AFAIK, there is no option to change this setting in Visual Studio itself, but if you open the project file you should be able to find it quite easily, right at the top of the file:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RuntimeFrameworkVersion>2.2.3</RuntimeFrameworkVersion>
<PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>
<Version>1.1</Version>
</PropertyGroup>
Removing the fifth line altogether (as recommended by #MartinUllrich in the comments) or changing it to
<RuntimeFrameworkVersion>3.0.0</RuntimeFrameworkVersion>
solved the problem for me.

How Do You Reference a .NET Standard Library from a .NET Framework 4.5 Console Application in Visual Studio 2017?

I have finally installed Visual Studio 2017.2 and am trying to get my first project working, but am running into some trouble that I hope to address here.
I have a very simple .NET Standard Library described as the following project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
</PropertyGroup>
</Project>
And a very simple .NET Framework console application that references the above .NET Standard library, and is described as the following project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
</Project>
When I build my console application, I get the following build error:
C:\Program Files\dotnet\sdk\1.0.4\NuGet.targets(97,5): error : Project Common is not compatible with net45 (.NETFramework,Version=v4.5). Project Common supports: netstandard1.6 (.NETStandard,Version=v1.6)
I saw this question and tried some of the suggestions provided there, but none of them worked. So, this appears to be a different problem. Please note that this occurs during the build of my solution and not referencing (explicit) NuGet packages in any way.
Finally, if it helps, I have a solution that demonstrates this issue here:
https://github.com/Mike-EEE/Stash/blob/master/VS2017.Multi/VS2017.dotNetFramework.sln
.NET Framework 4.5 only supports using .net standard libraries targeting .NET Standard 1.0 or 1.1. Since your library targets 1.6, the tooling does the right thing here and errors out (since your library may use APIs not available in .NET Framework 4.5). If you published the library as NuGet package and consumed it via a package reference, the package restore would error out as well (with an error saying that the package is incompatible).
There is some confusion about which .NET Standard version a .NET Framework version supports especially since there is preview tooling available ("2.0") that changes these versions. The ".NET platforms support" table in the documentation therefore contains two lines about the supported versions. In your case however, both versions limit .NET Framework 4.5 to .NET Standard 1.1.
for .net framework projects to be compatible with .net standard libraries you must acquire the NETStandard.Library from the nuget.
Now i cannot find any official resource that states exactly why this is a must, but from what i understand the NETStandard.Library has the necessary links to make a map from .NET Standard API's to .NET Framework.
If you want more info i suggest to read the official docs of NET Standard.

How to target multiple .NET frameworks with one project and put all versions into a single NuGet package? [duplicate]

I'm trying to build a class library that multi-targets both .NET 4.5.1 and .NET Standard 1.3. According to the documentation, I should be able to do this:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
However, when I try to build, I get these odd errors:
Cannot infer TargetFrameworkIdentifier and/or TargetFrameworkVersion from TargetFramework='net451'. They must be specified explicitly.
MSB3645 .NET Framework v3.5 Service Pack 1 was not found. In order to target ".NETFramework,Version=v1.3", .NET Framework v3.5 Service Pack 1 or later must be installed.
MSB3644 The reference assemblies for framework ".NETFramework,Version=v1.3" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
If I specify the target framework identifiers manually, it builds fine:
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net451'">
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<TargetFrameworkIdentifier>.NETStandard</TargetFrameworkIdentifier>
</PropertyGroup>
I'm using Visual Studio 2017 Community. Am I doing something wrong here?
Have you definitely written
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
and not
<TargetFramework>net451;netstandard1.3</TargetFramework>
?
I was getting the same error until I added the missing s

Categories

Resources