IoCTools.Abstractions
1.7.3
See the version list below for details.
dotnet add package IoCTools.Abstractions --version 1.7.3
NuGet\Install-Package IoCTools.Abstractions -Version 1.7.3
<PackageReference Include="IoCTools.Abstractions" Version="1.7.3" />
<PackageVersion Include="IoCTools.Abstractions" Version="1.7.3" />
<PackageReference Include="IoCTools.Abstractions" />
paket add IoCTools.Abstractions --version 1.7.3
#r "nuget: IoCTools.Abstractions, 1.7.3"
#:package IoCTools.Abstractions@1.7.3
#addin nuget:?package=IoCTools.Abstractions&version=1.7.3
#tool nuget:?package=IoCTools.Abstractions&version=1.7.3
IoCTools
A Roslyn source generator that lets each service declare its own lifetime, dependencies, and registration intent with small attributes. IoCTools emits constructors, service registrations, and analyzers at build time—no reflection, no runtime scanning.
Highlights
- Self-describing services –
[Scoped],[DependsOn<T>],[RegisterAs<…>], and[ConditionalService]live on the class, so intent never leaves the type - Dependency sets – Implement
IDependencySetto reuse dependency bundles across services - Inheritance-aware – Derived services inherit base class lifetime; diagnostics validate across the full chain
- 100+ diagnostics – Build-time validation catches missing lifetimes, circular dependencies, lifetime mismatches, open-generic edge cases, and FluentValidation anti-patterns
- Zero reflection – Everything happens at compile time; generated code is plain C# you can inspect
Authoring Posture
[Inject]is deprecated in 1.6.0 and emitsIOC095. A Roslyn code fix plusioc-tools migrate-injectmigrate in bulk. Scheduled for removal in 2.0.[DependsOn<T>]is the source-of-truth for explicit service dependencies.[DependsOnConfiguration<T>]/[DependsOnOptions<T>]for config dependencies.InjectConfigurationstays supported but is not preferred.- Auto-deps (1.6.0+) declare ambient dependencies once per assembly:
[assembly: AutoDep<T>],[assembly: AutoDepOpen(typeof(ILogger<>))], plusMicrosoft.Extensions.Logging.ILogger<T>is auto-detected with zero configuration. See docs/auto-deps.md.
Installation
dotnet add package IoCTools.Abstractions
dotnet add package IoCTools.Generator
Or directly in your project file:
<ItemGroup>
<PackageReference Include="IoCTools.Abstractions" Version="*" />
<PackageReference Include="IoCTools.Generator" Version="*" PrivateAssets="all" />
</ItemGroup>
What's New in v1.7.3
Fixed in 1.7.3
- IOC997 crash on array
TypedConstantargs fixed —TestFixtureAnalyzerandAttributeParsernow guard against calling.Valueon Array-kindedTypedConstants, which occurs when aDependsOn-prefixed attribute is called with multipletypeof()args (e.g.[DependsOnExtras(typeof(IFoo), typeof(IBar))]). - TDIAG04 false positive for
[GeneratedCode("IoCTools")]constructors fixed —HasConstructorGenerationIntentnow recognizes constructors tagged by IoCTools in a prior pass, preventing false TDIAG04 on KeelLoggedHandler<T>subclasses.
Fixed in 1.7.2
[Cover<T>]source compatibility restored —IoCTools.Testingnow injectsIoCTools.Testing.Annotationsas a global using via its MSBuild.targetsfile. Consumers usingPrivateAssets="all"with no explicitusingdirective no longer receive CS0246 onCover<>,CoverAttribute<>, orFixtureLoggerProfile.- IOC997 crash on scalar
nameof()args fixed —TestFixtureAnalyzernow correctly handlesparams string[]attribute arguments stored as scalarTypedConstantby the C# compiler (e.g.[ProjectSignalFrom<T>(nameof(T.Id))]). - TDIAG08 downgraded to
Info— no longer blocks compilation inTreatWarningsAsErrors=trueprojects. Escalate with<IoCToolsTestingDiagnosticSeverity>Warning</IoCToolsTestingDiagnosticSeverity>for strict mode.
Features introduced in v1.7.x (all in 1.7.2)
v1.7.0 was tagged but never published to NuGet — CI failed in the CLI fixture-evidence build step before the publish job ran. v1.7.1 is the first published release with the 1.7 features, but had consumer regressions fixed in 1.7.2.
AnalysisScopemodel +DiagnosticGate— each diagnostic declares whether it fires in production projects, test projects, or both.IOC081/IOC082/IOC086are reclassified asAnalysisScope.Production; test projects are auto-exempt via theIsTestProjectMSBuild property (forwarded as aCompilerVisiblePropertyfromMicrosoft.NET.Test.Sdk). No naming heuristics.- IOC110 (Warning, configurable via
IoCToolsLifetimeValidationSeverity) — deterministic multi-impl lifetime diagnostic. Fires when some (not all) implementations of an interface violate the lifetime dependency rule. Replaces the previous non-deterministic single-impl selection that caused IOC012 to fire in Rider but pass silently in CLI. - IOC073 / IOC066 — open-generic and inaccessible
IHostedServiceimplementers are skipped at registration with observable diagnostics (rather than emitting CS0246/CS0122 in consumer code). [RegisterAs<T>]+[RegisterAsAll]compose withIHostedService— the concrete class is registered once at the declared lifetime, companion interfaces bridged viaGetRequiredService<TImpl>(), andIHostedServicebridged to the same instance.- Test fixture generator v2 (
IoCTools.Testing) —FixtureMemberPlannerseparates planning from emission. Generated fixtures enable nullable context, emitnewmodifier on derived hidden members, supportGetRequiredSectionand binder-style configuration reads, and treatIClockas fixture-provided. - IOC032 InstanceSharing.Shared exemption, IOC081/082/086 carve-outs
for
Replace, factory lambdas,TryAddEnumerable, and IoCTools-unaware assemblies, buildTransitive packaging ofIoCTools.Generator.targets, and CLI host-path resolver that walksPATHbefore throwing. - Multitarget CLI —
IoCTools.Tools.Cliships for bothnet9.0andnet10.0. Existing .NET 9 global-tool consumers are not broken. - CI build fix (1.7.1) — removed accidental
IoCTools.Testinganalyzer reference fromFixtureEvidence.TestsProject(CLI evidence corpus only needsIoCTools.Testing.Abstractionsfor[Cover<T>]); removed unusedProductionPreferenceHelperTestshelper that combined IoCTools deps with a manual constructor, triggering IOC041 through diagnostics core (net9.0builds pass;net8.0CI now installs SDK8.0.x).
Getting Started in Three Steps
Annotate a partial service
[DependsOn<ILogger<EmailService>>] public partial class EmailService : IEmailService { public Task SendAsync(string to, string subject, string body) => Task.CompletedTask; }Tip:
[Scoped]is implied for partial classes implementing interfaces. Add[Singleton]/[Transient]only when you want to change that default.Build – IoCTools emits
Add<YourAssembly>RegisteredServices()into<AssemblyName>.Extensions.Generated.Call the extension during startup
using YourAssembly.Extensions.Generated; var builder = WebApplication.CreateBuilder(args); builder.Services.AddYourAssemblyRegisteredServices(builder.Configuration);
Common Open-Generic Pattern
[Scoped]
[RegisterAsAll]
public partial class Repository<T> : IRepository<T> where T : class
{
}
That common open-generic path is supported. If you ask for
InstanceSharing.Shared across open-generic interface aliases, IoCTools
emits the secondary IOC095 descriptor and falls back to valid direct
registrations because Microsoft.Extensions.DependencyInjection does not
support open-generic factory aliases. (In 1.6.0+, the primary IOC095
descriptor is the [Inject] deprecation diagnostic — both ship under the
same ID; see diagnostics.md.)
Platform Support
IoCTools works with .NET Framework 4.6.1+, .NET Core 2.0+, and .NET 5+. The generator targets netstandard2.0 internally, but your service code can use any C# features your framework supports. See platform constraints for details.
Testing with IoCTools
The IoCTools.Testing package auto-generates test fixtures, eliminating mock declaration boilerplate.
using IoCTools.Testing.Annotations;
[Cover<UserService>]
public partial class UserServiceTests
{
[Fact]
public void Test() {
var result = Sut.GetById(1); // Lazy auto-generated SUT
}
}
The CLI includes test scaffold and fixture-aware evidence --test-fixtures
paths for downstream adoption:
dotnet ioc-tools test scaffold --project src/App.csproj --type MyApp.Services.UserService --dry-run
dotnet ioc-tools evidence --project tests/App.Tests.csproj --production-project src/App.csproj --test-fixtures
IoCTools CLI
IoCTools.Tools.Cli ships as a dotnet global/local tool (dotnet ioc-tools …). It interrogates your project with the real IoCTools generator.
Installation
dotnet pack IoCTools.Tools.Cli/IoCTools.Tools.Cli.csproj -c Release -o ./artifacts
dotnet tool install --global --add-source ./artifacts IoCTools.Tools.Cli
Commands
| Command | What it surfaces |
|---|---|
fields --project <csproj> --file <class.cs> [--type ...] [--source] |
Lists generated [DependsOn] fields; outputs constructor source with --source |
services --project <csproj> [--output <dir>] [--source] [--type ...] |
Summarizes registrations (lifetimes, interfaces, factories); outputs source with --source |
explain --project <csproj> --type Namespace.Service |
Explains a single service: dependencies, config bindings, external flags |
graph --project <csproj> [--type ...] [--format json\|puml\|mermaid] |
Emits a service graph in JSON/PlantUML/Mermaid |
why --project <csproj> --type ... --dependency Fully.Qualified.Type |
Shows which generated field matches a dependency |
doctor --project <csproj> [--fixable-only] |
Runs generator and prints diagnostics; --fixable-only filters to warnings/infos |
evidence --project <csproj> [--type ...] [--settings ...] |
Emits one correlated evidence bundle across services, diagnostics, configuration, validators, profile, hints, and fingerprinted artifacts |
config-audit --project <csproj> [--settings appsettings.json] |
Lists required config bindings and reports missing keys |
suppress --project <csproj> [--codes IOC035,IOC092] [--json] |
Generates .editorconfig suppression recipes plus structured rule metadata |
validators --project <csproj> [--filter ...] |
Lists discovered FluentValidation validators |
validator-graph --project <csproj> [--why ValidatorName] [--json] |
Shows validator composition tree or structured lifetime explanation |
Before & After: Replacing DI Smells
Legacy (manual, brittle)
public class LegacyBillingService : IBillingService
{
private readonly ILogger<LegacyBillingService> _logger;
private readonly IHttpClientFactory _httpClients;
private readonly BillingOptions _options;
private readonly IConfiguration _config;
public LegacyBillingService(
ILogger<LegacyBillingService> logger,
IHttpClientFactory httpClients,
IOptionsMonitor<BillingOptions> options,
IConfiguration config)
{
_logger = logger;
_httpClients = httpClients;
_options = options.CurrentValue;
_config = config;
}
}
services.AddHttpClient();
services.Configure<BillingOptions>(configuration.GetSection("Billing"));
services.AddScoped<IBillingService, LegacyBillingService>();
Problems: duplicated registrations, runtime config lookups, no analyzer guardrails.
IoCTools (attributes, analyzers, generated DI)
using IoCTools.Abstractions.Annotations;
[Scoped]
[DependsOn<ILogger<BillingService>, IHttpClientFactory, IClock>]
[DependsOnConfiguration<string>("Billing:BaseUrl", Required = true)]
[DependsOnConfiguration<int>("Billing:RetryCount", DefaultValue = "3")]
public partial class BillingService : IBillingService
{
public async Task ChargeAsync(BillingRequest request)
{
using var client = _httpClientFactory.CreateClient("billing");
// _logger, _httpClientFactory, _clock, _baseUrl, _retryCount all available
}
}
Generated code creates the constructor, binds configuration, and registers everything via builder.Services.AddYourAssemblyRegisteredServices(configuration).
Attribute Reference
Complete attribute reference: docs/attributes.md
Key attributes: [Scoped], [Singleton], [Transient], [DependsOn<T>], [DependsOnConfiguration<T>], [DependsOnOptions<T>], [RegisterAs<T>], [ConditionalService], and the 1.6.0 auto-deps surface ([assembly: AutoDep<T>], [assembly: AutoDepOpen(...)], [AutoDeps<TProfile>], [NoAutoDeps], …)
Diagnostics Reference
IoCTools provides core diagnostics through IOC110, plus testing diagnostics (TDIAG01 through TDIAG08) and FluentValidation diagnostics (IOC100-IOC102). See diagnostics.md for the full list.
Error-Severity Diagnostics
| Rule | Summary |
|---|---|
| IOC001 | Service depends on unimplemented interface |
| IOC002 | Implementation missing lifetime attribute |
| IOC003 | Circular dependency detected |
| IOC004 | [RegisterAsAll] requires lifetime |
| IOC011 | Background service must be partial |
| IOC012 | Singleton depends on Scoped |
| IOC014 | Background service with non-Singleton lifetime |
| IOC015 | Lifetime mismatch in inheritance chain |
| IOC016 | Invalid configuration key |
| IOC018 | [InjectConfiguration] requires partial class |
| IOC021 | [ConditionalService] requires lifetime |
| IOC028 | [RegisterAs] without service indicators |
| IOC029 | [RegisterAs] specifies unimplemented interface |
| IOC031 | [RegisterAs] specifies non-interface type |
| IOC041 | Manual constructor conflicts with IoCTools |
| IOC049 | Dependency set with non-metadata members |
| IOC050 | Dependency set cycle detected |
| IOC051 | Dependency set name collision |
| IOC077 | Manual field shadows generated dependency |
| IOC080 | Code-generating attributes require partial |
| IOC081 | Manual registration duplicates IoCTools |
| IOC082 | Manual registration lifetime differs |
| IOC087 | Transient depends on Scoped |
| IOC088 | Configuration circular reference |
| IOC092 | typeof() registration lifetime mismatch |
| TDIAG04 | [Cover<T>] service has no generated constructor |
| TDIAG05 | Test class with [Cover<T>] must be partial |
| TDIAG07 | Fixture setup helper called after Sut access |
| TDIAG08 | Test class manually constructs a service that could use [Cover<T>] |
View all diagnostics including warnings and info
Configuration
IoCTools reads configuration from MSBuild properties/.editorconfig. Common knobs:
| Property | Purpose | Example |
|---|---|---|
IoCToolsNoImplementationSeverity, IoCToolsManualSeverity, IoCToolsLifetimeValidationSeverity |
Override analyzer severity per category | build_property.IoCToolsNoImplementationSeverity = error |
IoCToolsDisableDiagnostics |
Disable all IoCTools diagnostics | true |
IoCToolsIgnoredTypePatterns |
Patterns for cross-assembly interfaces to ignore | *.Abstractions.*;*.Contracts.*;*.Interfaces.* |
IoCToolsDefaultServiceLifetime |
Sets the implicit lifetime when no explicit attribute | Scoped | Singleton | Transient |
Samples & License
IoCTools.Sampledemonstrates every attribute, diagnostic, and configuration scenario- Licensed under MIT. See
LICENSE
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.9.1 | 88 | 5/23/2026 |
| 1.8.0 | 1,192 | 5/12/2026 |
| 1.7.3 | 1,545 | 5/6/2026 |
| 1.7.2 | 88 | 5/6/2026 |
| 1.7.1 | 94 | 5/6/2026 |
| 1.6.1 | 98 | 4/29/2026 |
| 1.6.0 | 91 | 4/29/2026 |
| 1.5.1 | 143 | 4/12/2026 |
| 1.4.0 | 211 | 3/21/2026 |
| 1.3.0 | 118 | 1/24/2026 |
| 1.2.0 | 403 | 11/18/2025 |
| 1.1.0 | 294 | 11/12/2025 |
| 1.0.0 | 198 | 9/10/2025 |
| 1.0.0-alpha | 243 | 8/28/2025 |
| 0.4.1 | 177 | 11/30/2024 |
| 0.4.0 | 150 | 11/30/2024 |
| 0.3.0 | 213 | 3/18/2024 |
| 0.2.6 | 186 | 2/6/2024 |
| 0.2.5 | 308 | 2/6/2024 |
| 0.2.4 | 183 | 2/6/2024 |
v1.7.3: fix IOC997 crash on array TypedConstant in DependsOn-family attrs; fix TDIAG04 false positive for services with [GeneratedCode("IoCTools")] constructor. See CHANGELOG.md.