Aspnetcore: TopLevelParameterNameAnalyzer threw an exception of type System.NullReferenceException.

Created on 4 Oct 2019  路  17Comments  路  Source: dotnet/aspnetcore

Describe the bug

The warning below is emitted when building my project.

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.'.

To Reproduce

  1. Install .NET Core SDK 3.0.100.
  2. git clone [email protected]:twitchax/AspNetCore.Proxy.git
  3. dotnet test

Expected behavior

No warnings.

Screenshots

N/A.

Additional context

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100
 Commit:    04339c3a26

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100\

Host (useful for support):
  Version: 3.0.0
  Commit:  7d57652f33

.NET Core SDKs installed:
  1.0.4 [C:\Program Files\dotnet\sdk]
  1.1.0-preview1-005051 [C:\Program Files\dotnet\sdk]
  1.1.0-preview1-005077 [C:\Program Files\dotnet\sdk]
  1.1.0-preview1-005098 [C:\Program Files\dotnet\sdk]
  1.1.0 [C:\Program Files\dotnet\sdk]
  2.0.0-preview1-005977 [C:\Program Files\dotnet\sdk]
  2.0.0-preview2-006497 [C:\Program Files\dotnet\sdk]
  2.0.0 [C:\Program Files\dotnet\sdk]
  2.0.2-vspre-006949 [C:\Program Files\dotnet\sdk]
  2.0.2 [C:\Program Files\dotnet\sdk]
  2.1.1-preview-007094 [C:\Program Files\dotnet\sdk]
  2.1.1-preview-007165 [C:\Program Files\dotnet\sdk]
  2.1.1 [C:\Program Files\dotnet\sdk]
  2.1.2 [C:\Program Files\dotnet\sdk]
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.100-preview-007326 [C:\Program Files\dotnet\sdk]
  2.1.100-preview-007363 [C:\Program Files\dotnet\sdk]
  2.1.100 [C:\Program Files\dotnet\sdk]
  2.1.200-preview-007474 [C:\Program Files\dotnet\sdk]
  2.1.200-preview-007597 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.301 [C:\Program Files\dotnet\sdk]
  2.1.400-preview-009171 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.600-preview-009426 [C:\Program Files\dotnet\sdk]
  2.1.600-preview-009472 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.2.102 [C:\Program Files\dotnet\sdk]
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.0.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0-preview1-002111-00 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0-preview2-25407-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
analyzer area-mvc bug

Most helpful comment

I have the same problem after migration to core 3, visual studio is 16.3.7

All 17 comments

Well, frustratingly, I opened up the latest VS 2019 Preview, and now the warnings have disappeared.

However, the warnings continue to appear:

  • In WSL with 3.0.100.
  • In an ubuntu docker container with 3.0.100.
  • In my GitHub Action build, which is an ubuntu docker container with 3.0.100.

Is there a delta in the toolchains between the .NET Core 3.0 SDK and the VS 2019 16.3.0 toolchain that would cause this to only appear on a bare machine with .NET Core 3.0, while not appearing on a machine with VS 2019 16.3.0?

@twitchax the test app is using a 3.0.0 of Microsoft.AspNetCore.TestHost which is effectively lifting the tests to compile the app against 3.0.0 versions of ASP.NET Core. Your app is still targeting .NET Core 2.2. Given there were many breaking changes in 3.0, I don't think you want to mix the two versions when testing.

Consider changing the test app to target 2.2.x version of TestHost or migrating your application to .NET Core 3.0. Alternatively, you should be able to suppress the analyzer in the test project.


More details:
The null refs is a result of one of the breaking changes in 3.0 - some types that were formerly pub-internal were moved to a public interface while also changing their namespace. The app's package reference to Microsoft.AspNetCore.Mvc to 2.2.0 gets lifted to the 3.0.0 framework reference in the test app, but the analyzer package remains 2.2.0. You effectively get this:

image

This might fall in to the category of the SDK lifting the 2.2 analyzer package reference to a 3.0 version if it sees a FrameworkReference to Microsoft.AspNetCore.App, but I'm not sure how trivial that is.

