ElektroStudios
ElektroStudios

Reputation: 20464

How to suppress assembly reference warnings in Visual Studio?

In Visual Studio 2015, I would like to suppress these sort of warnings:

enter image description here

However, they doesn't seem to have a warning identifier, so I can't apply solutions like this.


I understand the warnings but they are not resoluble in a friendlly way so I want to hide them, my project is already handling those assemblies properlly, but I will explain why the warnings are not resoluble:

My solution has two solution configurations, one to compile the solution under .NetFx4 and the other for .NetFx45, for that I use a conditional coompilation constant and also a manual step because I need to manually change the targetting framework of the solution after changing the solution configuration, so the assemblies gives a warning when the solution is configured to target .NetFx4 instead .NetFx45, however, in the solution explorer the assemblies are set to NEVER copy them to the deplyment folder, and also the classes that imports those assemblies are inside conditional compilation constants, so really there is any risk of error.

Upvotes: 1

Views: 2562

Answers (1)

Nicole Calinoiu
Nicole Calinoiu

Reputation: 20982

These are MSBuild warnings, not compiler nor code analysis warnings. You should be able to see the error codes (e.g.: MSB3268) in the Visual Studio output window even if they don't show up in error list.

That said, hiding these is not trivial since neither pragmas nor SuppressMessageattributes will work for these. The MSBuild ResolveAssemblyReference task generates these warnings and, while it does have properties that appear to be intended to help prevent the warnings from being generated, they don't fully work when targeting the v4.0 framework. (Even if they did work, using them also requires overriding the ResolveAssemblyReferences target that is imported via Microsoft.CSharp.targets to push values to the task properties, which would add a long-term maintenance headache).

There is one thing that will actually help here: indicating that the references should not be included when targeting the 4.0 framework. e.g. (in your .csproj file):

<ItemGroup>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  ...
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkVersion)' != 'v4.0'">
  <Reference Include="Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <HintPath>..\packages\Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.4.0.209160138-alpha\lib\net45\Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
    <Private>True</Private>
  </Reference>
  ...
</ItemGroup>

The important bit is the condition on the second ItemGroup, which instructs MSBuild to include the references in the group only when the target framework isn't 4.0.

Since this approach maps quite well to your intent, it seems preferable to suppressing the warnings (even if that were possible). However, there is one little hiccup: the IDE will still generate one "the referenced component could not be found" warning for each of the directly referenced assemblies, and there doesn't appear to be anything that can be done to prevent this. On the other hand, one warning per directly referenced excluded assembly is already quite a bit better than one warning for each framework assembly referenced by each of the directly referenced assemblies...

Upvotes: 3

Related Questions