Fireasy3 揭秘 — 使用 SourceGeneraor 改进服务发现

  • Fireasy3 揭秘 — 使用 SourceGeneraor 改进服务发现已关闭评论
  • 103 次浏览
  • A+
所属分类:.NET技术
摘要

  在 Fireasy3 揭秘 — 依赖注入与服务发现 这篇中,我们通过遍列程序集中的所有类,来查找三个类型的服务接口,这样应用在启动时会消耗一定的时间来处理这些事情。今天,我们将用 ISourceGenerator 来对它进行改进。
  ISourceGenerator 是 Microsoft.CodeAnalysis.Analyzers 中的一项技术,它是基于代码分析的原理,在语法树中查找所需要的内容,通过这些内容再构造一段源代码,使得我们在编译程序集的时候,把这些代码一并编译进去。使用它的好处在于,它是在编译时生成的,而不像 Emit 或其他反射等方法来构建的动态代码一样,在运行时将耗费一定的性能。
  需要新建一个 .net standard 2.0 的项目,并引入 Microsoft.CodeAnalysis.Analyzers 和 Microsoft.CodeAnalysis.CSharp,见 Fireasy.Common.Analyzers。
  在项目里添加一个类,实现 ISourceGenerator 接口,如下:


目录


  在 Fireasy3 揭秘 -- 依赖注入与服务发现 这篇中,我们通过遍列程序集中的所有类,来查找三个类型的服务接口,这样应用在启动时会消耗一定的时间来处理这些事情。今天,我们将用 ISourceGenerator 来对它进行改进。
  ISourceGeneratorMicrosoft.CodeAnalysis.Analyzers 中的一项技术,它是基于代码分析的原理,在语法树中查找所需要的内容,通过这些内容再构造一段源代码,使得我们在编译程序集的时候,把这些代码一并编译进去。使用它的好处在于,它是在编译时生成的,而不像 Emit 或其他反射等方法来构建的动态代码一样,在运行时将耗费一定的性能。
  需要新建一个 .net standard 2.0 的项目,并引入 Microsoft.CodeAnalysis.AnalyzersMicrosoft.CodeAnalysis.CSharp,见 Fireasy.Common.Analyzers
  在项目里添加一个类,实现 ISourceGenerator 接口,如下:

    [Generator]     public class ServiceDiscoverGenerator : ISourceGenerator     {         void ISourceGenerator.Initialize(GeneratorInitializationContext context)         {             Debugger.Launch();             context.RegisterForSyntaxNotifications(() => new ServiceDiscoverSyntaxReceiver());         }          void ISourceGenerator.Execute(GeneratorExecutionContext context)         {         }     } 

  Initialize 方法用于初始化生成器,使用 RegisterForSyntaxNotifications 向上下文注入一人上语法接收器,以便用来分析语法树。这里的语法接收器有两种,分别是 ISyntaxReceiverISyntaxContextReceiver,后者可以从上下文中获取到 SemanticModel 对象,这样的话能够从语法节点中获取到定义的符号模型。使用符号模型相对于语法节点来说要更方便一些。下面是基于 ISyntaxContextReceiver 接口的语法接收器。

    internal class ServiceDiscoverSyntaxReceiver : ISyntaxContextReceiver     {         private const string SingletonServiceName = "Fireasy.Common.DependencyInjection.ISingletonService";         private const string TransientServiceName = "Fireasy.Common.DependencyInjection.ITransientService";         private const string ScopedServiceName = "Fireasy.Common.DependencyInjection.IScopedService";         private const string RegisterAttributeName = "Fireasy.Common.DependencyInjection.ServiceRegisterAttribute";          private List<ClassMetadata> _metadatas = new();          void ISyntaxContextReceiver.OnVisitSyntaxNode(GeneratorSyntaxContext context)         {             if (context.Node is ClassDeclarationSyntax classSyntax)             {                 AnalyseClassSyntax(context.SemanticModel, classSyntax);             }         }     } 

  OnVisitSyntaxNode 方法正如 lambda 表达式树的 ExpressionVisitor 一样,语法树中的每一个节点都会被它访问到。我们需要分析的是类,因此只需要处理 ClassDeclarationSyntax 语法即可。AnalyseClassSyntax 方法如下:

        /// <summary>         /// 分析类型语法。         /// </summary>         /// <param name="model"></param>         /// <param name="syntax"></param>         private void AnalyseClassSyntax(SemanticModel model, ClassDeclarationSyntax syntax)         {             var typeSymbol = (ITypeSymbol)model.GetDeclaredSymbol(syntax)!;              var interfaces = typeSymbol.Interfaces;              //判断是否使用了 特殊             var regAttr = typeSymbol.GetAttributes().FirstOrDefault(s => s.AttributeClass!.ToDisplayString() == RegisterAttributeName);              var lifetime = string.Empty;             if (regAttr != null)             {                 lifetime = GetLifetime((int)regAttr.ConstructorArguments[0].Value!);             }             else if (interfaces.Any(s => s.ToDisplayString() == SingletonServiceName))             {                 lifetime = "Singleton";             }             else if (interfaces.Any(s => s.ToDisplayString() == TransientServiceName))             {                 lifetime = "Transient";             }             else if (interfaces.Any(s => s.ToDisplayString() == ScopedServiceName))             {                 lifetime = "Scoped";             }              if (!string.IsNullOrEmpty(lifetime))             {                 var serviceTypes = GetServiceTypes(interfaces).ToList();                  //如果没有实现任何接口,则判断基类是不是抽象类,如果不是,则注册自己                 if (serviceTypes.Count == 0 && (typeSymbol.BaseType?.Name == "Object" || typeSymbol.BaseType?.IsAbstract == false))                 {                     serviceTypes.Add(typeSymbol);                 }                  _metadatas.Add(new ClassMetadata(typeSymbol, lifetime).AddServiceTypes(serviceTypes));             }         }          /// <summary>         /// 获取生命周期。         /// </summary>         /// <param name="value"></param>         /// <returns></returns>         private string GetLifetime(int value) => value switch         {             0 => "Singleton",             1 => "Scoped",             2 => "Transient",             _ => string.Empty         };          /// <summary>         /// 从接口中筛选出服务类。         /// </summary>         /// <param name="types"></param>         /// <returns></returns>         private IEnumerable<ITypeSymbol> GetServiceTypes(IEnumerable<INamedTypeSymbol> types)         {             foreach (var type in types)             {                 if (type.ToDisplayString() == SingletonServiceName ||                     type.ToDisplayString() == TransientServiceName ||                     type.ToDisplayString() == ScopedServiceName)                 {                     continue;                 }                  yield return type;             }         } 

  至此,我们就得到了一份可注册的元数据,它由一个实现类对应多个服务类。ClassMetadata 的定义如下:

    /// <summary>     /// 类的元数据。     /// </summary>     public class ClassMetadata     {         /// <summary>         /// 初始化 <see cref="ClassMetadata"/> 类的新实例。         /// </summary>         /// <param name="implementationType">实现类的类型。</param>         /// <param name="lifetime">生命周期。</param>         public ClassMetadata(ITypeSymbol implementationType, string lifetime)         {             ImplementationType = implementationType;             Lifetime = lifetime;         }          /// <summary>         /// 获取实现类的类型。         /// </summary>         public ITypeSymbol ImplementationType { get; }          /// <summary>         /// 获取服务类的类型列表。         /// </summary>         public List<ITypeSymbol> ServiceTypes { get; } = new();          /// <summary>         /// 获取生命周期。         /// </summary>         public string Lifetime { get; }          /// <summary>         /// 添加服务类型。         /// </summary>         /// <param name="serviceTypes">服务类型列表。</param>         /// <returns></returns>         public ClassMetadata AddServiceTypes(IEnumerable<ITypeSymbol> serviceTypes)         {             ServiceTypes.AddRange(serviceTypes);              return this;         }     } 

  好了,得到这一份元数据后,我们转到 ServiceDiscoverGenerator,看看下一步它要做什么。

    [Generator]     public class ServiceDiscoverGenerator : ISourceGenerator     {         void ISourceGenerator.Initialize(GeneratorInitializationContext context)         {             Debugger.Launch();             context.RegisterForSyntaxNotifications(() => new ServiceDiscoverSyntaxReceiver());         }          void ISourceGenerator.Execute(GeneratorExecutionContext context)         {             if (context.SyntaxContextReceiver is ServiceDiscoverSyntaxReceiver receiver)             {                 var metadatas = receiver.GetMetadatas();                  if (metadatas.Count > 0)                 {                     context.AddSource("ServicesDiscover.cs", BuildDiscoverSourceCode(metadatas));                 }             }         }     } 

  在 Execute 方法中,拿到接收器分析出来的元数据,通过 BuildDiscoverSourceCode 方法去生成一段源代码。它是一个服务部署类,在 Configure 方法中,会把所有的服务描述添加到 IServiceCollection 容器内,如下:

        private SourceText BuildDiscoverSourceCode(List<ClassMetadata> metadatas)         {             var sb = new StringBuilder();             sb.AppendLine(@" using Fireasy.Common.DependencyInjection; using Fireasy.Common.DynamicProxy; using Microsoft.Extensions.DependencyInjection;  [assembly: Fireasy.Common.DependencyInjection.ServicesDeployAttribute(typeof(__ServiceDiscoverNs.__ServiceDiscoverServicesDeployer), Priority = 1)]  namespace __ServiceDiscoverNs {     internal class __ServiceDiscoverServicesDeployer: IServicesDeployer     {         void IServicesDeployer.Configure(IServiceCollection services)         {");              foreach (var metadata in metadatas)             {                 foreach (var svrType in metadata.ServiceTypes)                 {                     sb.AppendLine($"            services.Add{metadata.Lifetime}(typeof({GetTypeName(svrType)}), typeof({GetTypeName(metadata.ImplementationType)}));");                 }             }             sb.AppendLine(@"         }     } }");             return SourceText.From(sb.ToString(), Encoding.UTF8);         }          private string GetTypeName(ITypeSymbol symbol)         {             if (symbol is INamedTypeSymbol namedTypeSymbol)             {                 //如果是泛型,要处理成 Any<> 或 Any<,> 这样的描述                 if (namedTypeSymbol.IsGenericType)                 {                     var t = namedTypeSymbol.ToDisplayString();                     return t.Substring(0, t.IndexOf("<") + 1) + new string(',', namedTypeSymbol.TypeArguments.Length - 1) + ">";                 }             }              return symbol.ToDisplayString();         } 

  到这里,源代码生成器就算是完成了,那接下来怎么让它工作呢?
  首先,我们需要找到一个“宿主”,我之所以这么称呼,是因为 nuget 打包时,需要将分析器依附到一个包内,因此我选择 Fireasy.Common,在 Fireasy.Common 的项目文件中,加下以下一段代码,它的目的是当 Fireasy.Common 打包时,Fireasy.Common.Analyzers.dll 会自动打包到 analyzers 目录下,引用 Fireasy.Common 包时,会自动使用该分析器来生成代码。如下:

<Project Sdk="Microsoft.NET.Sdk">   <Target Name="_IncludeAllDependencies" BeforeTargets="_GetPackageFiles">     <ItemGroup>         <None Include="..Fireasy.Common.Analyzersbin$(Configuration)***.dll" Pack="True" PackagePath="analyzersdotnetcs" />     </ItemGroup>   </Target> </Project> 

  我们测试的时候,因为是直接引用的项目,因此需要引用包含分析器的项目,而且要加上 OutputItemTypeReferenceOutputAssembly,如下:

<Project Sdk="Microsoft.NET.Sdk">   <ItemGroup>     <ProjectReference Include="....librariesFireasy.Common.AnalyzersFireasy.Common.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />   </ItemGroup> </Project> 

  好了,编译测试项目,使用 ILSpy 反编译 dll 文件,你会发现,实现了 ISingletonServiceITransientService 或 IScopedService 的类自动注册进来了:

// __ServiceDiscoverNs.__ServiceDiscoverServicesDeployer using Fireasy.Common.DependencyInjection; using Fireasy.Common.Tests; using Microsoft.Extensions.DependencyInjection;  void IServicesDeployer.Configure(IServiceCollection services) { 	services.AddSingleton(typeof(DependencyInjectionTests.ITestSingletonService), typeof(DependencyInjectionTests.TestSingletonServiceImpl)); 	services.AddTransient(typeof(DependencyInjectionTests.ITestTransientService), typeof(DependencyInjectionTests.TestTransientServiceImpl)); 	services.AddScoped(typeof(DependencyInjectionTests.ITestScopedService), typeof(DependencyInjectionTests.TestScopedServiceImpl)); 	services.AddTransient(typeof(DependencyInjectionTests.ITestWithRegisterAttr), typeof(DependencyInjectionTests.TestWithRegisterAttrImpl)); 	services.AddTransient(typeof(DependencyInjectionTests.TestWithRegisterAttrNonIntefaceImpl), typeof(DependencyInjectionTests.TestWithRegisterAttrNonIntefaceImpl)); 	services.AddTransient(typeof(DependencyInjectionTests.IGenericService<, >), typeof(DependencyInjectionTests.GenericService<, >)); 	services.AddTransient(typeof(DependencyInjectionTests.TestDynamicProxyClass), typeof(DependencyInjectionTests.TestDynamicProxyClass)); 	services.AddTransient(typeof(ObjectActivatorTests.ITestService), typeof(ObjectActivatorTests.TestService)); } 

  另外还有一个小窍门,在测试项目的“依赖项”--“分析器”下,你会看到一个属于自己的分析器,依次展开,也会找到所生成的那个代码文件。

  最后,奉上 Fireasy 3 的开源地址:https://gitee.com/faib920/fireasy3 ,欢迎大家前来捧场。

  本文相关代码请参考:
  https://gitee.com/faib920/fireasy3/src/libraries/Fireasy.Common.Analyzers/ServiceDiscover
  https://gitee.com/faib920/fireasy3/tests/Fireasy.Common.Tests/DependencyInjectionTests.cs

  更多内容请移步官网 http://www.fireasy.cn 。