最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Passing logger to controller with carter - Stack Overflow

programmeradmin3浏览0评论

I'm playing with Carter and so far it works fine, but I have an issue when I want to pass logger (for now its NullLogger, later on I will replace it with something more menaingful).

System.InvalidOperationException: An action cannot use both form and JSON body parameters.
Below is the list of parameters that we found: 

Parameter           | Source                        
---------------------------------------------------------------------------------
file                | Form File (Inferred)
logger              | Body (Inferred)

How do I pass ILogger from Microsoft.Extensions.Logging.Abstractions and other things that I would have in builder.Services into Carter endpoints?

Here is my code:

using Carter;
using Microsoft.Extensions.Logging.Abstractions;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at 
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddLogging(l => l.AddProvider(NullLoggerProvider.Instance));
builder.Services.AddCarter();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.MapCarter();

app.UseHttpsRedirection();

app.Run();

And here is the controller:

using Carter;

namespace RESTDemo.Controllers
{
    public class ParseController : ICarterModule
    {
        public void AddRoutes(IEndpointRouteBuilder app)
        {
            var group = app.MapGroup("api/parse");

            group.MapPost("", ParseFile).DisableAntifery();
        }

        private IResult ParseFile(IFormFile file, ILogger logger)
        {
            logger.LogInformation("ParserExecuted");

            return Results.Ok();
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论