Skip to content

Configuration Service Registration

quyvu edited this page Oct 15, 2026 · 4 revisions

Service Registration

Comprehensive guide to registering and configuring FxMap services in your application's dependency injection container.

The AddFxMap Method

All FxMap configuration starts with the AddFxMap extension method on IServiceCollection.

using FxMap;
 
builder.Services.AddFxMap(cfg =>
{
    // Configuration goes here
});

Core Configuration Methods

AddEntitiesFromAssemblyContaining

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.

AddProfilesFromAssemblyContaining

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>();

AddReceivedPipelines / AddSendPipelines

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.

Adding Data Providers

Chain data provider registration after AddFxMap:

Entity Framework Core

builder.Services.AddFxMap(cfg => { ... })
    .AddEntityFrameworkCore(options => options.AddDbContexts(typeof(TestDbContext), typeof(OtherDbContext)));

MongoDB

builder.Services.AddFxMap(cfg => { ... })
   .AddMongoDb(cfg => cfg.AddCollection(userCollection).AddCollection(orderCollection));

Adding Transports

For microservices, add a transport for cross-service communication:

RabbitMQ

builder.Services.AddFxMap(cfg => {
    // Other configurations
    cfg.AddRabbitMq(config =>
    {
        config.Host("localhost", "/", 5672, c =>
        {
            c.UserName("guest");
            c.Password("guest");
        });
    });
});

NATS

builder.Services.AddFxMap(cfg => {
    // Other configurations
    cfg.AddNats(c => c.NatsOpts(opts => opts.Url = "nats://localhost:4222"));
});

gRPC

builder.Services.AddFxMap(cfg =>
{
    // Other configurations
    cfg.AddGrpcClients(c => c.AddGrpcHosts("http://localhost:5001", "http://localhost:5013"));
});

Complete Registration Example

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();

Best Practices

  • 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

Common Mistakes

  • 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.

Clone this wiki locally