AspectCore.Extensions.DependencyInjection.NAutowired 3.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AspectCore.Extensions.DependencyInjection.NAutowired --version 3.1.0                
NuGet\Install-Package AspectCore.Extensions.DependencyInjection.NAutowired -Version 3.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AspectCore.Extensions.DependencyInjection.NAutowired" Version="3.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspectCore.Extensions.DependencyInjection.NAutowired --version 3.1.0                
#r "nuget: AspectCore.Extensions.DependencyInjection.NAutowired, 3.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AspectCore.Extensions.DependencyInjection.NAutowired as a Cake Addin
#addin nuget:?package=AspectCore.Extensions.DependencyInjection.NAutowired&version=3.1.0

// Install AspectCore.Extensions.DependencyInjection.NAutowired as a Cake Tool
#tool nuget:?package=AspectCore.Extensions.DependencyInjection.NAutowired&version=3.1.0                

AspectCore-NAutowired

An extension library to integrate NAutowired and AspectCore framework into Asp.NET Core DI system.

一个基于AspectCore.DependencyInjection的扩展库,使NAutowired可以和AspectCore一起使用。 这样被代理的原始服务仍可以正常使用[Autowired]实现属性注入。

安装

Install-Package AspectCore.Extensions.DependencyInjection.NAutowired

示例

鉴于AspectCore文档较老,这里示例一下如何在.NET Core 5.0中使用AspectCore。

基于特性拦截

//Intercepter
public class DefaultInterceptorAttribute : AbstractInterceptorAttribute
{
    public async override Task Invoke(AspectContext context, AspectDelegate next)
    {
        Console.WriteLine("before service call");
        await next(context);
        Console.WriteLine("after service call");
    }
}
//Service
public interface IFooService
{
    [DefaultInterceptor]
    public string GetText();
}

public class FooService : IFooService
{
    [Autowired]
    private readonly IDAL dal;

    public string GetText()
    {
        return dal != null ? "Success" : "Fail";
    }
}
//Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        //省略其它
        //启用AspectCore
        .UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());

全局拦截

//Intercepter
public class DefaultInterceptorAttribute : AbstractInterceptorAttribute
{
    public async override Task Invoke(AspectContext context, AspectDelegate next)
    {
        Console.WriteLine("before service call");
        await next(context);
        Console.WriteLine("after service call");
    }
}
//Service
public interface IFooService
{
    public string GetText();
}

public class FooService : IFooService
{
    [Autowired]
    private readonly IDAL dal;

    public string GetText()
    {
        return dal != null ? "Success" : "Fail";
    }
}
//Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        //省略其它
        .UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureDynamicProxy(config => {
        //拦截所有Execure开头的方法
        config.Interceptors.AddTyped<DefaultInterceptorAttribute>(Predicates.ForMethod("Execute*"));

        //拦截所有Service结尾的服务内的方法
        config.Interceptors.AddTyped<CustomInterceptorAttribute>(Predicates.ForService("*Service"));

        //App1命名空间下的Service不会被代理
        config.NonAspectPredicates.AddNamespace("App1");

        //最后一级为App1的命名空间下的Service不会被代理
        config.NonAspectPredicates.AddNamespace("*.App1");

        //ICustomService接口不会被代理
        config.NonAspectPredicates.AddService("ICustomService");

        //后缀为Svc的接口和类不会被代理
        config.NonAspectPredicates.AddService("*Svc");

        //命名为Query的方法不会被代理
        config.NonAspectPredicates.AddMethod("Query");

        //后缀为Query的方法不会被代理
        config.NonAspectPredicates.AddMethod("*Query");
    });
}

完整示例

请参看TestConsoleApp示例项目。

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on AspectCore.Extensions.DependencyInjection.NAutowired:

Package Downloads
EasyCaching.ExpressionInterceptor.AspectCore.NAutowired

A simple caching AOP extension library based on AspectCore with NAutowired enabled.

AspectCore.Extensions.Hosting.NAutowired

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.1 472 9/12/2024
3.1.0 4,117 4/14/2023
3.0.0 5,042 2/19/2022
2.3.0 670 2/14/2022

Interceptor and dynamicProxy support for Microsoft.Extensions.DependencyInjection via AspectCore Framework with NAutowired enabled.