Repository navigation
Configuration Service Registration
Comprehensive guide to registering and configuring FxMap services in your application's dependency injection container.
All FxMap configuration starts with the AddFxMap extension method on IServiceCollection.
using FxMap;
builder.Services.AddFxMap(cfg =>
{
// Configuration goes here
});Scans an assembly for all EntityConfigureOf<T> classes and registers them. This is required for FxMap to discover your entity configurations on the data-provider side.
cfg.AddEntitiesFromAssemblyContaining<SomeEntityAssemblyMarker>();Note: FxMap discovers all classes inheriting from EntityConfigureOf<T> in the specified assembly automatically.
Scans an assembly for all ProfileOf<T> classes and registers them. These define how DTOs are populated on the consumer side.
// Use a marker type from the assembly containing your profiles
cfg.AddProfilesFromAssemblyContaining<SomeProfileAssemblyMarker>();Registers pipeline behaviors that run before and after each mapping request. Pipelines are the recommended extension point for cross-cutting concerns like logging and tracing.
cfg.AddReceivedPipelines(c => c.OfType<LoggingReceivedPipeline>());
cfg.AddSendPipelines(c => c.OfType<LoggingSendPipeline>());Note: Since v2.0, pipelines are the only handler registration mechanism. The removed AddHandlersFromNamespaceContaining method should be deleted from any 1.x code you are migrating.
Chain data provider registration after AddFxMap:
builder.Services.AddFxMap(cfg => { ... })
.AddEntityFrameworkCore(options => options.AddDbContexts(typeof(TestDbContext), typeof(OtherDbContext)));builder.Services.AddFxMap(cfg => { ... })
.AddMongoDb(cfg => cfg.AddCollection(userCollection).AddCollection(orderCollection));For microservices, add a transport for cross-service communication:
builder.Services.AddFxMap(cfg => {
// Other configurations
cfg.AddRabbitMq(config =>
{
config.Host("localhost", "/", 5672, c =>
{
c.UserName("guest");
c.Password("guest");
});
});
});builder.Services.AddFxMap(cfg => {
// Other configurations
cfg.AddNats(c => c.NatsOpts(opts => opts.Url = "nats://localhost:4222"));
});builder.Services.AddFxMap(cfg =>
{
// Other configurations
cfg.AddGrpcClients(c => c.AddGrpcHosts("http://localhost:5001", "http://localhost:5013"));
});Here's a comprehensive example showing all configuration options together:
using FxMap;
var builder = WebApplication.CreateBuilder(args);
// Complete FxMap configuration
builder.Services.AddFxMap(cfg =>
{
// Register entity configs (data-provider side)
cfg.AddEntitiesFromAssemblyContaining<SomeEntityAssemblyMarker>();
// Register DTO profiles (consumer side)
cfg.AddProfilesFromAssemblyContaining<SomeProfileAssemblyMarker>();
// Add pipelines
cfg.AddReceivedPipelines(c => c.OfType<LoggingReceivedPipeline>());
cfg.AddSendPipelines(c => c.OfType<LoggingSendPipeline>());
// Configure strongly typed IDs (if needed)
cfg.AddStronglyTypeIdConverter(a => a.OfType<StronglyTypeIdConverters>());
// Error handling
cfg.ThrowIfException();
// Set limits
cfg.SetMaxNestingDepth(16);
// Retry policy
cfg.SetRetryPolicy(
retryCount: 3,
sleepDurationProvider: n => TimeSpan.FromSeconds(Math.Pow(2, n - 1)), // waited before retry n
onRetry: (exception, timeSpan) =>
{
Console.WriteLine($"Retry after {timeSpan}: {exception.Message}");
}
);
cfg.AddRabbitMq(config =>
{
config.Host("localhost", "/", 5672, c =>
{
c.UserName("guest");
c.Password("guest");
});
});
})
.AddEntityFrameworkCore(options => options.AddDbContexts(typeof(TestDbContext), typeof(OtherDbContext)));
var app = builder.Build();
app.Run();- Register entity configs before profiles for clarity
- Add exactly one data provider (EFCore or MongoDB)
- Transport packages are optional but recommended for microservices
- Use ThrowIfException during development for better debugging
- Forgetting to register the assembly containing your entity configs or profiles
- Not adding a data provider (EFCore or MongoDB is required)
- Calling the removed
AddHandlersFromNamespaceContaining— use pipelines instead (see v2.0 migration guide) - Using mismatched versions across FxMap packages
Source: https://fxmapper.net/docs/configuration/service-registration/ — the website is always the most up to date version.
Introduction
Getting Started
Configuration
- Service Registration
- Entities & Profiles
- Handlers
- Pipelines
- Strongly Typed IDs
- Error Handling
- Retry Policy
- Supervisor
- Telemetry
- Kestrel (HTTP/2)
- Analyzers
Expressions
- Overview
- Property Access
- Filters, Sorting & Paging
- Functions
- Aggregations
- Projections
- Coalesce & Ternary
- GroupBy
Data Providers
Transports
Integrations
Quality
AI Assistants
Migration
Release Notes