{"id":8388,"date":"2020-07-11T16:13:45","date_gmt":"2020-07-11T16:13:45","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8388"},"modified":"2020-07-11T16:13:45","modified_gmt":"2020-07-11T16:13:45","slug":"dependency-injection-and-blazor","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/dependency-injection-and-blazor\/","title":{"rendered":"Dependency Injection and Blazor"},"content":{"rendered":"<p>In a previous post <a href=\"http:\/\/putridparrot.com\/blog\/blazor-routing-and-navigation\/\" rel=\"noopener noreferrer\" target=\"_blank\">Blazor routing and Navigation<\/a> we injected the NavigationManager into out page using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject NavigationManager NavManager\r\n<\/pre>\n<p>So when we use @inject followed by the type we want injected, ASP.NET\/Blazor will automatically supply the NavigationManager (assuming one exists).<\/p>\n<p><strong>Adding services<\/strong><\/p>\n<p>Ofcourse we can also add our own types\/services to the DI container. <\/p>\n<p>On a Blazor WebAssembly application, we can add types to the Program.cs, Main method, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static async Task Main(string&#x5B;] args)\r\n{\r\n   var builder = WebAssemblyHostBuilder.CreateDefault(args);\r\n   \/\/ template generated code here\r\n\r\n   \/\/ my custom DataService\r\n   builder.Services.AddSingleton&lt;IDataService, DataService&gt;();\r\n\r\n   await builder.Build().RunAsync();\r\n}\r\n<\/pre>\n<p>In Blazor Server, we add our types to the Startup.cs, ConfigureServices method, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic void ConfigureServices(IServiceCollection services)\r\n{\r\n   \/\/ template generated code here\r\n\r\n   \/\/ my custom DataService\r\n   services.AddSingleton&lt;IDataService, DataService&gt;();\r\n}\r\n<\/pre>\n<p><strong>Service lifetime<\/strong><\/p>\n<p>In the examples in the previous section we added the service as a singleton.<\/p>\n<ul>\n<li>Scoped &#8211; this is means the service is scoped to the connection. This is the preferred way to handle per user services &#8211; there is no concept of scope services in WebAssembly as obviously it&#8217;s a client technology at this point and already per user scoped<\/ul>\n<li>Singleton &#8211; As you&#8217;d expect, this is a single instance for the lifetime of the application<\/li>\n<li>Transient &#8211; each request for a transient service will receive a new instance of the service<\/li>\n<\/ul>\n<p>If you need access to service is a Component class, i.e. you&#8217;re creating your own IComponent you have mark a property with the InjectAttribute<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyService\r\n{\r\n   &#x5B;Inject]\r\n   IDataService DataService { get; set; }\r\n}\r\n<\/pre>\n<p>Ofcourse constructor injection (my preferred way to do things) is also available, so we just write code such as this, assuming that MyService is created using the service container<\/p>\n<p>public class MyService<br \/>\n{<br \/>\n   public MyService(IDataService dataService)<br \/>\n   {<br \/>\n      \/\/ do something with dataService<br \/>\n   }<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post Blazor routing and Navigation we injected the NavigationManager into out page using the following @inject NavigationManager NavManager So when we use @inject followed by the type we want injected, ASP.NET\/Blazor will automatically supply the NavigationManager (assuming one exists). Adding services Ofcourse we can also add our own types\/services to the DI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305],"tags":[],"class_list":["post-8388","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8388"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8388\/revisions"}],"predecessor-version":[{"id":8422,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8388\/revisions\/8422"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}