Add MediatorOptions class and update SendEndpointRequestFilter to use options for request failure handling
This commit is contained in:
parent
544d23a397
commit
37cb52fe3e
@ -0,0 +1,8 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||
|
||||
public class MediatorOptions
|
||||
{
|
||||
public Func<EndpointFilterInvocationContext, object?, ValueTask<IResult>> OnRequestSendFailed { get; set; } = (_, _) => throw new NotImplementedException();
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Baguette.Extensions.AspNetCore.Mediator;
|
||||
|
||||
@ -8,6 +9,7 @@ public class SendEndpointRequestFilter<TResponse> : IEndpointFilter
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
var mediator = context.HttpContext.RequestServices.GetRequiredService<IMediatorService>();
|
||||
var options = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<MediatorOptions>>().Value;
|
||||
|
||||
var value = await next(context);
|
||||
|
||||
@ -18,7 +20,7 @@ public class SendEndpointRequestFilter<TResponse> : IEndpointFilter
|
||||
|
||||
return success
|
||||
? Results.Ok(response)
|
||||
: throw new InvalidOperationException(); // TODO: Handle case where request can't be sent or response can't be produced
|
||||
: await options.OnRequestSendFailed(context, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,6 +29,7 @@ public class SendEndpointRequestFilter : IEndpointFilter
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
var mediator = context.HttpContext.RequestServices.GetRequiredService<IMediatorService>();
|
||||
var options = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<MediatorOptions>>().Value;
|
||||
|
||||
var value = await next(context);
|
||||
|
||||
@ -35,6 +38,6 @@ public class SendEndpointRequestFilter : IEndpointFilter
|
||||
|
||||
return await mediator.TrySendAsync(value)
|
||||
? Results.Ok()
|
||||
: throw new InvalidOperationException(); // TODO: Handle case where request can't be sent
|
||||
: await options.OnRequestSendFailed(context, value);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user