Moclawr.Services.Autofac 2.1.1

dotnet add package Moclawr.Services.Autofac --version 2.1.1
                    
NuGet\Install-Package Moclawr.Services.Autofac -Version 2.1.1
                    
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="Moclawr.Services.Autofac" Version="2.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moclawr.Services.Autofac" Version="2.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Moclawr.Services.Autofac" />
                    
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 Moclawr.Services.Autofac --version 2.1.1
                    
#r "nuget: Moclawr.Services.Autofac, 2.1.1"
                    
#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 Moclawr.Services.Autofac@2.1.1
                    
#: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=Moclawr.Services.Autofac&version=2.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Moclawr.Services.Autofac&version=2.1.1
                    
Install as a Cake Tool

Moclawr.Services.Autofac

NuGet

Overview

Moclawr.Services.Autofac provides seamless integration with Autofac dependency injection container for .NET applications. It offers convention-based registration, attribute-based service registration, and modular configuration to enhance your application's dependency injection capabilities.

Features

  • Simple Registration Methods: Easy-to-use extension methods following the same pattern as other Moclawr services
  • Attribute-based Registration: Register services with custom attributes for different lifetimes (Transient, Scoped, Singleton)
  • Assembly Scanning: Automatically register services based on conventions from specified assemblies
  • Module-based Organization: Group related registrations into Autofac modules
  • Built-in Modules:
    • ServiceModule: Registers application services by convention and attributes
    • GenericModule: Handles open generic type registrations
    • ControllerModule: Registers MVC controllers for ASP.NET Core applications
  • ASP.NET Core Integration: Extension methods for WebApplicationBuilder and IHostBuilder

Installation

Install the package via NuGet Package Manager:

dotnet add package Moclawr.Services.Autofac

Usage

Basic Integration with ASP.NET Core

In your Program.cs:

using Services.Autofac;

var builder = WebApplication.CreateBuilder(args);

// Configure Autofac as the service provider
builder.AddAutofacServiceProvider(
    // Optional: configure container manually
    configureContainer: container => {
        // Add manual registrations
        container.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
    },
    // Assemblies to scan for services
    typeof(Program).Assembly,
    typeof(ApplicationLayer).Assembly
);

// Add other services
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

Alternative Registration Methods

// For Generic Host applications
var hostBuilder = Host.CreateDefaultBuilder(args);
hostBuilder.AddAutofacServiceProvider(
    configureContainer: container => {
        // Configure container
    },
    typeof(Program).Assembly
);

// For existing service collections
var services = new ServiceCollection();
var serviceProvider = services.AddAutofacServices(
    configureContainer: container => {
        // Configure container
    },
    typeof(Program).Assembly
);

Attribute-based Service Registration

Use attributes to mark services with their intended lifetimes:

using Services.Autofac.Attributes;

// Mark interfaces with ServiceContract attribute (optional but recommended)
[ServiceContract]
public interface IUserService
{
    Task<User> GetUserByIdAsync(int userId);
    Task<bool> CreateUserAsync(User user);
}

// Mark implementation classes with lifetime attributes
[ScopedService]
public class UserService : IUserService
{
    // Implementation
}

[TransientService]
public class EmailService : IEmailService
{
    // Implementation
}

[SingletonService]
public class ConfigurationService : IConfigurationService
{
    // Implementation
}

Manual Container Configuration

builder.AddAutofacServiceProvider(container => {
    // Register individual services
    container.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope();
    
    // Register generic types
    container.AddGenericServices(typeof(Program).Assembly);
    
    // Register controllers
    container.AddControllers(typeof(Program).Assembly);
    
    // Register assemblies with specific options
    container.AddServiceAssemblies(
        registerByNamingConvention: true,
        autoRegisterConcreteTypes: false,
        typeof(Program).Assembly
    );
});

Convention-based Registration

Services ending with "Service", "Repository", or "Manager" are automatically registered:

// These will be automatically registered
public interface IUserRepository { }
public class UserRepository : IUserRepository { } // Automatically registered as IUserRepository

public interface IEmailService { }
public class EmailService : IEmailService { } // Automatically registered as IEmailService

public interface IDataManager { }
public class DataManager : IDataManager { } // Automatically registered as IDataManager

Integration with Other Moclawr Packages

This package works seamlessly with other packages in the Moclawr ecosystem:

  • Moclawr.Core: Provides core utilities and extensions for enhanced functionality
  • Moclawr.Shared: Uses dependency injection for shared services and interfaces
  • Moclawr.Host: Can be used together for building complete API solutions with advanced DI patterns
  • Moclawr.MinimalAPI: Perfect for dependency injection in Minimal API applications with MediatR
  • Moclawr.EfCore: Registers repository and context services automatically
  • Moclawr.MongoDb: Supports MongoDB context and repository registration
  • Moclawr.Services.Caching: Integrates caching services with Autofac container

Requirements

  • .NET 9.0 or higher
  • Autofac 8.3.0 or higher
  • Autofac.Extensions.DependencyInjection 10.0.0 or higher

License

This package is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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

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
2.1.1 274 6/3/2025
2.1.0 145 5/28/2025
2.0.0 73 5/24/2025
1.0.3 74 5/24/2025
1.0.2 138 5/22/2025
1.0.1 143 5/21/2025
1.0.0 193 5/16/2025

Added improved XML documentation and bug fixes.