While this seems to be a non-supported scenario, let's try to polish up the analyzer to account for this scenarios too.

I have the same problem after migration to core 3, visual studio is 16.3.7

Without disabling the Code Analysis (Project Settings > Code Analysis > Run On Build) this error prevents my project from building in Visual Studio.

VS version: 16.3.9
.NET Core SDK: 3.0.100

As you can see my project after upgrading from .net core 2.2 to 3.0 has references to two different versions of Microsoft.AspNetCore.Mvc.Analyzers
image

@yuchen-w You probably have nuget package which have 2.2.0 as dependency, like:

devenv_2019-11-26_21-16-53

I have a similar issue that get's introduced by a dll that uses entity framework where EF is abstracted from the consuming assembly. It then load analyzers from a backup folder as shown in image bellow
image

I am also seeing the same issue after upgrading to dotnet core 3.1.101.

@PeterPann23 were you able to resolve your issue, I'm also interested in how you established it was introduced via an entity framework dll? so that I could potentially track down where my issue lies.

@OwenPattison
Yes, I have noted that the issue is that code analysis by default does generate the issue by the way it is configured.
2 options:
The first
image
remove the checkboxes, the default is checked and what it installs does not work

The 2nd
Install FxCop package
image
and then
image
update to the new version, no clue why it isn't installing a updated version to start with

From the reports we've seen for this issue, this seems to only occur if you have an app that mixes 3.x shared framework and 2.x packages.

  • If you haven't already looked at our migration guide, https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio, I'd recommend starting there.
  • If you've followed the steps in the migration guide but continue to see this error, please file a separate issue. A resolution for the issue reported here might not your help in your case since it's specific to using 2.x and 3.x binaries in the same application, something which we generally do not support.
  • If this is blocking you from building your applications, you can turn off the analyzers that the WebSDK adds by setting this property in your project file:
<PropertyGroup>
   <DisableImplicitAspNetCoreAnalyzers>true</DisableImplicitAspNetCoreAnalyzers>
</PropertyGroup>

We generally wouldn't recommend doing this since the analyzers will recommend against using problematic code patterns, but getting an application building is much more important.

this is definitely not the case with my projects, they are not migrated to start with and I do not have 2.2 on my station. the issue I have is the default analyzers are wrong on a "file new project"

@PeterPann23 thanks for the feedback, I can confirm i was up to date with analyzers and had those options checked.

I've delved deeper into external dependencies as per @pranavkm comments and can confirm I found a nested package that had a 2.x dependency which seems to have solved the problem for me, thanks for the suggestions.

I'm still getting this message. Under dependencies, I see analyzers for 2.2 and 3.1 which I think is the problem, but I can't figure out how to get rid of the 2.2 analyzers. (The project is targeting .NET Core 3.1)

perhaps have a look at each nuget package and have a look if you have the right ones, sometimes this happens when you have a nuget package added that was suggested by intellisense when actually it's mend as a dependent project,

after that you have a version 2.2 package in a 3.1 solution that actually you do not need as it's coming in the right version from another nuget package.

In my case I had .net core entity framework packages added wher actually I needed to remove all and have only the SQL server nuget and the right dependent core EF loaded and the FXCop issues where okey and the conflict was solved.

Closing this as there are no follow-up items left here. If you hit this issue, read @pranavkm 's comment above: https://github.com/dotnet/aspnetcore/issues/14723#issuecomment-588389938

This page is the top of google for this error.

It would be nice to post here a way to check if you have any pre 3.x .Net Core dependencies.

Or do I just have to expand all my dependency trees in all my projects and try to visually catch any that are referencing the wrong version.

@Vaccano unfortunately there isn't a very good way to do this. The two approaches that I have followed were:

  • to use the search option in Solution Explorer

image

  • or opening up the obj\project.assets.json and looking for packages or projects that reference MVC.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ipinak picture ipinak  路  3Comments

Kevenvz picture Kevenvz  路  3Comments

ermithun picture ermithun  路  3Comments

githubgitgit picture githubgitgit  路  3Comments

guardrex picture guardrex  路  3Comments