人如果没有梦想那和咸鱼有什么区别,即使是做咸鱼也要做最咸的那一条

—— Wy

ASP.NET Core 依赖注入的生命周期

ASP.NET Core 依赖注入有几种生命周期:


1、 Transient暂时生存期)--暂时生存期服务是每次从服务容器进行请求时创建的。 这种生存期适合轻量级、 无状态的服务。

暂时生存期会在每次请求的时候创建一个实例

2、 Scoped范围生存期)--范围生存期服务是每个客户端连接时创建的一次实例

范围生存期会在客户端连接时创建一次实例,然后每次请求的实例都是相同的

3、 Singleton单例生存期)--单例生存期会在程序第一次请求是创建一次实例

单例生存期仅会在第一次连接时创建一次实例,所有整个程序使用的实例都是同一个实例


在.NET Core中DI的核心分为两个组件:IServiceCollection和 IServiceProvider 


  •   IServiceCollection---负责注册
  •   IServiceProvider---负责提供实例

以下是测试代码


控制台项目需要添加Nuget包:Microsoft.Extensions.DependencyInjection

static void Main(string[] args)
        {
            IServiceCollection services = new ServiceCollection();
            services.AddTransient<IOperationTransient, Operation>();
            services.AddScoped<IOperationScoped, Operation>();
            services.AddSingleton<IOperationSingleton, Operation>();
            services.AddTransient<IOperationSingletonInstance, Operation>(p =>
            {
                if (DateTime.Now.Millisecond > 500)
                {
                    return new Operation(Guid.Empty);
                }
                else
                    return new Operation(Guid.NewGuid());
            });
            // OperationService depends on each of the other Operation types.
            services.AddTransient<OperationService>();
            services.AddTransient<IndexModel>();
            //services.AddSingleton<IMemcachedClient, MemcachedClientCC>();
            //构建容器
            IServiceProvider serviceProvider = services.BuildServiceProvider();
            serviceProvider = services.BuildServiceProvider();
            //解析
            object objLock = new object();
            for (int i = 0; i < 2; i++)
            {
                Task.Run(() =>
                {
                    lock (objLock)
                    {
                        foreach (var memcachedClient in serviceProvider.GetServices<IndexModel>())
                        {
                            //Console.WriteLine(memcachedClient.OperationId);
                            memcachedClient.OnGet();
                            Console.WriteLine("********************************");
                        }
                        Console.WriteLine("");
                        Console.WriteLine("");
                        Console.WriteLine("");
                        Thread.Sleep(500);
                    }
                });
            }
            Console.Read();
        }  

public interface IOperation
    {
        Guid OperationId { get; set; }
    }
    public interface IOperationTransient : IOperation
    {
    }
    public interface IOperationScoped : IOperation
    {
    }
    public interface IOperationSingleton : IOperation
    {
    }
    public interface IOperationSingletonInstance : IOperation
    {
    }

    public class Operation : IOperationTransient, IOperationScoped, IOperationSingleton, IOperationSingletonInstance
    {
        public Operation() : this(Guid.NewGuid())
        {
        }
        public Operation(Guid id)
        {
            OperationId = id;
        }
        public Guid OperationId { get; set; }
    }

    public class OperationService
    {
        public OperationService(
            IOperationTransient transientOperation,
            IOperationScoped scopedOperation,
            IOperationSingleton singletonOperation,
            IOperationSingletonInstance instanceOperation)
        {
            _transientOperation = transientOperation;
            _scopedOperation = scopedOperation;
            _singletonOperation = singletonOperation;
            _singletonInstanceOperation = instanceOperation;
        }
        public IOperationTransient _transientOperation { get; }
        public IOperationScoped _scopedOperation { get; }
        public IOperationSingleton _singletonOperation { get; }
        public IOperationSingletonInstance _singletonInstanceOperation { get; }
    }

    public class IndexModel
    {
        public OperationService _operationService { get; }
        public IOperationTransient _transientOperation { get; }
        public IOperationScoped _scopedOperation { get; }
        public IOperationSingleton _singletonOperation { get; }
        public IOperationSingletonInstance _singletonInstanceOperation { get; }
        public IndexModel(
        OperationService operationService,
        IOperationTransient transientOperation,
        IOperationScoped scopedOperation,
        IOperationSingleton singletonOperation,
        IOperationSingletonInstance singletonInstanceOperation)
        {
            _operationService = operationService;
            _transientOperation = transientOperation;
            _scopedOperation = scopedOperation;
            _singletonOperation = singletonOperation;
            _singletonInstanceOperation = singletonInstanceOperation;
        }
        public void OnGet()
        {
            Console.WriteLine("IOperation操作:");
            Console.WriteLine("暂时:" + _transientOperation.OperationId.ToString());
            Console.WriteLine("作用域:" + _scopedOperation.OperationId.ToString());
            //_scopedOperation.OperationId = Guid.NewGuid();
            Console.WriteLine("单例:" + _singletonOperation.OperationId.ToString());
            Console.WriteLine("实例:" + _singletonInstanceOperation.OperationId.ToString());

            Guid guid = Guid.NewGuid();
            _scopedOperation.OperationId = guid;
            Console.WriteLine($"赋予作用域新的Guid:{guid.ToString()}");

            Console.WriteLine("OperationService操作:");
            Console.WriteLine("暂时:" + _operationService._transientOperation.OperationId.ToString());
            Console.WriteLine("作用域:" + _operationService._scopedOperation.OperationId.ToString());
            Console.WriteLine("单例:" + _operationService._singletonOperation.OperationId.ToString());
            Console.WriteLine("实例:" + _operationService._singletonInstanceOperation.OperationId.ToString());
        }
    }






评论

用户默认头像
发表我的评论
取消评论
看不清,点击刷新验证码