1

I specify mapster service operations in web program.cs in layered architecture, but I want to use this application in another layer. And I'm having trouble choosing assembly.

Web Layer Program.cs

var config = TypeAdapterConfig.GlobalSettings;
config.Scan(Assembly.GetAssembly(typeof(UserMappingConfig)));
builder.Services.AddSingleton(config);
builder.Services.AddScoped<IMapper, ServiceMapper>();

Service Layer Mapping Classes

namespace Exams.Service.Mapping
{
    public class QuestionMappingConfig : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.NewConfig<QuestionViewModel, Question>().IgnoreNullValues(true);
            config.NewConfig<List<QuestionViewModel>,List<Question>>().IgnoreNullValues(true);
            config.NewConfig<Question, QuestionViewModel>().IgnoreNullValues(true);
        }
    }
}

I am getting a warning like this

Severity Code Description Project File Line Suppression State Warning CS8604 Possible null reference argument for parameter 'assemblies' in 'IList TypeAdapterConfig.Scan(params Assembly[] assemblies)'.

Program.cs

NLayerProject

To summarize, for the Mapster application that I have to define in the Web layer, I want to use the configurations that I defined in the Service layer in the Service layer, but I have trouble choosing the assembly. How can I fix?

Berk KARASU
  • 71
  • 1
  • 9

1 Answers1

1

Assembly.GetAssembly returns Assembly? (see nullable reference types), you can either use null-forgiving operator (!):

config.Scan(Assembly.GetAssembly(typeof(UserMappingConfig))!);

Or just use Assembly from the type instance:

config.Scan(typeof(UserMappingConfig).Assembly);
Guru Stron
  • 102,774
  • 10
  • 95
  • 132