Asp.Net Core 自动注册Service

生命周期描述:

Transient  瞬时,每个请求都会创建一个实例,同一个请求,使用多次server,用的不同实例。(轻量级无状态服务尽量用Transient)
Scoped     作用域,在同一个Scoped内只初始化一个实例,同一个请求,使用多次server,就用的同一个实例。(日常用Scoped)
Singleton  单例,整个应用程序生命周期内只创建一个实例。

1、创建Service扩展,创建ServiceAttribute类

注意:ServiceLifetime需要引用 Microsoft.Extensions.DependencyInjection

    /// <summary>
    /// 定义生命周期
    /// 自动注册service
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class ServiceAttribute : Attribute
    {
        public ServiceLifetime LifeTime { get; set; }
        /// <summary>
        /// 请求模式
        /// </summary>
        /// <param name="serviceLifetime">默认Scoped</param>
        /// Transient  瞬时,每个请求都会创建一个实例,同一个请求,使用多次server,用的不同实例。(轻量级无状态服务尽量用Transient)
        /// Scoped     作用域,在同一个Scoped内只初始化一个实例,同一个请求,使用多次server,就用的同一个实例。(日常用Scoped)
        /// Singleton  单例,整个应用程序生命周期内只创建一个实例
        public ServiceAttribute(ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            LifeTime = serviceLifetime;
        }
    }

2、注入业务组件,创建ServiceCollectionExpand类

public static class ServiceCollectionExpand
    {
        /// <summary>
        /// 按特性中的生命周期注入业务组件
        /// </summary>
        /// <param name="service"></param>
        public static void AddBusiness(this IServiceCollection service)
        {
            //获取有ServiceAttribute特性的所有类
            List<Type> types = AppDomain.CurrentDomain
                .GetAssemblies()
                .SelectMany(x => x.GetTypes())
                .Where(t => t.IsClass && !t.IsAbstract && t.GetCustomAttributes(typeof(ServiceAttribute), false).Length > 0)
                .ToList();

            types.ForEach(impl => {
                //获取该类所继承的所有接口
                Type[] interfaces = impl.GetInterfaces();
                //获取该类注入的生命周期
                var lifetime = impl.GetCustomAttribute<ServiceAttribute>().LifeTime;
                interfaces.ToList().ForEach(i => {
                    switch (lifetime)
                    {
                        case ServiceLifetime.Singleton:
                            service.AddSingleton(i, impl);
                            break;
                        case ServiceLifetime.Scoped:
                            service.AddScoped(i, impl);
                            break;
                        case ServiceLifetime.Transient:
                            service.AddTransient(i, impl);
                            break;
                    }
                });
            });
        }
    }

3、在业务逻辑层的Service里引入 [Service()]

4、在Startup.cs文件中引入 services.AddBusiness();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Introduction Welcome to the Azure Development Lifecycle guide for .NET! This guide introduces the basic concepts of building a development lifecycle around Azure using .NET tools and processes. After finishing this guide, you’ll reap the benefits of a mature DevOps toolchain. Who this guide is for You should be an experienced ASP.NET developer (200-300 level). You don’t need to know anything about Azure, as we’ll cover that in this introduction. This guide may also be useful for DevOps engineers who are more focused on operations than development. This guide targets Windows developers. However, Linux and macOS are fully supported by .NET Core. To adapt this guide for Linux/macOS, watch for callouts for Linux/macOS differences. What this guide doesn’t cover This guide is focused on an end-to-end continuous deployment experience for .NET developers. It’s not an exhaustive guide to all things Azure, and it doesn’t focus extensively on .NET APIs for Azure services. The emphasis is all around continuous integration, deployment, monitoring, and debugging. Near the end of the guide, recommendations for next steps are offered. Included in the suggestions are Azure platform services that are useful to ASP.NET developers. What’s in this guide Tools and downloads Learn where to acquire the tools used in this guide. Deploy to App Service Learn the various methods for deploying an ASP.NET Core app to Azure App Service. Continuous integration and deployment Build an end-to-end continuous integration and deployment solution for your ASP.NET Core app with GitHub, VSTS, and Azure. Monitor and debug Use Azure’s tools to monitor, troubleshoot, and tune your application.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值