AspectCore.Extensions.Hosting.NAutowired 3.1.1

dotnet add package AspectCore.Extensions.Hosting.NAutowired --version 3.1.1                
NuGet\Install-Package AspectCore.Extensions.Hosting.NAutowired -Version 3.1.1                
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.Hosting.NAutowired" Version="3.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspectCore.Extensions.Hosting.NAutowired --version 3.1.1                
#r "nuget: AspectCore.Extensions.Hosting.NAutowired, 3.1.1"                
#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.Hosting.NAutowired as a Cake Addin
#addin nuget:?package=AspectCore.Extensions.Hosting.NAutowired&version=3.1.1

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

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 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.1 112 9/12/2024
3.1.0 226 4/14/2023
3.0.0 451 2/19/2022
2.3.0 429 2/14/2022