JasperFx / wolverine

Supercharged .NET server side development!
https://wolverinefx.net
MIT License
1.21k stars 131 forks source link

This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode #970

Closed lamelyan closed 1 month ago

lamelyan commented 1 month ago

Describe the bug Trying to move query logic into handlers. I run the following code and get a message:

This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode

It sounds like, I can't call IMessage InvokeAsync in MediatorOnly mode...

Here is my set up.

Given this handler:

public class GetProjectsHandler(IQuerySession session)
{
    public async Task<IReadOnlyList<ProjectOverview>> Handle(Guid userId)
    {
        var projects = await session.Query<Project>()
            .Where(p => p.UserId == userId)
            .Select(project => new ProjectOverview
            {
                Id = project.Id,
                UserId = project.UserId,
                Name = project.Name,
                Category = project.Category
            })
            .ToListAsync();

        return projects;
    }
}

when called from the controller:

[HttpGet("/projects", Name = "GetProjects")]
    public async Task<ActionResult<IReadOnlyList<ProjectOverview>>> Get(IMessageBus bus)
    {
        var userId = Guid.Parse(HttpContext.Items["userId"].ToString());

        var projects = await bus.InvokeAsync<IReadOnlyList<ProjectOverview>>(userId);

        return Ok(projects);
    }

I get the following error message:

System.Guid[0] Invocation failed! System.InvalidOperationException: This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode

In Program.cs it's wired like so:

builder.Host.UseWolverine(opts => 
{
    // Automatically Commit Marten transactions
    opts.Policies.AutoApplyTransactions();
    // Using Wolverine as Mediator
    opts.Durability.Mode = DurabilityMode.MediatorOnly;
});
lamelyan commented 1 month ago

Full stack trace:

fail: System.Guid[0] Invocation failed! System.InvalidOperationException: This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode at Wolverine.Runtime.MessageBus.assertNotMediatorOnly() in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 36 at Wolverine.Runtime.MessageBus.PublishAsync[T](T message, DeliveryOptions options) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 161 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 370 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 354 at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Wolverine.Runtime.Handlers.Executor.InvokeAsync(MessageContext context, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 202 fail: System.Guid[0] Inline invocation of 01906f7b-05b5-4711-92d2-a6180dbf6e05 failed System.InvalidOperationException: This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode at Wolverine.Runtime.MessageBus.assertNotMediatorOnly() in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 36 at Wolverine.Runtime.MessageBus.PublishAsync[T](T message, DeliveryOptions options) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 161 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 370 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 354 at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Wolverine.Runtime.Handlers.Executor.InvokeAsync(MessageContext context, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 202 at Wolverine.Runtime.Handlers.Executor.InvokeAsync(MessageContext context, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 212 at Wolverine.Runtime.Handlers.Executor.InvokeInlineAsync(Envelope envelope, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 93 fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. System.InvalidOperationException: This operation is not allowed with Wolverine is bootstrapped in MediatorOnly mode at Wolverine.Runtime.MessageBus.assertNotMediatorOnly() in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 36 at Wolverine.Runtime.MessageBus.PublishAsync[T](T message, DeliveryOptions options) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageBus.cs:line 161 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 370 at Wolverine.Runtime.MessageContext.EnqueueCascadingAsync(Object message) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/MessageContext.cs:line 354 at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Internal.Generated.WolverineHandlers.GuidHandler418050186.HandleAsync(MessageContext context, CancellationToken cancellation) at Wolverine.Runtime.Handlers.Executor.InvokeAsync(MessageContext context, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 202 at Wolverine.Runtime.Handlers.Executor.InvokeAsync(MessageContext context, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 212 at Wolverine.Runtime.Handlers.Executor.InvokeInlineAsync(Envelope envelope, CancellationToken cancellation) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 93 at Wolverine.Runtime.Handlers.Executor.InvokeAsync[T](Object message, MessageBus bus, CancellationToken cancellation, Nullable`1 timeout, String tenantId) in /home/runner/work/wolverine/wolverine/src/Wolverine/Runtime/Handlers/Executor.cs:line 129 at NorthStar.Server.Controllers.ProjectController.Get(IMessageBus bus) in /Users/lamelyan/repos/NorthStar/northstar.server/NorthStar.API/Controllers/ProjectController.cs:line 30 at lambda_method89(Closure, Object) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gLogged|12_1(ControllerActionInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gLogged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at NorthStar.Server.Middleware.Users.UserIdFromTokenMiddleware.InvokeAsync(HttpContext context) in /Users/lamelyan/repos/NorthStar/northstar.server/NorthStar.API/Middleware/Users/UserIdFromTokenMiddleware.cs:line 37 at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

lamelyan commented 1 month ago

It looks like this is the line that does this check. However, it's in the method PublishAsync method. I'm not calling it directly...

jeremydmiller commented 1 month ago

Meh, I see it. Easy fix when I can get there. Thanks for the repro!

jeremydmiller commented 1 month ago

@lamelyan Sorry for the delay, finally got back to wolverine fixes after a vacation. Fix coming out soon.

Just to let you know though, I'm not recommending the "Wolverine as MediatR replacement" approach except as an intermediate step. I think you can get a lot simpler code by going all Wolverine.HTTP. And wolverine is a touch weak with the InvokeAsync() return value. That causes folks some confusion sometimes.