© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
4 replies
Rizell

MediatR - System.Text.Json.JsonException: 'S' is an invalid start of a value.

I have a project with Clean Architecture and MediatR.

My API gets the request fine and the command is created with correct data. In this case my tests passes Email = "test@test.com"
var command = new CreateUserCommand(userReq.Email);
var createUserResult = await mediator.Send(command);

My command:
public record CreateUserCommand(string Email) : IRequest<ErrorOr<User>>;

My handler:
  public async Task<ErrorOr<User>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
    {
        var user = await _userRepository.CreateUser(new User(){Email = command.Email});
        await _unitOfWork.CommitChangesAsync();
        return user;
    }

I Register my MediatR like this:
 services.AddMediatR(options =>
        {
            // options.RegisterServicesFromAssemblyContaining(typeof(DependencyInjection));
            options.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
        });
var command = new CreateUserCommand(userReq.Email);
var createUserResult = await mediator.Send(command);

My command:
public record CreateUserCommand(string Email) : IRequest<ErrorOr<User>>;

My handler:
  public async Task<ErrorOr<User>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
    {
        var user = await _userRepository.CreateUser(new User(){Email = command.Email});
        await _unitOfWork.CommitChangesAsync();
        return user;
    }

I Register my MediatR like this:
 services.AddMediatR(options =>
        {
            // options.RegisterServicesFromAssemblyContaining(typeof(DependencyInjection));
            options.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly);
        });


However, my Handler is never invoked. Directly after
mediator.Send(command);
mediator.Send(command);

I get the following error
System.Text.Json.JsonException
'S' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)



Any pointers or ideas what the cause might be?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

System.Text.Json.JsonException: A possible object cycle was detected
C#CC# / help
2y ago
✅ JSON Contract : modifying the value of a property, using System.Text.Json 7
C#CC# / help
4y ago
❔ ✅ System.Text.Json paths
C#CC# / help
3y ago
System.Text.Json Source Generators
C#CC# / help
3y ago