Open Source Web Application Framework for ASP.NET Core. Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET and the ASP.NET Core platforms. Provides the fundamental infrastructure, production-ready startup templates, application modules, UI themes, tooling, guides and documentation.
Even though there is no official documentation about this, I tried to add an interceptor to one of my app service methods. Although I can confirm the interceptor has been added to the list of interceptors via IOnServiceRegistredContext.Interceptors property, it is not being used.
(BTW: I want to try to get around the problem described here: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3221 so I'm going to share the code exactly as is at the moment. If you think there is a better way other than trying with interceptor, please be free to point me in another direction..)
To reproduce
Define custom attribute to add to the method which should be intercepted
[AttributeUsage(AttributeTargets.Method)]
public class RetryOnTransientErrorAttribute : Attribute
{
}
Define the interceptor
public class RetryOnTransientErrorInterceptor : AbpInterceptor, ITransientDependency
{
private readonly ResilientDbActionHandler resilientDbActionHandler;
public RetryOnTransientErrorInterceptor(ResilientDbActionHandler resilientDbActionHandler)
{
this.resilientDbActionHandler = resilientDbActionHandler;
}
public override async Task InterceptAsync(IAbpMethodInvocation invocation)
{
if (!invocation.Method.ShouldRetryAction())
{
await invocation.ProceedAsync();
return;
}
await resilientDbActionHandler.ExecuteUsingRetryAsync(invocation.ProceedAsync);
}
}
Silly me. app services are not constructed via interfaces, but directly via implementations.
If I mark the method I want to intercept virtual, then it works fine.
ABP version: 4.4
--
Even though there is no official documentation about this, I tried to add an interceptor to one of my app service methods. Although I can confirm the interceptor has been added to the list of interceptors via
IOnServiceRegistredContext.Interceptors
property, it is not being used.(BTW: I want to try to get around the problem described here: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3221 so I'm going to share the code exactly as is at the moment. If you think there is a better way other than trying with interceptor, please be free to point me in another direction..)
To reproduce
RegisterIfNeeded
inPreConfigureServices
in my application module[RetryOnTransientError]
You will observe that the interceptor would not be used.
Additional information
If I manually resolve my app service using the service interface, the interceptor is used.
Workaround
As a workaround which can be used for app service actions, I made an action filter like this:
and registered it in my host module:
Unrelated: If, by any chance, someone might want to know how I implemented the
ResilientDbActionHandler
, here it is:The referenced
ShouldRetryAction
would be written like:where
ITransientDbExceptionDetector
is simply:with implementation suitable for Npgsql.EntityFrameworkCore users: