Skip to content

Error when mapping with injected service #204

@jornhd

Description

@jornhd

AgileMapper Version 1.7.1
I'm having problems when trying to use an injected service in member mapping.
Using .NET core 3.1, I've setup the service provider in public static void Configure(IApplicationBuilder app):

            Mapper.WhenMapping
                .UseServiceProvider(app.ApplicationServices);

This seems to work fine as I get a logger message in mapping configuration:

    public class MyDtoMapper : MapperConfiguration
    {
        protected override void Configure()
        {
            GetService<ILogger<MyDtoMapper>>().LogInformation("Hello, from MyDtoMapper");
            WhenMapping....
        }
    }

I've tried this from the documentation:

A value from an injected service:

// Retrieve an IDateTimeProvider instance from a configured
// service provider, and use its UtcNow value:
Mapper.WhenMapping
    .From<OrderDto>()        // Apply to OrderDto mappings
    .ToANew<Order>()         // Apply to Order creation
    .Map(ctx => ctx.GetService<IDateTimeProvider>().UtcNow)
    .To(o => o.DateCreated); // o is the Order

But this results in the following exception.

System.InvalidOperationException:
variable 'i' of type 'System.Int32' referenced from scope '', but it is not defined
at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)

So.. I'm I doing something wrong, or is this a bug?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions