为什么我们需要在ASP.NET核心的startup.cs文件中添加addtransient方法中的所有服务? [英] Why do we need to add all the services in addtransient method in startup.cs file in ASP.NET core?

查看:675
本文介绍了为什么我们需要在ASP.NET核心的startup.cs文件中添加addtransient方法中的所有服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在addTransient中添加程序中存在的所有服务将为这些服务创建实例。此实例创建为所有服务分配内存。如果有200个服务,那么在启动时为所有这些服务分配内存是否合适?



公共静态类ServiceExtensions 
{
public static IServiceCollection RegisterServices(
this IServiceCollection services)
{
services.AddTransient< ITopicAreaService,TopicAreaService>();
//添加说200个服务在这里将为启动时的所有服务分配内存
//这样做是好的
返回服务;
}
}





我的尝试:



公共静态类ServiceExtensions 
{
public static IServiceCollection RegisterServices(
this IServiceCollection services)
{
services.AddTransient< ITopicAreaService,TopicAreaService>();
//添加说200个服务在这里将为启动时的所有服务分配内存
//这样做是好的
返回服务;
}
}

解决方案

此时实际上并没有创建服务。每个从DI容器中解析的瞬态服务都会创建。所有分配的对象都描述了服务的构造及其生命周期。

用于创建实例的技术,如果非常快速和高效,导致最快的DI容器之一

adding all services present in the program in addTransient will create instance for these services . This instance creation allocate memory for all service. If there are 200 services then Is this good to allocate memory for all these services at startup ?

public static class ServiceExtensions
    {
        public static IServiceCollection RegisterServices(
            this IServiceCollection services)
        {
            services.AddTransient<ITopicAreaService, TopicAreaService>();
            // Add say 200 services here will allocate memory for all  services at startup 
            //is this good to do so
            return services;
        }
    }



What I have tried:

public static class ServiceExtensions
    {
        public static IServiceCollection RegisterServices(
            this IServiceCollection services)
        {
            services.AddTransient<ITopicAreaService, TopicAreaService>();
            // Add say 200 services here will allocate memory for all  services at startup 
            //is this good to do so
            return services;
        }
    }

解决方案

It is not actually creating the services at this point. Transient services are created as each it resolved from the DI container. All that is allocated is an object that describes the construction of the service and its lifetime.
The techniques that are used to create the instances if very fast and efficient resulting in one of the fastest DI Containers out there.


这篇关于为什么我们需要在ASP.NET核心的startup.cs文件中添加addtransient方法中的所有服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