FluentResults icon indicating copy to clipboard operation
FluentResults copied to clipboard

Problems with FluentResults.Extensions.AspNetCore

Open PUDGE133 opened this issue 5 months ago • 4 comments

My code: Image

Image Image

The problematic part is here:

using FluentResults;
using FluentResults.Extensions.AspNetCore;
using Microsoft.AspNetCore.Mvc;

namespace WebApi
{
    public sealed class PlayerResultProfile : DefaultAspNetCoreResultEndpointProfile
    {
        public override ActionResult TransformFailedResultToActionResult(FailedResultToActionResultTransformationContext context)
        {
            return base.TransformFailedResultToActionResult(context);
        }

        public override ActionResult TransformOkNoValueResultToActionResult(OkResultToActionResultTransformationContext<Result> context)
        {
            return base.TransformOkNoValueResultToActionResult(context);
        }

        public override ActionResult TransformOkValueResultToActionResult<T>(OkResultToActionResultTransformationContext<Result<T>> context)
        {
            return base.TransformOkValueResultToActionResult(context);
        }
    }
}

I get the error "System.MissingMethodException: "Method not found: 'System.Collections.Generic.List`1<FluentResults.IError> FluentResults.ResultBase.get_Errors()'."" every time I get to the Result.Fail method after branching.

For example, I posted a player with the id "str13131414ing". And this operation was successful, because the code passed all the checks and got to the Result.Ok method. However, when I try to create a player with exactly the same id, I do not pass the check and get to the Result.Fail method. As a result:

Image Image

In principle, the same thing happens in any REST methods when hitting Fail. And I want to note that with any request I hit one of the three overridden methods in the PlayerResultProfile class inherited from DefaultAspNetCoreResultEndpointProfile. I understand that this is how it is intended, but I only get an error with Result.Fail.

Now the most interesting part. If I go to the DefaultAspNetCoreResultEndpointProfile class and completely transfer its contents to my PlayerResultProfile class, all exceptions disappear.

using System.Linq;
using Microsoft.AspNetCore.Mvc;

namespace FluentResults.Extensions.AspNetCore
{
    public class DefaultAspNetCoreResultEndpointProfile : IAspNetCoreResultEndpointProfile
    {
        public virtual ActionResult TransformFailedResultToActionResult(FailedResultToActionResultTransformationContext context)
        {
            var result = context.Result;

            var errorDtos = result.Errors.Select(e => new ErrorDto
                                                      {
                                                          Message = e.Message
                                                      });

            return new BadRequestObjectResult(errorDtos);
        }

        public virtual ActionResult TransformOkNoValueResultToActionResult(OkResultToActionResultTransformationContext<Result> context)
        {
            return new OkResult();
        }

        public virtual ActionResult TransformOkValueResultToActionResult<T>(OkResultToActionResultTransformationContext<Result<T>> context)
        {
            return new OkObjectResult(context.Result.ValueOrDefault);
        }
    }
}

DeepSeek explained that this is due to a version mismatch in your library. Image

I used your documentation. In WebApi - the project with the ASP.NET part, where the controllers and Program.cs are, I added only NuGet FluentResults.Extensions.AspNetCore. In business logic - the project where the PlayerService class is located, which connects the ASP.NET layer and the layers with validation and the database, I added only NuGet FluentResults.

Image Image Image

Also, there is a small question. Why can't DI be added via services?

builder.Services.AddControllers()
    .AddFluentResults(options =>
    {
        options.ResultProfile = new PlayerResultProfile();
    });

PUDGE133 avatar Jul 22 '25 13:07 PUDGE133