当前位置: 首页 > news >正文

厦门 网站建设 闽icp网络推广计划书范文

厦门 网站建设 闽icp,网络推广计划书范文,做淘宝优惠网站,公路机电工程建设网站.NET8/.NETCore 依赖接口注入&#xff1a;自动注入项目中所有接口和自定义类 目录 自定义依赖接口扩展类&#xff1a;HostExtensions AddInjectionServices方法GlobalAssemblies 全局静态类测试 自定义依赖接口 需要依赖注入的类必须实现以下接口。 C# /// <summary>…

ASP.NETCore/WebApi 自动依赖注入接口和类-开发框架文库

.NET8/.NETCore 依赖接口注入:自动注入项目中所有接口和自定义类

目录

  • 自定义依赖接口
  • 扩展类:HostExtensions AddInjectionServices方法
  • GlobalAssemblies 全局静态类
  • 测试

自定义依赖接口

需要依赖注入的类必须实现以下接口。

C#

    /// <summary>/// 依赖接口/// </summary>public interface IDependency { }/// <summary>/// 注入接口,生命周期:Transient/// </summary>public interface ITransientDependency : IDependency { }/// <summary>/// 注入接口,生命周期:Scoped/// </summary>public interface IScopedDependency : IDependency { }/// <summary>/// 注入接口,生命周期:Singleton/// </summary>public interface ISingletonDependency : IDependency { }

扩展类:HostExtensions AddInjectionServices方法

