{"id":1738,"date":"2023-06-14T08:28:46","date_gmt":"2023-06-14T08:28:46","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1738"},"modified":"2023-06-14T08:28:46","modified_gmt":"2023-06-14T08:28:46","slug":"asp-net-built-in-ioc-container","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/14\/asp-net-built-in-ioc-container\/","title":{"rendered":"Asp.Net Built In IoC Container"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1738\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1738\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<h1 class=\"wp-block-heading\">ASP.NET Core: Built-in IoC Container<\/h1>\n\n\n\n<p>ASP.NET Core framework includes built-in IoC container for automatic dependency injection. The built-in IoC container is a simple yet effective container. Let&#8217;s understand how the built-in IoC container works internally.<\/p>\n\n\n\n<p>The followings are important interfaces and classes for built-in IoC container:<\/p>\n\n\n\n<p><strong>Interfaces:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>IServiceProvider<\/li>\n\n\n\n<li>IServiceCollection<\/li>\n<\/ol>\n\n\n\n<p><strong>Classes:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>ServiceProvider<\/li>\n\n\n\n<li>ServiceCollection<\/li>\n\n\n\n<li>ServiceDescription<\/li>\n\n\n\n<li>ServiceCollectionServiceExtensions<\/li>\n\n\n\n<li>ServiceCollectionContainerBuilderExtensions<\/li>\n<\/ol>\n\n\n\n<p>The following diagram illustrates the relationship between these classes:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"289\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture31.png\" alt=\"\" class=\"wp-image-1739\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture31.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture31-300x144.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/builtin-ioc.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>built-in IoC Container<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IServiceCollection<\/h2>\n\n\n\n<p>As you know, we can register application services with built-in IoC container in the Configure method of Startup class by using IServiceCollection. IServiceCollection interface is an empty interface. It just inherits&nbsp;<code>IList&lt;servicedescriptor&gt;<\/code>. See the source code&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/api\/microsoft.extensions.dependencyinjection.iservicecollection\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>The ServiceCollection class implements IServiceCollection interface. See the ServiceCollection source code&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/api\/microsoft.extensions.dependencyinjection.servicecollection\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>So, the services you add in the&nbsp;<code>IServiceCollection<\/code>&nbsp;type instance, it actually creates an instance of&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/api\/microsoft.extensions.dependencyinjection.servicedescriptor\" target=\"_blank\" rel=\"noreferrer noopener\">ServiceDescriptor<\/a>&nbsp;and adds it to the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IServiceProvider<\/h2>\n\n\n\n<p><code>IServiceProvider<\/code>&nbsp;includes&nbsp;<code>GetService<\/code>&nbsp;method. The&nbsp;<code>ServiceProvider<\/code>&nbsp;class implements&nbsp;<code>IServiceProvider<\/code>&nbsp;interface which returns registered services with the container. We cannot instantiate&nbsp;<code>ServiceProvider<\/code>&nbsp;class because its constructors are marked with internal access modifier.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ServiceCollectionServiceExtensions<\/h2>\n\n\n\n<p>The&nbsp;<code>ServiceCollectionServiceExtensions<\/code>&nbsp;class includes extension methods related to service registrations which can be used to add services with lifetime. AddSingleton, AddTransient, AddScoped extension methods defined in this class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ServiceCollectionContainerBuilderExtensions<\/h2>\n\n\n\n<p><code>ServiceCollectionContainerBuilderExtensions<\/code>&nbsp;class includes&nbsp;<code>BuildServiceProvider<\/code>&nbsp;extension method which creates and returns an instance of&nbsp;<code>ServiceProvider<\/code>.<\/p>\n\n\n\n<p>There are three ways to get an instance of&nbsp;<code>IServiceProvider<\/code>:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using IApplicationBuilder<\/strong><\/h3>\n\n\n\n<p>We can get the services in Configure method using IApplicationBuilder&#8217;s&nbsp;<code>ApplicationServices<\/code>&nbsp;property as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> Configure(IServiceProvider pro, IApplicationBuilder app, IHostingEnvironment env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>var<code> services = app.ApplicationServices;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>var<code> logger = services.GetService&lt;ILog&gt;() }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>\/\/other code removed for clarity<code> <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using HttpContext<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">var<code> services = HttpContext.RequestServices;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">var<code> log = (ILog)services.GetService(<\/code>typeof<code>(ILog));<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using IServiceCollection<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> ConfigureServices(IServiceCollection services)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>var<code> serviceProvider = services.BuildServiceProvider();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core: Built-in IoC Container ASP.NET Core framework includes built-in IoC container for automatic dependency injection. The built-in IoC container is a simple yet effective container. Let&#8217;s understand how the built-in IoC container works internally. The followings are important interfaces and classes for built-in IoC container: Interfaces: Classes: The following diagram illustrates the relationship between [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1738","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":28,"uagb_excerpt":"ASP.NET Core: Built-in IoC Container ASP.NET Core framework includes built-in IoC container for automatic dependency injection. The built-in IoC container is a simple yet effective container. Let&#8217;s understand how the built-in IoC container works internally. The followings are important interfaces and classes for built-in IoC container: Interfaces: Classes: The following diagram illustrates the relationship between&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1738"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1738\/revisions"}],"predecessor-version":[{"id":1740,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1738\/revisions\/1740"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}