{"id":10787,"date":"2021-07-19T15:01:50","date_gmt":"2021-07-19T14:01:50","guid":{"rendered":"https:\/\/fortude1.wpenginepowered.com\/blog\/asp-net-core-dependency-injection-and-service-lifetimes\/"},"modified":"2026-05-07T09:22:21","modified_gmt":"2026-05-07T08:22:21","slug":"aspnet-core-dependency-injection-and-service-lifetimes","status":"publish","type":"blog","link":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/","title":{"rendered":"ASP.NET Core Dependency Injection and Service Lifetimes"},"content":{"rendered":"<p>ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components. Also, ASP.NET Core has a built-in IoC container, eliminating the need to use a third-party container to inject the service. Some services will be instantiated for a short time and will be available only in a particular component and request. Some will be instantiated just once and will be available throughout the application.<\/p>\n<p>To inject application services automatically, first register them with the IoC container.<\/p>\n<p>There are two key components in the built-in container:<\/p>\n<ol>\n<li>IServiceCollection &#8211; All the services will register here.<\/li>\n<li>IServiceProvider &#8211; Implementation supports the constructor injection.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/blog-image-04-2048x1072-1.png\" alt=\"\" \/><\/p>\n<p>Figure 01: Key components in the built-in IoC container<\/p>\n<h2><b>Service lifetimes<br \/>\n<\/b><\/h2>\n<p>The built-in IoC container manages the lifetime of the registered service type. It automatically disposes of the service instance according to the specified lifetime.<\/p>\n<p>ASP.NET Core services support three types of lifetimes:<\/p>\n<h4><b>Singleton<\/b><br \/>\n<b><\/b><\/h4>\n<p>A single instance of the registered service class is created and stored in the memory. It is re-used across the application when multiple requests come into the application. Thus, we can use the singleton lifetime for services that are expensive to instantiate.<\/p>\n<p>When using the singleton type in real use cases, you need to consider the following:<\/p>\n<ol>\n<li>The service should not be dependent on other service types.<\/li>\n<li>A singleton instance lasts the entire lifetime of the application until it is terminated. Therefore, the instance should be disposed properly. If not, it can cause a performance issues in the application.<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/blog-image-05-2048x1072-1.png\" alt=\"\" \/><\/p>\n<p>Figure 02: Singleton instances are created the first time they are requested<\/p>\n<p>How to register the singleton service .NET Core during startup.<\/p>\n<p>public void ConfigureServices(IServiceCollection services)<\/p>\n<p>{<br \/>\nservices.AddSingleton();<\/p>\n<p>}<\/p>\n<h4><b>Scoped<\/b><\/h4>\n<p>&nbsp;<\/p>\n<p>Think of a scoped lifetime as a service instance that is created per web request. The request will be considered as scope. This lifetime works best for lightweight, stateless services.<\/p>\n<p>A common usage of this approach is to declare the DbContext class of Entity Framework Core. The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/blog-image-06-2048x1072-1.png\" alt=\"\" \/><\/p>\n<p>Figure 03: A scoped lifetime denotes that services are created once per client web request.<\/p>\n<p>How to register the scoped service .NET Core during startup.<\/p>\n<p>public void ConfigureServices(IServiceCollection services)<br \/>\n{<br \/>\nservices.AddScoped();<br \/>\n}<\/p>\n<h4><b>Transient<br \/>\n<\/b><\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/blog-image-07-2048x1072-1.png\" alt=\"\" \/><\/p>\n<p>Figure 04: Transient lifetime services are created each time they are requested from the service container<\/p>\n<p>Each time a service is requested, a new instance will be created. This is the most common and always the safest option if you are worried about multithreading.<\/p>\n<p>If we inject the same service into a Controller and a View in an MVC application, two different instances will be created in the controller and the view.<\/p>\n<p>How to register the transient service .NET Core during startup.<\/p>\n<p>public void ConfigureServices(IServiceCollection services)<\/p>\n<p>{ services.AddTransient();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>&#8220;In terms of the lifetime of the instance, the Singleton object gets the highest life per instantiation, followed by a Scoped service object and the least by a Transient object.&#8221;<\/strong><\/p>\n<p>Written by<\/p>\n","protected":false},"featured_media":14296,"template":"","meta":{"_acf_changed":false,"content-type":""},"industry":[],"service":[57],"class_list":["post-10787","blog","type-blog","status-publish","has-post-thumbnail","hentry"],"acf":{"blog_render_type":"legacy_acf","sections":[{"section_title":"Introduction","section_content":"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components. Also, ASP.NET Core has a built-in IoC container, eliminating the need to use a third-party container to inject the service. Some services will be instantiated for a short time and will be available only in a particular component and request. Some will be instantiated just once and will be available throughout the application.\r\n\r\nTo inject application services automatically, first register them with the IoC container.\r\n\r\nThere are two key components in the built-in container:\r\n<ol>\r\n \t<li>IServiceCollection - All the services will register here.<\/li>\r\n \t<li>IServiceProvider - Implementation supports the constructor injection.<\/li>\r\n<\/ol>\r\nFigure 01: Key components in the built-in IoC container","section_image":10790,"table_rows":null,"pro-tip":""},{"section_title":"Service lifetimes","section_content":"The built-in IoC container manages the lifetime of the registered service type. It automatically disposes of the service instance according to the specified lifetime.\r\n\r\nASP.NET Core services support three types of lifetimes:","section_image":"","table_rows":null,"pro-tip":""},{"section_title":"Singleton","section_content":"A single instance of the registered service class is created and stored in the memory. It is re-used across the application when multiple requests come into the application. Thus, we can use the singleton lifetime for services that are expensive to instantiate.\r\n\r\nWhen using the singleton type in real use cases, you need to consider the following:\r\n<ol>\r\n \t<li>The service should not be dependent on other service types.<\/li>\r\n \t<li>A singleton instance lasts the entire lifetime of the application until it is terminated. Therefore, the instance should be disposed properly. If not, it can cause a performance issues in the application.<\/li>\r\n<\/ol>\r\nFigure 02: Singleton instances are created the first time they are requested\r\n\r\nHow to register the singleton service .NET Core during startup.\r\n\r\npublic void ConfigureServices(IServiceCollection services)\r\n\r\n{\r\nservices.AddSingleton();\r\n\r\n}","section_image":10791,"table_rows":null,"pro-tip":""},{"section_title":"Scoped","section_content":"Think of a scoped lifetime as a service instance that is created per web request. The request will be considered as scope. This lifetime works best for lightweight, stateless services.\r\n\r\nA common usage of this approach is to declare the DbContext class of Entity Framework Core. The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed.\r\n\r\nFigure 03: A scoped lifetime denotes that services are created once per client web request.\r\n\r\nHow to register the scoped service .NET Core during startup.\r\n\r\npublic void ConfigureServices(IServiceCollection services)\r\n{\r\nservices.AddScoped();\r\n}","section_image":10792,"table_rows":null,"pro-tip":""}],"bottom_sections":[{"section_title":"Transient","section_content":"Figure 04: Transient lifetime services are created each time they are requested from the service container\r\n\r\nEach time a service is requested, a new instance will be created. This is the most common and always the safest option if you are worried about multithreading.\r\n\r\nIf we inject the same service into a Controller and a View in an MVC application, two different instances will be created in the controller and the view.\r\n\r\nHow to register the transient service .NET Core during startup.\r\n\r\npublic void ConfigureServices(IServiceCollection services)\r\n\r\n{ services.AddTransient();\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>\"In terms of the lifetime of the instance, the Singleton object gets the highest life per instantiation, followed by a Scoped service object and the least by a Transient object.\"<\/strong>\r\n\r\nWritten by","section_image":10793,"table_rows":null,"faq":null,"pro-tip":""}]},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.7 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>ASP.NET Core Dependency Injection and Service Lifetimes | Fortude<\/title>\n<meta name=\"description\" content=\"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ASP.NET Core Dependency Injection and Service Lifetimes\" \/>\n<meta property=\"og:description\" content=\"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/\" \/>\n<meta property=\"og:site_name\" content=\"Fortude\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-07T08:22:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"ASP.NET Core Dependency Injection and Service Lifetimes\" \/>\n<meta name=\"twitter:description\" content=\"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/\",\"url\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/\",\"name\":\"ASP.NET Core Dependency Injection and Service Lifetimes | Fortude\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fortude.co\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/19th-of-July-2021-2-scaled.png\",\"datePublished\":\"2021-07-19T14:01:50+00:00\",\"dateModified\":\"2026-05-07T08:22:21+00:00\",\"description\":\"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fortude.co\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/19th-of-July-2021-2-scaled.png\",\"contentUrl\":\"https:\\\/\\\/fortude.co\\\/wp-content\\\/uploads\\\/2021\\\/07\\\/19th-of-July-2021-2-scaled.png\",\"width\":2560,\"height\":1006},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fortude.co\\\/blog\\\/aspnet-core-dependency-injection-and-service-lifetimes\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fortude.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ASP.NET Core Dependency Injection and Service Lifetimes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fortude.co\\\/#website\",\"url\":\"https:\\\/\\\/fortude.co\\\/\",\"name\":\"Fortude\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fortude.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fortude.co\\\/#organization\",\"name\":\"Fortude\",\"url\":\"https:\\\/\\\/fortude.co\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/fortude.co\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fortude.co\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/Fortude-Logo.svg\",\"contentUrl\":\"https:\\\/\\\/fortude.co\\\/wp-content\\\/uploads\\\/2026\\\/02\\\/Fortude-Logo.svg\",\"width\":100,\"height\":15,\"caption\":\"Fortude\"},\"image\":{\"@id\":\"https:\\\/\\\/fortude.co\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"ASP.NET Core Dependency Injection and Service Lifetimes | Fortude","description":"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/","og_locale":"en_GB","og_type":"article","og_title":"ASP.NET Core Dependency Injection and Service Lifetimes","og_description":"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.","og_url":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/","og_site_name":"Fortude","article_modified_time":"2026-05-07T08:22:21+00:00","twitter_card":"summary_large_image","twitter_title":"ASP.NET Core Dependency Injection and Service Lifetimes","twitter_description":"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.","twitter_misc":{"Estimated reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/","url":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/","name":"ASP.NET Core Dependency Injection and Service Lifetimes | Fortude","isPartOf":{"@id":"https:\/\/fortude.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/#primaryimage"},"image":{"@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/#primaryimage"},"thumbnailUrl":"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/19th-of-July-2021-2-scaled.png","datePublished":"2021-07-19T14:01:50+00:00","dateModified":"2026-05-07T08:22:21+00:00","description":"ASP.NET Core supports the Dependency Injection (DI) software design pattern that allows registering services and controlling how these services are instantiated and injected in different components.","breadcrumb":{"@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/#primaryimage","url":"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/19th-of-July-2021-2-scaled.png","contentUrl":"https:\/\/fortude.co\/wp-content\/uploads\/2021\/07\/19th-of-July-2021-2-scaled.png","width":2560,"height":1006},{"@type":"BreadcrumbList","@id":"https:\/\/fortude.co\/blog\/aspnet-core-dependency-injection-and-service-lifetimes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fortude.co\/"},{"@type":"ListItem","position":2,"name":"ASP.NET Core Dependency Injection and Service Lifetimes"}]},{"@type":"WebSite","@id":"https:\/\/fortude.co\/#website","url":"https:\/\/fortude.co\/","name":"Fortude","description":"","publisher":{"@id":"https:\/\/fortude.co\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fortude.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/fortude.co\/#organization","name":"Fortude","url":"https:\/\/fortude.co\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/fortude.co\/#\/schema\/logo\/image\/","url":"https:\/\/fortude.co\/wp-content\/uploads\/2026\/02\/Fortude-Logo.svg","contentUrl":"https:\/\/fortude.co\/wp-content\/uploads\/2026\/02\/Fortude-Logo.svg","width":100,"height":15,"caption":"Fortude"},"image":{"@id":"https:\/\/fortude.co\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/blog\/10787","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/types\/blog"}],"version-history":[{"count":0,"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/blog\/10787\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/media\/14296"}],"wp:attachment":[{"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/media?parent=10787"}],"wp:term":[{"taxonomy":"industry","embeddable":true,"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/industry?post=10787"},{"taxonomy":"service","embeddable":true,"href":"https:\/\/fortude.co\/wp-json\/wp\/v2\/service?post=10787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}