Analyzers in .net core are for informing the developers about potential issues in the code. The analyzers have been built into the C# compiler and every framework like asp.net core could have its own analyzers. The analyzers are included in the .NET Core SDK. For example, when you call BuildServiceProvider() of IServiceCollection, it informs you that an additional copy of singleton services will be created by calling it:
It's a built-in analyzer that comes up and tells you that don’t use that method inside the startup code. Let’s make a little change:
After calling the Build() in line 10 of the above picture, we can use GetService() without getting any hint from analyzer. If you have worked with asp.net core in production, you may have seen a lot of conflict and errors in application builder (IApplicationBuilder) priorities:
Such an example by putting the app.UseAuthorization(); before app.UseRouting(); you will get a run time error. This hint is different from the previous one but it helps developers to write the correct code. In fact, this analyzer helps the developer after executing the code! Here is the most used analyzer in asp.net core that helps developers behind the scene:
- BuildServiceProviderAnalyzer
- UseAuthorizationAnalyzer
- DetectMismatchedParameterOptionalityRuleId
- DetectMisplacedLambdaAttribute
- UseMvcAnalyzer
By creating any version of asp.net core you can watch the list of Analyzers under the dependencies in the solution explorer: