Moclawr.Services.Autofac 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Moclawr.Services.Autofac --version 2.0.0
                    
NuGet\Install-Package Moclawr.Services.Autofac -Version 2.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="Moclawr.Services.Autofac" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moclawr.Services.Autofac" Version="2.0.0" />
                    
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.0.0
                    
#r "nuget: Moclawr.Services.Autofac, 2.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.
#:package Moclawr.Services.Autofac@2.0.0
                    
#: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.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Moclawr.Services.Autofac&version=2.0.0
                    
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

  • 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
    • RepositoryModule: Registers repositories
    • ControllerModule: Registers controllers for ASP.NET Core applications
    • AttributeModule: Handles attribute-based registration
  • 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.Extensions;

// Configure the host to use Autofac
var builder = WebApplication.CreateBuilder(args);

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

// Add services to the container
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();

Attribute-based Service Registration

Use attributes to mark services with their intended lifetimes:

using Services.Autofac.Attributes;

// Mark interfaces with ServiceContract attribute
[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
}

Custom Modules

Create custom Autofac modules for specific registration logic:

using Autofac;
using Services.Autofac.Modules;

public class DatabaseModule : BaseModule
{
    protected override void Load(ContainerBuilder builder)
    {
        // Register database-related services
        builder.RegisterType<DbContext>()
            .AsSelf()
            .InstancePerLifetimeScope();
            
        builder.RegisterGeneric(typeof(GenericRepository<>))
            .As(typeof(IGenericRepository<>))
            .InstancePerLifetimeScope();
            
        base.Load(builder);
    }
}

// Use the custom module
builder.UseAutofacServiceProvider(
    configureContainer: builder => {
        builder.RegisterModule<DatabaseModule>();
    },
    typeof(Program).Assembly
);

Integration with Other Moclawr Packages

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

  • Moclawr.Core: Provides core utilities and extensions
  • Moclawr.Host: Can be used together for building complete API solutions
  • Moclawr.MinimalAPI: Works with Autofac for dependency injection in Minimal API applications

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 276 6/3/2025
2.1.0 146 5/28/2025
2.0.0 74 5/24/2025
1.0.3 75 5/24/2025
1.0.2 139 5/22/2025
1.0.1 144 5/21/2025
1.0.0 194 5/16/2025

Added improved XML documentation and bug fixes.