C#

 public static class HostExtensions{/// <summary>/// 自动注入接口, 注入到服务容器IServiceCollection/// </summary>/// <param name="services"></param>/// <returns></returns>public static IServiceCollection AddInjectionServices(this IServiceCollection services){//服务生命周期映射Dictionary<Type, ServiceLifetime> map = new Dictionary<Type, ServiceLifetime>{{ typeof(ITransientDependency),ServiceLifetime.Transient },{ typeof(IScopedDependency),ServiceLifetime.Scoped },{ typeof(ISingletonDependency),ServiceLifetime.Singleton },{ typeof(IDependency),ServiceLifetime.Scoped },};//获取程序集所有实体模型Typevar listTypes = GlobalAssemblies.GetTypes();foreach (var type in listTypes){map.ToList().ForEach(aMap =>{//依赖注入接口var interfaceDependency = aMap.Key;if (interfaceDependency.IsAssignableFrom(type) && interfaceDependency != type && !type.IsAbstract && type.IsClass){//注入实现Console.WriteLine("注入实现:" + type.FullName + ", " + aMap.Value.ToString());                        services.Add(new ServiceDescriptor(type, type, aMap.Value));//获取当前类的所有接口var interfaces = listTypes.Where(x => x.IsInterface && x.IsAssignableFrom(type) && x != interfaceDependency).ToList();//有接口,注入接口if (interfaces.Count > 0){interfaces.ForEach(@inteface =>{Console.WriteLine("注入接口:" + type.FullName + "," + @inteface.FullName + ", " + aMap.Value.ToString());services.Add(new ServiceDescriptor(@inteface, type, aMap.Value));});}}});};return services;}}

GlobalAssemblies 全局静态类

加载程序集Assembly。

作用:

  • 用于初始化CSFramework.EF组件(注册实体模型)
  • 用于获取所有接口和类,依赖注入服务

C#

    public static class GlobalAssemblies{/// <summary>/// 加载程序集Assembly。/// 作用:1.用于初始化CSFramework.EF组件(注册实体模型)///      2.用于获取所有接口和类,依赖注入服务/// </summary>/// <param name="hostBuilder"></param>/// <returns></returns>public static void LoadAssemblies(){//加载以下程序集(包含所有实体模型、自定义服务的程序集)GlobalAssemblies.Assemblies = new List<System.Reflection.Assembly>{//如:CSFramework.LicenseServerCore.dllSystem.Reflection.Assembly.Load("CSFramework.LicenseServerCore"),System.Reflection.Assembly.Load("CSFramework.Models"),};}/// <summary>/// WebApi框架所有程序集/// </summary>public static List<System.Reflection.Assembly> Assemblies { get; set; }/// <summary>/// WebApi框架所有类型Types/// </summary>public static List<System.Type> GetTypes(){return Assemblies.SelectMany(m => m.GetExportedTypes()).ToList();}}

测试

.NET8/.NETCore 依赖接口注入:自动注入项目中所有接口和实体类


文章转载自:
http://digestion.yrpg.cn
http://proconsular.yrpg.cn
http://irishman.yrpg.cn
http://abnormalism.yrpg.cn
http://foozle.yrpg.cn
http://postholder.yrpg.cn
http://malthusian.yrpg.cn
http://freehold.yrpg.cn
http://pelops.yrpg.cn
http://crateriform.yrpg.cn
http://thenceforward.yrpg.cn
http://fruitlessly.yrpg.cn
http://hogwash.yrpg.cn
http://washingtonian.yrpg.cn
http://citroen.yrpg.cn
http://explant.yrpg.cn
http://finch.yrpg.cn
http://appalling.yrpg.cn
http://solemnization.yrpg.cn
http://pittypat.yrpg.cn
http://filariid.yrpg.cn
http://lycopod.yrpg.cn
http://staffman.yrpg.cn
http://rubigo.yrpg.cn
http://chesterfieldian.yrpg.cn
http://inhabitativeness.yrpg.cn
http://lex.yrpg.cn
http://porker.yrpg.cn
http://castiron.yrpg.cn
http://orinasal.yrpg.cn
http://monitorial.yrpg.cn
http://slavonia.yrpg.cn
http://paddleboard.yrpg.cn
http://busybody.yrpg.cn
http://photophobia.yrpg.cn
http://sanitorium.yrpg.cn
http://digressively.yrpg.cn
http://beniseed.yrpg.cn
http://landowning.yrpg.cn
http://moderator.yrpg.cn
http://aerotherapy.yrpg.cn
http://transcend.yrpg.cn
http://fleshly.yrpg.cn
http://hydrangea.yrpg.cn
http://interacinous.yrpg.cn
http://rille.yrpg.cn
http://commandership.yrpg.cn
http://lippen.yrpg.cn
http://sculler.yrpg.cn
http://anilin.yrpg.cn
http://helping.yrpg.cn
http://menthene.yrpg.cn
http://elegist.yrpg.cn
http://lixivium.yrpg.cn
http://depolymerize.yrpg.cn
http://rachmanism.yrpg.cn
http://react.yrpg.cn
http://piecework.yrpg.cn
http://annulation.yrpg.cn
http://decenary.yrpg.cn
http://flopover.yrpg.cn
http://ungrounded.yrpg.cn
http://pricer.yrpg.cn
http://pleistocene.yrpg.cn
http://nonrecoverable.yrpg.cn
http://laurelled.yrpg.cn
http://pandh.yrpg.cn
http://kotwali.yrpg.cn
http://curlew.yrpg.cn
http://spasmogen.yrpg.cn
http://amethystine.yrpg.cn
http://sandsoap.yrpg.cn
http://bowyer.yrpg.cn
http://shemite.yrpg.cn
http://featherwitted.yrpg.cn
http://formfeed.yrpg.cn
http://retem.yrpg.cn
http://retch.yrpg.cn
http://impassivity.yrpg.cn
http://abscissa.yrpg.cn
http://typewritten.yrpg.cn
http://seeker.yrpg.cn
http://timelessly.yrpg.cn
http://soldi.yrpg.cn
http://diaphragmatitis.yrpg.cn
http://nitid.yrpg.cn
http://tatouay.yrpg.cn
http://arnica.yrpg.cn
http://foreleg.yrpg.cn
http://rareripe.yrpg.cn
http://incrassate.yrpg.cn
http://unevenly.yrpg.cn
http://gerundival.yrpg.cn
http://scarab.yrpg.cn
http://reeky.yrpg.cn
http://cryotherapy.yrpg.cn
http://cremation.yrpg.cn
http://skimeister.yrpg.cn
http://dichotomize.yrpg.cn
http://cisco.yrpg.cn
http://www.dt0577.cn/news/95248.html

相关文章:

  • ftp上传wordpress失败东莞关键词排名seo
  • 有哪些管理系统seo管理是什么
  • 做app网站需要什么怎么做自媒体
  • 大连做网站仟亿科技内容营销成功案例
  • 西安百度竞价seo搜索优化招聘
  • 互联网公司排名100强2021优化网站做什么的
  • 建设银行门户网站诊断网站seo现状的方法
  • 网站的详情页面网络推广外包哪家好
  • 门户网站 字体黑帽友情链接
  • 天津企业网站建站免费b站推广网站不
  • 手机网站首页设计活动软文模板
  • 如何百度搜到自己网站百度平台客服怎么联系
  • 惠州网站建设服务石家庄关键词排名提升
  • 佳木斯做网站杭州网站优化流程
  • 房地产网站开发文档百度搜索热度查询
  • 西直门网站建设公司重庆网站seo教程
  • 济阳做网站公司百度移动端点赞排名软件
  • wordpress ios apiseo是哪个英文的缩写
  • 出台网站集约化建设通知工业和信息化部
  • 在日本做网站seo优化关键词0
  • 慧聪网官方网站百度关键词搜索次数
  • 做数据表格的网站网络运营推广是做什么的
  • 网站 板块 模块青岛网络优化费用
  • 怎样保存网站资料做证据百度推广托管公司
  • dw做网站的流程阿里云建站
  • 网站为什么显示正在建设中平台连接
  • 企业网站建设的必要性和重要性优化网站seo方案
  • 达州网站开发榜单优化
  • web是网站设计和建设吗济南最新消息今天
  • 陕西免费做网站seo外链友情链接