Masterly.Autofac.DependenciesScanner 1.0.0

.NET 5.0 .NET Core 3.1 .NET Standard 2.0 .NET Framework 4.6.2
There is a newer version of this package available.
See the version list below for details.
Install-Package Masterly.Autofac.DependenciesScanner -Version 1.0.0
dotnet add package Masterly.Autofac.DependenciesScanner --version 1.0.0
<PackageReference Include="Masterly.Autofac.DependenciesScanner" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Masterly.Autofac.DependenciesScanner --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Masterly.Autofac.DependenciesScanner, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Masterly.Autofac.DependenciesScanner as a Cake Addin
#addin nuget:?package=Masterly.Autofac.DependenciesScanner&version=1.0.0

// Install Masterly.Autofac.DependenciesScanner as a Cake Tool
#tool nuget:?package=Masterly.Autofac.DependenciesScanner&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.


Assembly scanning and decoration extensions for Autofac.


  • Scan the given assemblies and exteract all services.
  • Register extracted services and determine its lifetime according to the implemented interface (eg. ISocopedDependnecy ⇒ Instance Per Lifetime Scope ...etc).
  • Support custom registration by inheret from ScannerModule.
  • Scan both concrete and generic class types.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!


Install the Masterly.Autofac.DependenciesScanner NuGet Package.

Package Manager Console

Install-Package Masterly.Autofac.DependenciesScanner


dotnet add package Masterly.Autofac.DependenciesScanner


IScopedDependencyIDependencyA lifetime would be Instance Per Lifetime Scope

public interface IDependencyA : IScopedDependency { }

IDependencyB going to ineheret IDependencyA lifetime with is Instance Per Lifetime Scope

public interface IDependencyB : IDependencyA { }

ISingletonDependencyIDependencyC lifetime would be Single Instance

public interface IDependencyC : ISingletonDependency { }
public abstract class AbstractDependencyClassA : IDependencyB { }

// ITransientDependency => AbstractDependencyClassB lifetime would be Instance per dependency
public abstract class AbstractDependencyClassB : ITransientDependency { }

public class DependencyA : IDependencyA { }
public class DependencyB : AbstractDependencyClassA { }
public class DependencyC : AbstractDependencyClassB { }
public class DependencyD : IDependencyC { }

As you can see above, all services going to be registered as all implemented interfaces and inhereted abstract classes, in addition to itself. For example, DependencyB can be resolved as IDependencyA, IDependencyB, AbstractDependencyClassA, in addition to DependencyB.

In the beginning use UseServiceProviderFactory extension in Program.cs as below

public class Program
  public static void Main(string[] args)
    // ASP.NET Core 3.0+:
    // The UseServiceProviderFactory call attaches the
    // Autofac provider to the generic hosting mechanism.
    var host = Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webHostBuilder => {


You can use ContainerBootstraper to prepare and config Autofac container and scan services and modules in all given assemblies. Here, it registers services and Autofac modules and excexutes ScannerModules, then returns a built container (not recomended for Asp Core)

IContainer container = ContainerBootstrapper.Bootstrap(assemblies);

For Asp Core, use ContainerBootstrapper as below

// ConfigureContainer is where you can register things directly
// with Autofac. This runs after ConfigureServices so the things
// here will override registrations made in ConfigureServices.
// Don't build the container; that gets done for you by the factory.
public void ConfigureContainer(ContainerBuilder containerBuilder)
    // Register your own things directly with Autofac here. Don't
    // call containerBuilder.Populate(), that happens in AutofacServiceProviderFactory
    // for you.
    ContainerBootstrapper.Bootstrap(builder, assemblies);

To register single service


Scan all services those implements IScopedDependency, ITransientDependency or ISingletonDependency and register them in the ContainerBuilder, also exceute scanner modules those inheret from ScannerModule.



ScannerModule gives tha ability to customize scanning and component registration. For example:

public interface IApi { }

public class Api1 : IApi { public Api1(string url) { } }
public class Api2 : IApi { public Api2(string url) { } }

public static class ApiFactory
    public static object Create(Type type, string url) {
        object api =  Activator.CreateInstance(type, url);
        return api;

public class SampleScannerModule : ScannerModule
    public override void Scan(ContainerBuilder builder, params Assembly[] assemblies)
        foreach (var serviceType in assemblies.SelectMany(assembly => assembly.GetExportedTypes().Where(t => typeof(IApi).IsAssignableFrom(t))))
            Register(builder, serviceType);

    private void Register(ContainerBuilder builder, Type serviceType)
        if (serviceType.IsInterface || serviceType.IsAbstract || !typeof(IApi).IsAssignableFrom(serviceType))

        string url = "";

            .ForDelegate((ctx, parameters) => ApiFactory.Create(serviceType, url))

The default lifetime for any service deos not implement any of the mentioned interfaces, it is going to be Instance Per Lifetime Scope



Free Software, Hell Yeah!

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.0.1 93 12/30/2021
1.0.0 92 11/22/2021