Exemple #1
0
        private static void TryOutTransient()
        {
            Console.WriteLine($"RUNNING {nameof(TryOutTransient)}");

            ServiceCollection services = new ServiceCollection();                              // 准备好我们的容器

            services.AddTransient <MyClass>();                                                 //把MyClass注册为 Transient 生命周期

            ConcurrentBag <MyClass> bag = GetObjectsFromDI(services);                          // 调用我们准备好的方法,用若干线程从 IServiceProvider 中获取 MyClass 实例,并加入到集合

            Console.WriteLine($"All items in collection are DIFFERENT: {bag.AreDifferent()}"); // 验证集合中的所有元素是否各不相同
        }