Вызвать обработчик CreateUserComandC#

Место общения программистов C#
Ответить
Anonymous
 Вызвать обработчик CreateUserComand

Сообщение Anonymous »

Я пытаюсь вызвать ручку, чтобы вернуть значения с уровня приложения. Я использую шаблон посредника. Я пытался позвонить это, но не смог из -за отсутствия опыта.

Код: Выделить всё

using CleanAchitecture.Security.Api.Filters;
using CleanAchitecture.Security.Application.Abstractions.Messaging;
using CleanAchitecture.Security.Application.Contracts.User;
using CleanAchitecture.Security.Application.User;
using Microsoft.AspNetCore.Mvc;

namespace CleanAchitecture.Security.Api.Endpoints
{
internal sealed class User : IEndpoint
{
public void MapEndpoint(IEndpointRouteBuilder app)
{
app.MapPost("todos", async (
CreateUserRequest request,
[FromServices] ICommandHandler handler,
CancellationToken cancellationToken) =>
{

var result = await handler.Handle(new CreateUserComand { CreateUserRequest = request }, cancellationToken);

return result;
})
.AddEndpointFilter(new AuthorizationFilter("apiii"));//Here
}
}
}
Фильтр этого класса: это фильтр, который я хочу вызвать командой выполнения, чтобы вернуть значение, если оно действительно или нет.
"CreateUserComand" не содержит ни определения для "Handle", ни доступного метода расширения "Handle", который принимает первый аргумент типа "CreateUserComand" (вам не хватает директивы using или сборки ссылку?)

Код: Выделить всё

using CleanAchitecture.Security.Application.User;
using CleanAchitecture.Security.Domain.Common;
using System.Threading;

namespace CleanAchitecture.Security.Api.Filters
{
public class AuthorizationFilter(string api) : IEndpointFilter
{
public async ValueTask InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{

var handler = new CreateUserComand();

var result = await handler.Handle(new CreateUserComand { CreateUserRequest = request });
if(result != null) return await next(context);

return Result.BadRequest();
}
}
}
< /code>
Этот класс CreateUserCommand: < /p>
using CleanAchitecture.Security.Application.Abstractions.Messaging;
using CleanAchitecture.Security.Application.Contracts.User;
using CleanAchitecture.Security.Domain.Common;
using CleanAchitecture.Security.Domain.Entities;
using CleanAchitecture.Security.Infrastructure.Interface;
using FluentValidation;

namespace CleanAchitecture.Security.Application.User
{
#region command
public sealed record CreateUserComand : ICommand
{
public CreateUserRequest CreateUserRequest { get; set; }
};
#endregion

#region command handler
internal sealed class CreateUserCommandHandler(
IUserRepository IUserRepository
)
: ICommandHandler
{
public async Task Handle(CreateUserComand command, CancellationToken cancellationToken)
{
var securityUser = new SecurityUser
{
Id = "",
Name = command.CreateUserRequest.Name,
Password = command.CreateUserRequest.Password,
ProfileId = command.CreateUserRequest.ProfileId,

DateCreated = DateTimeOffset.UtcNow,
DateModified = DateTimeOffset.UtcNow
};

await IUserRepository.AddAsync(securityUser);

return Result.OK();
}
}
#endregion

#region command validator
internal sealed class CreateUserCommandValidator : AbstractValidator
{
public CreateUserCommandValidator()
{
RuleFor(c => c.CreateUserRequest.Name).NotEmpty();
RuleFor(c => c.CreateUserRequest.Password).NotEmpty();
RuleFor(c => c.CreateUserRequest.ProfileId).NotEmpty();
}
}
#endregion
}
Пожалуйста, не могли бы вы помочь мне решить эту ошибку?

Подробнее здесь: https://stackoverflow.com/questions/797 ... nd-handler
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»