{"id":6385,"date":"2017-01-26T17:13:37","date_gmt":"2017-01-27T01:13:37","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/cesardelatorre\/?p=6385"},"modified":"2017-01-26T17:13:37","modified_gmt":"2017-01-27T01:13:37","slug":"comparing-asp-net-core-ioc-service-life-times-and-autofac-ioc-instance-scopes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/comparing-asp-net-core-ioc-service-life-times-and-autofac-ioc-instance-scopes\/","title":{"rendered":"Comparing ASP.NET Core IoC container service lifetimes with Autofac IoC container instance scopes"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/DI_NET.png\"><img decoding=\"async\" title=\"DI_NET\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;float: none;padding-top: 0px;padding-left: 0px;margin-left: auto;padding-right: 0px;border-top-width: 0px;margin-right: auto\" border=\"0\" alt=\"DI_NET\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/32\/2019\/03\/DI_NET_thumb.png\" width=\"244\" height=\"91\"><\/a><\/p>\n<p>In ASP.NET Core you can use the simple built-in IoC container or you can also plug any other more advanced IoC container like Autofac.<\/p>\n<p>When plugin an external container like Autofac, you can still use ASP.NET Core methods like <strong>services.AddTransient&lt;&gt;<\/strong> or <strong>services.AddSingleton&lt;&gt;<\/strong> or you can also use the more granular methods in Autofac based on its instance scopes, which offer a larger set of possibilities.<\/p>\n<p>Anyways, because I\u2019m working \u201cfrom both sides\u201d, I wanted to have clear a comparison of each type and how are they exactly called from each API..<\/p>\n<p>So here it is a convenient table that I just created:<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"812\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p align=\"center\"><b><font size=\"1\">ASP.NET Core<\/font><\/b><\/p>\n<p align=\"center\"><b><font size=\"1\">Built-in IoC container<\/font><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p align=\"center\"><b><font size=\"1\">Autofac <\/font><\/b><\/p>\n<p align=\"center\"><b><font size=\"1\">IoC container<\/font><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p align=\"center\"><b><font size=\"1\">Description<\/font><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b>ServiceLifetime<\/b><b>.<font style=\"background-color: #ffff00\">Transient<\/font><\/b><\/p>\n<p><b><\/b><\/p>\n<p><b>services.AddTransient&lt;,&gt;<\/b><b><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b><font style=\"background-color: #ffff00\">InstancePerDependency<\/font>()<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>A unique instance will be returned from each object request.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b>ServiceLifetime<\/b><b>.<font style=\"background-color: #ffff00\">Scoped<\/font><\/b><\/p>\n<p><b><\/b><\/p>\n<p><b>services.AddScope&lt;,&gt;<\/b><\/p>\n<p>In ASP.NET Core applications a scope is created around each server request.<\/p>\n<p>But it could be used as plain Scoped (non related to Http requests) if using DI out of ASP.NET Core<\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b><font style=\"background-color: #ffff00\">InstancePerLifetimeScope()<\/font><\/b><\/p>\n<p><b><\/b>&nbsp;<\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>A component with per-lifetime scope will have at most a single instance per nested lifetime scope.<\/p>\n<p>This is useful for objects specific to a single unit of work that may need to nest additional logical units of work. Each nested lifetime scope will get a new instance of the registered dependency.<\/p>\n<p>For example, this type of lifetime scope is useful for Entity Framework DbContext objects (Unit of Work pattern) to be shared across the object scope so you can run transactions across multiple objects.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b>ServiceLifetime<\/b><b>.<font style=\"background-color: #ffff00\">Scoped<\/font><\/b><\/p>\n<p><b><\/b><\/p>\n<p><b>services.AddScope&lt;,&gt;<\/b><\/p>\n<p>In ASP.NET Core applications a scope is created around each server request, so it will work similar to InstancePerRequest, in this case.<\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b><font style=\"background-color: #ffff00\">InstancePerRequest<\/font>()<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>Application types like ASP.NET Core naturally lend themselves to \u201crequest\u201d type semantics. You have the ability to have a sort of \u201csingleton per request.\u201d<\/p>\n<p>Instance per request builds on top of instance per matching lifetime scope by providing a well-known lifetime scope tag, a registration convenience method, and integration for common application types. Behind the scenes, though, it\u2019s still just instance per matching lifetime scope.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b>ServiceLifetime<\/b><b>.<font style=\"background-color: #ffff00\">Singleton<\/font><\/b><\/p>\n<p><b><\/b><\/p>\n<p><b>services.AddSingleton&lt;,&gt;<\/b><b><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b><font style=\"background-color: #ffff00\">SingleInstance<\/font>()<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>One instance is returned from all requests in the root and all nested scopes<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b><font color=\"#ff0000\">NO<\/font><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b>InstancePerMatchingLifetimeScope()<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>you have the ability to \u201ctag\u201d or \u201cname\u201d the scope. A component with per-matching-lifetime scope will have at most a single instance per nested lifetime scope that matches a given name. This allows you to create a sort of \u201cscoped singleton\u201d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b><font color=\"#ff0000\">NO<\/font><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b>InstancePerOwned()<\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>The Owned&lt;T&gt; implicit relationship type creates new nested lifetime scopes. It is possible to scope dependencies to the owned instance using the instance-per-owned registrations.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"185\">\n<p><b><font color=\"#ff0000\">NO<\/font><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"254\">\n<p><b>Thread Scope<\/b><\/p>\n<p>(based on lifetime scopes)<\/p>\n<\/td>\n<td valign=\"top\" width=\"371\">\n<p>Autofac can enforce that objects bound to one thread will not satisfy the dependencies of a component bound to another thread. While there is not a convenience method for this, you can do it using lifetime scopes.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Note that ASP.NET Core only uses the three more common types of instance life time:<\/p>\n<p>&#8211; Transient, PerRequest\/Scoped and Singleton.<\/p>\n<p><strong>References<\/strong>:<\/p>\n<p>Service Lifetime in .NET Core: <a title=\"https:\/\/github.com\/aspnet\/DependencyInjection\/blob\/dev\/src\/Microsoft.Extensions.DependencyInjection.Abstractions\/ServiceLifetime.cs\" href=\"https:\/\/github.com\/aspnet\/DependencyInjection\/blob\/dev\/src\/Microsoft.Extensions.DependencyInjection.Abstractions\/ServiceLifetime.cs\">https:\/\/github.com\/aspnet\/DependencyInjection\/blob\/dev\/src\/Microsoft.Extensions.DependencyInjection.Abstractions\/ServiceLifetime.cs<\/a><\/p>\n<p>Instance Scopes in Autofac: <a href=\"http:\/\/docs.autofac.org\/en\/latest\/lifetime\/instance-scope.html\">http:\/\/docs.autofac.org\/en\/latest\/lifetime\/instance-scope.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ASP.NET Core you can use the simple built-in IoC container or you can also plug any other more advanced IoC container like Autofac. When plugin an external container like Autofac, you can still use ASP.NET Core methods like services.AddTransient&lt;&gt; or services.AddSingleton&lt;&gt; or you can also use the more granular methods in Autofac based on [&hellip;]<\/p>\n","protected":false},"author":362,"featured_media":12806,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[21,22,42,55],"class_list":["post-6385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cesardelatorre","tag-asp-net-core","tag-autofac","tag-di","tag-ioc"],"acf":[],"blog_post_summary":"<p>In ASP.NET Core you can use the simple built-in IoC container or you can also plug any other more advanced IoC container like Autofac. When plugin an external container like Autofac, you can still use ASP.NET Core methods like services.AddTransient&lt;&gt; or services.AddSingleton&lt;&gt; or you can also use the more granular methods in Autofac based on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/6385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/comments?post=6385"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/posts\/6385\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media\/12806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/media?parent=6385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/categories?post=6385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cesardelatorre\/wp-json\/wp\/v2\/tags?post=6385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}