ServiceCollectionValidation 1.1.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package ServiceCollectionValidation --version 1.1.7
                    
NuGet\Install-Package ServiceCollectionValidation -Version 1.1.7
                    
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="ServiceCollectionValidation" Version="1.1.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ServiceCollectionValidation" Version="1.1.7" />
                    
Directory.Packages.props
<PackageReference Include="ServiceCollectionValidation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ServiceCollectionValidation --version 1.1.7
                    
#r "nuget: ServiceCollectionValidation, 1.1.7"
                    
#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.
#:package ServiceCollectionValidation@1.1.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ServiceCollectionValidation&version=1.1.7
                    
Install as a Cake Addin
#tool nuget:?package=ServiceCollectionValidation&version=1.1.7
                    
Install as a Cake Tool

ServiceCollectionValidation

Exactly what it sounds like, validate your service collection.

Validate in your Setup.cs

In setup:

var services = new ServiceCollection();

// hundreds of lines of registration

var validator = Validators.Predefined.Default;
var results = validator.Validate(services);
if (results.Any())
{
    foreach (var result in results)
    {
        Console.WriteLine(result.Message);
    }
    throw new InvalidOperationException("ServiceCollection is not set up correctly.");
}

Validate in your tests

public void ValidateSetup()
{
    IServiceCollection services = null!;

    Host
        .CreateDefaultBuilder()
        .ConfigureServices(config =>
        {
            services = config;
        })
        .Build();

  var validator = Validators.Predefined.Default;
  var results = validator.Validate(services);
  if (results.Any())
  {
      foreach (var result in results)
      {
          Console.WriteLine(result.Message);
      }
      Assert.Fail();
  }
}

Write new rules

You can write your own rules. This one is already defined, but not used by default.

public class ShouldBeInAlphabeticalOrder : IRule
{
    public IEnumerable<Result> Validate(ServiceCollection services)
    {
        var types = services.Select(s => s.ServiceType);
        var firstOutOfOrder = types.Zip(types.OrderBy(t => t.Name)).FirstOrDefault(pair => pair.First != pair.Second);

        return firstOutOfOrder == default
            ? Enumerable.Empty<Result>()
            : [new Result { Message = $"Services should be registered in alphabetical order but found '{firstOutOfOrder.First.Name}' instead of expected '{firstOutOfOrder.Second.Name}'." }];
    }
}

Use them with a validator

You can new up your own validator

var validator = new Validator();
validator.Rules.Add(new ShouldBeInAlphabeticalOrder());

var results = validator.Validate(services);

or create one based on an existing one since With() and Without() create a copy without modifying the original.

var validator = Validators.Predefined.Default
  .With<ShouldBeInAlphabeticalOrder>();

var results = validator.Validate(services);

or add an instance if you don't want to rely on a default constructor

var validator = Validators.Predefined.Default
  .With(new ShouldBeConfiguredFor(CurrentEnvironment));

var results = validator.Validate(services);

Add your own predefined validator

You may want to define what rules to use in one place and reuse them, possibly across projects or repositories. Here's an example of what that might look like in a common business app.

namespace MyCompany.Common;

public static class ValidatorsExtensions
{
    public static Validator MyCompanyValidator(this Validators predefs)
    {
        return predefs.Default
            .With<ShouldBeInAlphabeticalOrder>()
            .With<ShouldBuildAllServices>()
            .With<MyCompany.Common.ShouldFollowOurNamingConventions>()
            .With<MyCompany.Common.ShouldHaveFewerThan50Methods>()
            .With(new MyCompany.Infrastructure.CommonValidator(strict: false));
            // Note: we need this because of bug #1234
            .Without<ShouldNotHaveDuplicates>()
    }
}

Then use it in any projects that reference that extension method.

Validators.Predefined.MyCompanyValidator().Validate(services);

The Default validator is defined like this

public Validator Default = new Validator()
    .With<ShouldNotBeEmpty>()
    .With<ShouldNotHaveDuplicates>()
    .With<ShouldNotCaptureScope>()
    .With<ShouldIncludeAllDependencies>();

As shown in an earlier example, you can even compose validators themselves. They're "flattened" "so only the rules are added or removed.

var validator = Validators.Predefined.MyCompanyValidator()
  .With(new SuperAdvancedValidator())
  .Without<ShouldBeInAlphabeticalOrder>();

Existing rules

ShouldBeInAlphabeticalOrder

This is not included in the Default validator.

ShouldBuildAllServices

Validates that all services can actually be built.

This is not included in the Default validator.

ShouldIncludeAllDependencies

Validates all dependencies are registered.

This is included in the Default validator.

ShouldNotBeEmpty

This is included in the Default validator.

ShouldNotCaptureScope

Validates that no services with Scope lifetime are injected into services with the Singleton lifetime.

This is included in the Default validator.

ShouldNotHaveDuplicates

Validates the exact same implementation and service pair aren't registered more than once.

This is included in the Default validator.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ServiceCollectionValidation:

Package Downloads
ServiceCollectionValidation.AspNetCore

AspNetCore-specific rules to validate your ServiceCollection.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.8.0 157 2/13/2026
1.7.0 698 12/2/2025
1.6.1 152 11/28/2025
1.6.0 145 11/28/2025
1.5.0 753 9/30/2025
1.4.0 266 9/29/2025
1.3.0 204 9/29/2025
1.2.0 197 9/29/2025
1.1.8 177 9/13/2025
1.1.7 178 9/13/2025
1.1.6 174 9/13/2025
1.1.5 213 8/12/2025
1.1.4 271 8/6/2025
1.1.3 279 8/6/2025