I have an ASP.NET Core Web API project. In order to configure AutoMapper, I added the following code in my Program.cs file:
builder.Services.AddAutoMapper(typeof(Program));
I inject AutoMapper in my service, BpmsServices, as follows:
public class BpmsServices(
IMapper mapper,
IRepository<Workflow> workflowRepository)
{
public IMapper DataMapper { get; } = mapper;
public IRepository<Workflow> WorkflowRepository { get; } = workflowRepository;
}
In the API file, I have:
public static IEndpointRouteBuilder MapWorkflowDesignApiV1(this IEndpointRouteBuilder app)
{
var api = app.MapGroup("api/workflow")
.HasApiVersion(1.0);
api.MapPut("/Node/CreateUpdate", CreateUpdateNode);
return app;
}
public static async Task<Workflow> CreateUpdateNode([AsParameters] BpmsServices services, NodeDto node)
{
var = await services.WorkflowRepository
.Include("Nodes")
.FirstOrDefaultAsync(s => s.Id == node.WorkflowId);
if (node.Id != 0)
{
.AddNode(services.DataMapper.Map<NodeDto, Node>(node));
}
else
{
.UpdateNode(services.DataMapper.Map<NodeDto, Node>(node));
}
await services.WorkflowRepository.SaveChangesAsync();
return;
}
And here is the exception I get:
System.InvalidOperationException: The public parameterized constructor must contain only parameters that match the declared public properties for type 'BpmsServices'.
What is the problem?
Map<src,dest>(src, dest)method to go direct from a Dto to a tracked entity.[AsParameters]rather than[FromServices]?[AsParameters]works with the incoming request, you're not sending your mapper and repository via the request.public IMapper DataMapper { get; } = mapper;) to directly set the properties from the constructor parameters. While this is valid C# syntax, it can confuse the DI system, which expects the property values to be set explicitly within the constructor rather than through initializers.public class BpmsServices { public IMapper DataMapper { get; } public IRepository<Workflow> WorkflowRepository { get; } public BpmsServices(IMapper dataMapper, IRepository<Workflow> workflowRepository) { DataMapper = dataMapper; WorkflowRepository = workflowRepository; } }