Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

using autofac with automapper .net 6 with Dependency Injection

//create class name it for example AutoMapperModule

    public class AutoMapperModule :  Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
        //Also register any custom type converter/value resolvers
            builder.RegisterType<CustomValueResolver>().AsSelf();
            builder.RegisterType<CustomTypeConverter>().AsSelf();
        
            builder.Register(c =>

            {
                var context = c.Resolve<IComponentContext>();
               return new MapperConfiguration(cfg =>
           
               cfg.CreateMap<MyModel, MyVModelDto>().
               ConstructUsing(e => new MyModelDto(context.Resolve<IMyModelDtoRegisteredService>())).ReverseMap());               

           }).AsSelf().SingleInstance();

            builder.Register(c =>
            {
            //This resolves a new context that can be used later.
                var context = c.Resolve<IComponentContext>();
                var config = context.Resolve<MapperConfiguration>();
                return config.CreateMapper(context.Resolve);
            }
            ).As<IMapper>().InstancePerLifetimeScope();
        }
    }

// in Program.cs file 

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());


// Add services to the container.
builder.Services.AddControllersWithViews();

//Register your own things directly with Autofac here

builder.Host.ConfigureContainer<ContainerBuilder>(builder => builder.RegisterType<Service>().As<IService>());
//etc...
// At the end registering your own things directly with Autofac add here
builder.Host.ConfigureContainer<ContainerBuilder>(builder => builder.RegisterModule(new AutoMapperModule()));

var app = builder.Build();

Comment

PREVIOUS NEXT
Code Example
Csharp :: c# windows form BalloonTipIcon close 
Csharp :: how can datetimepicker accept hour as well 
Csharp :: regex ip rage detect c# 
Csharp :: c# functions 
Csharp :: .net framework cheat sheet 
Csharp :: unity get layermask 
Csharp :: return array in c# 
Csharp :: unity inspector sliders 
Csharp :: unity bool to int 
Csharp :: call ienumerator unity 
Csharp :: c# String Uppercase and Lowercase method 
Csharp :: copy file 
Csharp :: C#: casting string to enum object 
Csharp :: how to clear a dictionary in c# 
Csharp :: run as administrator vs 2019 
Csharp :: Task w = Task.Delay(600);w.Wait();new Program().Start(); 
Csharp :: unity colllion not working 
Csharp :: wpf scoll to on new item datagrtid 
Html :: fevicon 
Html :: ion-item remove bottom line 
Html :: multipart form 
Html :: how to center html heading 
Html :: html input float type 
Html :: lien dans un nouvel onglet html 
Html :: add favicon to html 
Html :: bootstrap div vertical center 
Html :: html code æøå 
Html :: css textarea limit 
Html :: how to comment out html 
Html :: bootstrap outline buttons 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =