Skip to content

feat: Enhance SubscribeFilter to Support Multiple Instances #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DotNetCore.CAP/CAP.Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public CapBuilder(IServiceCollection services)
/// <typeparam name="T">Type of filter</typeparam>
public CapBuilder AddSubscribeFilter<T>() where T : class, ISubscribeFilter
{
Services.TryAddScoped<ISubscribeFilter, T>();
Services.AddScoped<ISubscribeFilter, T>();
return this;
}

Expand Down
38 changes: 22 additions & 16 deletions src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -87,40 +88,45 @@ public async Task<ConsumerExecutedResult> InvokeAsync(ConsumerContext context,
}
}

var filter = provider.GetService<ISubscribeFilter>();
var filters = provider.GetServices<ISubscribeFilter>().ToList();
Stack<ISubscribeFilter> executedFilters = new Stack<ISubscribeFilter>();

object? resultObj = null;
try
{
if (filter != null)
foreach (var filter in filters)
{
var etContext = new ExecutingContext(context, executeParameters);
await filter.OnSubscribeExecutingAsync(etContext).ConfigureAwait(false);
executeParameters = etContext.Arguments;
var ctx = new ExecutingContext(context, executeParameters);
await filter.OnSubscribeExecutingAsync(ctx).ConfigureAwait(false);
executeParameters = ctx.Arguments;
executedFilters.Push(filter);
}

resultObj = await ExecuteWithParameterAsync(executor, obj, executeParameters).ConfigureAwait(false);

if (filter != null)
while (executedFilters.Count > 0)
{
var edContext = new ExecutedContext(context, resultObj);
await filter.OnSubscribeExecutedAsync(edContext).ConfigureAwait(false);
resultObj = edContext.Result;
var filter = executedFilters.Peek();
var ctx = new ExecutedContext(context, resultObj);
await filter.OnSubscribeExecutedAsync(ctx).ConfigureAwait(false);
resultObj = ctx.Result;
executedFilters.Pop();
}
}
catch (Exception e)
catch (Exception ex)
{
if (filter != null)
if (executedFilters.Count == 0)
throw;
while (executedFilters.Count > 0)
{
var exContext = new ExceptionContext(context, e);
var exContext = new ExceptionContext(context, ex);
var filter = executedFilters.Pop();
await filter.OnSubscribeExceptionAsync(exContext).ConfigureAwait(false);

if (!exContext.ExceptionHandled) exContext.Exception.ReThrow();

if (exContext.Result != null) resultObj = exContext.Result;
}
else
{
throw;
}
}

var callbackName = message.GetCallbackName();
Expand Down