├── .gitignore
├── README.md
├── BannedApiAnalyzers
├── appsettings.Development.json
├── appsettings.json
├── BannedApiAnalyzers.csproj.user
├── Program.cs
├── WeatherForecast.cs
├── BannedApiAnalyzers.csproj
├── Properties
│ └── launchSettings.json
├── Controllers
│ └── WeatherForecastController.cs
├── Startup.cs
└── BannedSymbols.txt
├── LICENSE
└── BannedApiAnalyzers.sln
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | .vs
3 | .vscode
4 | bin
5 | obj
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BannedApiAnalyzersDemo
2 | A basic demo on how to use Microsoft.CodeAnalysis.BannedApiAnalyzers in a .NET Core project
3 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "AllowedHosts": "*"
10 | }
11 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/BannedApiAnalyzers.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ProjectDebugger
5 |
6 |
7 | BannedApiAnalyzers
8 |
9 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Hosting;
2 | using Microsoft.Extensions.Hosting;
3 | using System;
4 |
5 | namespace BannedApiAnalyzers
6 | {
7 | public class Program
8 | {
9 | public static void Main(string[] args)
10 | {
11 | Console.WriteLine("I'm starting!!");
12 | CreateHostBuilder(args).Build().Run();
13 | }
14 |
15 | public static IHostBuilder CreateHostBuilder(string[] args) =>
16 | Host.CreateDefaultBuilder(args)
17 | .ConfigureWebHostDefaults(webBuilder =>
18 | {
19 | webBuilder.UseStartup();
20 | });
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/WeatherForecast.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BannedApiAnalyzers
4 | {
5 | public class WeatherForecast
6 | {
7 | public DateTime Date { get; set; }
8 |
9 | public int TemperatureC { get; set; }
10 |
11 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
12 |
13 | public string Summary { get; set; }
14 |
15 | public WeatherForecast()
16 | {
17 | }
18 |
19 | public WeatherForecast(DateTime dateTime, int temperatureC, string summary)
20 | {
21 | Date = dateTime;
22 | TemperatureC = temperatureC;
23 | Summary = summary;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/BannedApiAnalyzers.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 | true
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | all
16 | runtime; build; native; contentfiles; analyzers; buildtransitive
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:1109",
8 | "sslPort": 44300
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "swagger",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "BannedApiAnalyzers": {
21 | "commandName": "Project",
22 | "dotnetRunMessages": "true",
23 | "launchBrowser": true,
24 | "launchUrl": "swagger",
25 | "applicationUrl": "https://localhost:5001;http://localhost:5000",
26 | "environmentVariables": {
27 | "ASPNETCORE_ENVIRONMENT": "Development"
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Raul Piraces Alastuey
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31205.134
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BannedApiAnalyzers", "BannedApiAnalyzers\BannedApiAnalyzers.csproj", "{A0D03908-282B-4BB2-8E37-22935369F249}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {A0D03908-282B-4BB2-8E37-22935369F249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {A0D03908-282B-4BB2-8E37-22935369F249}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {A0D03908-282B-4BB2-8E37-22935369F249}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {A0D03908-282B-4BB2-8E37-22935369F249}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {2E73D69A-A770-42ED-9CF3-24EBF5364129}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/Controllers/WeatherForecastController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.Extensions.Logging;
3 | using System;
4 | using System.Collections.Generic;
5 |
6 | namespace BannedApiAnalyzers.Controllers
7 | {
8 | [ApiController]
9 | [Route("[controller]")]
10 | public class WeatherForecastController : ControllerBase
11 | {
12 | private static readonly string[] Summaries = new[]
13 | {
14 | "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
15 | };
16 |
17 | private readonly ILogger _logger;
18 |
19 | public WeatherForecastController(ILogger logger)
20 | {
21 | _logger = logger;
22 | }
23 |
24 | [HttpGet]
25 | public IEnumerable Get()
26 | {
27 | var rng = new Random(1);
28 | var weatherForecast = new WeatherForecast(); // No reason to initialize the object here and set the properties later
29 | weatherForecast.Date = DateTime.Now;
30 | weatherForecast.TemperatureC = rng.Next(-20, 55);
31 | weatherForecast.Summary = Summaries[rng.Next(Summaries.Length)];
32 | Activator.CreateInstance(); // Reflection in .NET is very powerful, but expensive
33 | _logger.LogInformation($"Temperature for date {weatherForecast.Date} is {weatherForecast.TemperatureF}"); // Deprecated use of property
34 | return new []{ weatherForecast };
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/Startup.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Builder;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Configuration;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using Microsoft.Extensions.Hosting;
6 | using Microsoft.OpenApi.Models;
7 |
8 | namespace BannedApiAnalyzers
9 | {
10 | public class Startup
11 | {
12 | public Startup(IConfiguration configuration)
13 | {
14 | Configuration = configuration;
15 | }
16 |
17 | public IConfiguration Configuration { get; }
18 |
19 | // This method gets called by the runtime. Use this method to add services to the container.
20 | public void ConfigureServices(IServiceCollection services)
21 | {
22 |
23 | services.AddControllers();
24 | services.AddSwaggerGen(c =>
25 | {
26 | c.SwaggerDoc("v1", new OpenApiInfo { Title = "BannedApiAnalyzers", Version = "v1" });
27 | });
28 | }
29 |
30 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
31 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
32 | {
33 | if (env.IsDevelopment())
34 | {
35 | app.UseDeveloperExceptionPage();
36 | app.UseSwagger();
37 | app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BannedApiAnalyzers v1"));
38 | }
39 |
40 | app.UseHttpsRedirection();
41 |
42 | app.UseRouting();
43 |
44 | app.UseAuthorization();
45 |
46 | app.UseEndpoints(endpoints =>
47 | {
48 | endpoints.MapControllers();
49 | });
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/BannedApiAnalyzers/BannedSymbols.txt:
--------------------------------------------------------------------------------
1 | M:System.Console.WriteLine;Don't use console for logging
2 | M:System.Console.Write(System.Int32);Don't use Console.Write for logging.
3 | M:System.Console.Write(System.Int64);Don't use Console.Write for logging.
4 | M:System.Console.Write(System.UInt32);Don't use Console.Write for logging.
5 | M:System.Console.Write(System.UInt64);Don't use Console.Write for logging.
6 | M:System.Console.Write(System.Boolean);Don't use Console.Write for logging.
7 | M:System.Console.Write(System.Char);Don't use Console.Write for logging.
8 | M:System.Console.Write(System.Char[]);Don't use Console.Write for logging.
9 | M:System.Console.Write(System.Char[],System.Int32,Symstem.Int32);Don't use Console.Write for logging.
10 | M:System.Console.Write(System.Single);Don't use Console.Write for logging.
11 | M:System.Console.Write(System.Double);Don't use Console.Write for logging.
12 | M:System.Console.Write(System.Object);Don't use Console.Write for logging.
13 | M:System.Console.Write(System.String);Don't use Console.Write for logging.
14 | M:System.Console.Write(System.String,System.Object);Don't use Console.Write for logging.
15 | M:System.Console.Write(System.String,System.Object,System.Object);Don't use Console.Write for logging.
16 | M:System.Console.Write(System.String,System.Object,System.Object,System.Object);Don't use Console.Write for logging.
17 | M:System.Console.Write(System.String,System.Object[]);Don't use Console.Write for logging.
18 | M:System.Console.Write(System.Decimal);Don't use Console.Write for logging.
19 | M:System.Console.WriteLine;Don't use Console.WriteLine for logging.
20 | M:System.Console.WriteLine(System.Int32);Don't use Console.WriteLine for logging.
21 | M:System.Console.WriteLine(System.Int64);Don't use Console.WriteLine for logging.
22 | M:System.Console.WriteLine(System.UInt32);Don't use Console.WriteLine for logging.
23 | M:System.Console.WriteLine(System.UInt64);Don't use Console.WriteLine for logging.
24 | M:System.Console.WriteLine(System.Boolean);Don't use Console.WriteLine for logging.
25 | M:System.Console.WriteLine(System.Char);Don't use Console.WriteLine for logging.
26 | M:System.Console.WriteLine(System.Char[]);Don't use Console.WriteLine for logging.
27 | M:System.Console.WriteLine(System.Char[],System.Int32,Symstem.Int32);Don't use Console.WriteLine for logging.
28 | M:System.Console.WriteLine(System.Single);Don't use Console.WriteLine for logging.
29 | M:System.Console.WriteLine(System.Double);Don't use Console.WriteLine for logging.
30 | M:System.Console.WriteLine(System.Object);Don't use Console.WriteLine for logging.
31 | M:System.Console.WriteLine(System.String);Don't use Console.WriteLine for logging.
32 | M:System.Console.WriteLine(System.String,System.Object);Don't use Console.WriteLine for logging.
33 | M:System.Console.WriteLine(System.String,System.Object,System.Object);Don't use Console.WriteLine for logging.
34 | M:System.Console.WriteLine(System.String,System.Object,System.Object,System.Object);Don't use Console.WriteLine for logging.
35 | M:System.Console.WriteLine(System.String,System.Object[]);Don't use Console.WriteLine for logging.
36 | M:System.Console.WriteLine(System.Decimal);Don't use Console.WriteLine for logging.
37 |
38 | M:System.Activator.CreateInstance(System.String, System.String);Avoid using reflection
39 | M:System.Activator.CreateInstance`1();Avoid using reflection
40 |
41 | M:System.Random.#ctor;Use another random source
42 | M:System.Random.#ctor(System.Int32);Use another random source
43 |
44 |
45 | P:BannedApiAnalyzers.WeatherForecast.TemperatureF;We should not use this property.
46 | M:BannedApiAnalyzers.WeatherForecast.#ctor;Avoid using the WeatherForecast empty constructor.
47 |
--------------------------------------------------------------------------------