Home > Software design >  Do TypeConverters need to be registered with the DI?
Do TypeConverters need to be registered with the DI?

Time:02-05

I have a mapping in my WebApi project:

cfg.CreateMap(typeof(Source<>), typeof(Destination), MemberList.Destination)
    .ConvertUsing(typeof(SourceConverter<>));

And the converter itself:

internal class SourceConverter<T> : ITypeConverter<Source<T>, Destination>
    where T : struct, IConvertible
{
    public Destination Convert(SourceConverter<T> exception, Destination error, ResolutionContext ctx)
    {
        return new Destination();
    }
}

And when I try to map, I'm getting the following exception:

The requested service 'SourceConverter`1[[MyEnum, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

Do the converters need to be registered with the DI?

CodePudding user response:

No, you can see an example in the official documentation here: https://docs.automapper.org/en/stable/Custom-type-converters.html

Your error is elsewhere.

  • Related