Moclawr.Services.Autofac
2.0.0
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
<PackageReference Include="Moclawr.Services.Autofac" Version="2.0.0" />
<PackageVersion Include="Moclawr.Services.Autofac" Version="2.0.0" />
<PackageReference Include="Moclawr.Services.Autofac" />
paket add Moclawr.Services.Autofac --version 2.0.0
#r "nuget: Moclawr.Services.Autofac, 2.0.0"
#:package Moclawr.Services.Autofac@2.0.0
#addin nuget:?package=Moclawr.Services.Autofac&version=2.0.0
#tool nuget:?package=Moclawr.Services.Autofac&version=2.0.0
Moclawr.Services.Autofac
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 servicesRepositoryModule
: Registers repositoriesControllerModule
: Registers controllers for ASP.NET Core applicationsAttributeModule
: 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 | Versions 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. |
-
net9.0
- Autofac (>= 8.3.0)
- Autofac.Extensions.DependencyInjection (>= 10.0.0)
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.5)
- Moclawr.Core (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added improved XML documentation and bug fixes.