Hi, I'm implementing a new interceptor on Core version of Abp to make a different kind of log. Initially it didn't work but I've read that for Core the method must be virtual. But I've also read in this post [https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=11098&p=28564&hilit=interceptors#p28564]) that you don't use interceptor. So, to stay in sync, how do you log every application service methods on Core ?
Thank you
18 Answer(s)
-
0
Similar to the AbpAuthorize mentioned in the post your provided.
Abp does the same thing for auditing as well. Using native filter available in Asp.Net Core Mvc , AbpAuditActionFilter works without Interceptor
See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.AspNetCore/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs">https://github.com/aspnetboilerplate/as ... nFilter.cs</a>
However, abp still has auditing function implemented using IInterceptor.
See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Auditing/AuditingInterceptor.cs">https://github.com/aspnetboilerplate/as ... rceptor.cs</a>
-
0
Thank you. But what I don't understand is that abpcore auditing intereceptor catch all my application service methods, but if I do the same thing, I cant catch only methods that are virtual.
Where is the magic ?
-
0
Filters in ASP.NET Core allow you to run code before or after specific stages in the request processing pipeline. Interceptors allow you to intercept even non-Controller methods, but rely on the power of Castle DynamicProxy.
ABP defines several pre-built filters for ASP.NET Core: https://aspnetboilerplate.com/Pages/Documents/AspNet-Core#filters
-
0
Thank you @aaron.
I think that in this case the filters in Core are the best option. So I've created my filter to log some particular methods, but how can I add my filters to abp core filters? Note that I'm developing a module (folder plugins)
-
0
You can register your filters with MvcOptions.Filters
See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.AspNetCore/AspNetCore/Mvc/AbpMvcOptionsExtensions.cs#L28-L36">https://github.com/aspnetboilerplate/as ... cs#L28-L36</a>
Looking at similar issue at <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3302">https://github.com/aspnetboilerplate/as ... ssues/3302</a>
Assemblies loading at <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp/PlugIns/FolderPlugInSource.cs#L36-L52">https://github.com/aspnetboilerplate/as ... cs#L36-L52</a>
You can try adding the following code snippets in StartUp.cs of your ..Web.Host or ..Web.Mvc. I have not fully tested out the code yet but it should be somewhere along the line.
services.PostConfigure<MvcOptions>(options => { foreach(var a in new FolderPlugInSource(@"Your Plugin Folder").GetAssemblies()) { foreach (var type in a.GetTypes()) { if (type is IFilterMetadata) { options.Filters.AddService(type); } } } });
-
0
Thank you @ryancyq
I'm sorry but I don't see any snippet of code suitable to add new filter outside of the abzero code, because in the plugin I've not access to services configuration like in startup with options.Filters.AddService(typeof(AbpAuthorizationFilter); or services.PostConfigure<MvcOptions>....
I mean something like : IocManager.Instance.Resolve<Filters>().Add(typeof(GdprFilter)) in preInitialize step of module.
The problem is how to add new filters in plugin module.
Thanks
-
0
The code snippet is to add in to your main project.
The purpose of it is to load all the filters that are declared in the plug-in module class library.
-
0
Yes but you have to modify the main source code and a we prefer don't touch the main project for many reasons, first of all the upgrade to new versions.
So briefly, you say that is not possibile to add a filter from a plugin module without modify the main code, right ?
-
0
It's still possible to add in your filters within AbpModule loaded via plug-in.
You can try to resolve MvcOption in your module PostInitialize(), and add your filters using _mvcOptions.Filters.AddService(type of(Your filter)), however this require further analysis and test if it works or not.
-
0
Hi, I've tried many ways but all fails :-) (No component for supporting the service xyz was found)
On PostBuild event:
IocManager.Resolve<MvcOptions>().Filters.Add(typeof(GdprAuditFilter)); // Error IocManager.Resolve<IServiceProvider>().GetService(typeof(IMvcBuilder)); // Error IocManager.Resolve<IServiceCollection>(); //Error
Can you help me?
-
0
Please share the full error stack trace and code for your plug-in module
Do you mean PostInitialize of AbpModule for PostBuild ? Or you are referring to Visual Studio PostBuild event.
-
0
I mean PostInitialize of AbpModule.
using Abp.Modules; using Abp.Reflection.Extensions; using Microsoft.AspNetCore.Mvc; using Uno.Gdpr.Application.Filters; using Uno.Gdpr.Core; using Uno.Gdpr.EntityFramework; namespace Uno.Gdpr.Application { [DependsOn(typeof(GdprCoreModule), typeof(GdprEntityFrameworkModule))] public class GdrpApplicationModule : AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(GdrpApplicationModule).GetAssembly()); } public override void PostInitialize() { IocManager.Resolve<MvcOptions>().Filters.AddService(typeof(GdprAuditFilter)); } } }
using System; using System.Linq; using System.Threading.Tasks; using Abp.AspNetCore.Mvc.Extensions; using Abp.Auditing; using Abp.Dependency; using Microsoft.AspNetCore.Mvc.Filters; using Uno.Gdpr.Application.Interceptors; using Uno.Gdpr.Core; using Uno.Gdpr.Core.WriteModel; namespace Uno.Gdpr.Application.Filters { public class GdprAuditFilter : IAsyncActionFilter, ITransientDependency { private readonly IAuditingHelper _auditingHelper; private readonly IGdprHelper _gdprHelper; public GdprAuditFilter(IAuditingHelper auditingHelper, IGdprHelper gdprHelper) { _auditingHelper = auditingHelper; _gdprHelper = gdprHelper; } public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // do something } } }
-
0
Hi, I'm in a black hole ? No ways to register filters on abp module?
-
0
show what you've done.
-
0
I've tried many ways on PostBuildEvent:
IocManager.Resolve<MvcOptions>().Filters.Add(typeof(GdprAuditFilter)); // Error IocManager.Resolve<MvcOptions>().Filters.AddService(typeof(GdprAuditFilter)); //Error IocManager.Resolve<IServiceProvider>().GetService(typeof(IMvcBuilder)); // Error IocManager.Resolve<IServiceCollection>(); //Error
-
0
Have you tried something like this;
services.Configure<MvcOptions>(options => { options.Filters.AddService(typeof(GdprAuditFilter), order: 1); });
-
0
@ismcagdas, please, bear with me but I'm in a module plugin and I haven't access to services configurations. Sure, if I could change the aspnetboilerplate code it will work, but I can't.
I'm on a module plugin.
-
0
@ivanosw1, sorry. Could you try something like below;
- In the Initialize method of your plugin module,
IocManager.IocContainer.Register( Component.For<IConfigureOptions<MvcOptions>>().Instance(new MyTestMvcConfigureOptions()).LifestyleSingleton() );
- Other classes,
public class MyTestMvcConfigureOptions : IConfigureOptions<MvcOptions> { public void Configure(MvcOptions options) { options.Filters.Add(new GdprAuditFilter()); } } public class GdprAuditFilter : IAsyncActionFilter { public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { throw new NotImplementedException(); } }