EnvironmentAbstractions.BannedApiAnalyzer 5.0.0

dotnet add package EnvironmentAbstractions.BannedApiAnalyzer --version 5.0.0                
NuGet\Install-Package EnvironmentAbstractions.BannedApiAnalyzer -Version 5.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EnvironmentAbstractions.BannedApiAnalyzer" Version="5.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EnvironmentAbstractions.BannedApiAnalyzer --version 5.0.0                
#r "nuget: EnvironmentAbstractions.BannedApiAnalyzer, 5.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EnvironmentAbstractions.BannedApiAnalyzer as a Cake Addin
#addin nuget:?package=EnvironmentAbstractions.BannedApiAnalyzer&version=5.0.0

// Install EnvironmentAbstractions.BannedApiAnalyzer as a Cake Tool
#tool nuget:?package=EnvironmentAbstractions.BannedApiAnalyzer&version=5.0.0                

EnvironmentAbstractions.BannedApiAnalyzer

Nuget (with prereleases)

The EnvironmentAbstractions.BannedApiAnalyzer package uses the Microsoft.CodeAnalysis.BannedApiAnalyzers package to prevent usages of System.Environment to access the environment.

Adding a <PackageReference /> to the package in your project is all you need:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="EnvironmentAbstractions.BannedApiAnalyzer" Version="1.0.0" />
  </ItemGroup>
</Project>

Then when anyone attempts to call the built-in methods for accessing the environment, they'll receive a build warning indicating that they should use IEnvironmentProvider instead:

public static void Main(string[] args)
{
    Console.WriteLine("Hello, {0}!", System.Environment.UserName);
}
warning RS0030: The symbol 'Environment.UserName' is banned in this project: Use IEnvironmentProvider.UserName instead.

If you want to only ban access to environment variables, you'll need to first set the MSBuild property BanSystemEnvironmentVariableAPIs to true in your project:

<PropertyGroup>
  
  <BanSystemEnvironmentVariableAPIs>true</BanSystemEnvironmentVariableAPIs>
</PropertyGroup>

Then only accessing environment variables will emit a compile-time error telling developers to use IEnvironmentVariableProvider instead:

public static void Main(string[] args)
{
    Console.WriteLine("Hello, {0}!", System.Environment.GetEnvironmentVariable("USERNAME"));
}
warning RS0030: The symbol 'Environment.GetEnvironmentVariable(string)' is banned in this project: Use IEnvironmentVariableProvider.GetEnvironmentVariable(string) instead.
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EnvironmentAbstractions.BannedApiAnalyzer:

Package Downloads
File.TestLogger

Provides a logger for the Visual Studio Test Platform that writes output to a file.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EnvironmentAbstractions.BannedApiAnalyzer:

Repository Stars
microsoft/slngen
Visual Studio solution generator
Version Downloads Last updated
5.0.0 632 9/9/2024
4.0.0 5,980 5/28/2024
3.0.2 1,046 11/15/2023
2.1.0 565 9/26/2023