Menu

WORKAROUND: AD0001 Analyzer 'Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer' threw an exception

Hello folks! This is just a quick blog post to show a workaround to https://github.com/dotnet/aspnetcore/issues/14723 (couldn't comment there because the msftbot locked the issue as resolved and limited the conversation to collaborators). I'm currently building a .NET 5 WebAPI project, that references Application Insights between other dependencies, causing that when building the project I'm getting a lot errors like the one below:

CSC : warning AD0001: Analyzer 'Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

As described in the GitHub thread, the cause is because we are mixing dependencies of 5.0 and 2.2 core versions.

githubthread

The proposed workaround in the thread is to completely disable the Code Analysis, but I wanted to find a less dramatic approach.

Workaround

When checking the rules on the Microsoft.AspNetCore.Mvc.Analyzers, found just these 5 rules on the 2.2 assembly:

microsoft-aspnetcore-mvc-analytzers

My approach was to just try disable these rules and allow all the others to run. But surprise, when testing disabling the rules, seems that the one causing the "TopLevelParameterNameAnalyzer" exception was the MVC1004.

Here is the quick workaround:

  1. Add a new item to the WebAPI project
  2. In the search box add "editorconfig" and then select the "editorconfig File (.NET)". Use the ".editorconfig" as name for the file
    editorconfig
  3. Under the [*.{cs,vb}] section, add the rule "dotnet_diagnostic.MVC1004.severity = none" to disable this rule, and save the file

    dotnet-diagnostic
  4. Build the project and voilà, problem solved! I continue getting other code analysis warnings, but not this one.

Hope this helps!

 
 
Posts relacionados
¿Qué es y para qué sirve un TDD o Test Driven Development?
Publicado por Sergio Darias Pérez  |  16 febrero 2023

En este post te explicamos en que consiste el enfoque de TDD o Test Driven Development durante el ciclo de vida del software y cuáles son sus ventajas

Leer más
Descubriendo Extensiones Únicas para Visual Studio Code: Rest Client
Publicado por Héctor García Delgado  |  30 agosto 2022

Cómo probar y trabajar con una API web desde Visual Studio Code gracias a Rest Client. En este post te contamos como funciona paso a paso 😉

Leer más
Qué es la Programación Orientada a Objetos
Publicado por Sergio Darias Pérez  |  26 noviembre 2021

¿Qué es la POO o Programación Orientada a Objetos? Este paradigma de prorgamación, permite que el código sea reutilizable, organizado y sencillo de mantener

Leer más