{"id":650,"date":"2019-09-25T17:12:54","date_gmt":"2019-09-25T08:12:54","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=650"},"modified":"2019-09-25T17:12:54","modified_gmt":"2019-09-25T08:12:54","slug":"middleware-8","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=650","title":{"rendered":"middleware-8"},"content":{"rendered":"\n<pre class=\"lang:c# decode:true \" >using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder\r\nusing Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder\r\nusing Microsoft.AspNetCore.Http; \/\/WriteAsync\r\nusing System.Threading.Tasks;\r\nusing Microsoft.Extensions.DependencyInjection;\r\nusing Microsoft.AspNetCore.Builder.Extensions;\r\nusing Microsoft.AspNetCore;\r\n\r\nnamespace WebApplication1\r\n{\r\n    public class Content\r\n    {\r\n        public string SayHello() =&gt; \"Hello world\";\r\n    }\r\n\r\n    public class Greeting\r\n    {\r\n        public string Greet() =&gt; \"Good morning\";\r\n    }\r\n\r\n    public class Startup\r\n    {\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n            services.AddSingleton&lt;Content&gt;();\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app)\r\n        {\r\n            \/\/Pay attention to the order of the parameter passed.\r\n            \/\/If your parameter is distinct, the order does not matter, As you can see here we put Greeting\r\n            \/\/at the end of the parameter passing although in the constructor Greeting was the second on the parameter list.\r\n            \/\/However if you pass multiple parameters of the same type, the order matters. \r\n            app.UseMiddleware&lt;TerminalMiddleware&gt;(\"Cairo\", \"Dody\", new Greeting());\r\n        }\r\n    }\r\n\r\n    public class TerminalMiddleware\r\n    {\r\n        readonly Content _content;\r\n        readonly string _city;\r\n        readonly Greeting _greet;\r\n        readonly string _name;\r\n\r\n        public TerminalMiddleware(RequestDelegate next, Greeting greet, string city, Content cnt, string name)\r\n        {\r\n            \/\/We are not using the parameter next in this middleware since this middleware is terminal\r\n            _content = cnt;\r\n            _city = city;\r\n            _greet = greet;\r\n            _name = name;\r\n        }\r\n        public async Task Invoke(HttpContext context)\r\n        {\r\n            await context.Response.WriteAsync($\"{_greet.Greet()} {_name}, {_content.SayHello()} from {_city}\");\r\n        }\r\n    }\r\n\r\n    public class Program\r\n        {\r\n            public static void Main(string[] args)\r\n            {\r\n                CreateWebHostBuilder(args).Build().Run();\r\n            }\r\n\r\n            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\r\n                WebHost.CreateDefaultBuilder(args)\r\n                    .UseStartup&lt;Startup&gt;()\r\n                    .UseEnvironment(\"Development\");\r\n        }\r\n}<\/pre>\n<p>\uacb0\uacfc :<br \/>\nGood morning Dody, Hello world from Cairo<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder using Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder using Microsoft.AspNetCore.Http; \/\/WriteAsync using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Builder.Extensions; using Microsoft.AspNetCore; namespace WebApplication1 { public class Content { public string SayHello() =&gt; &#8220;Hello world&#8221;; } public class Greeting { public string Greet() =&gt; &#8220;Good morning&#8221;; } public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddSingleton&lt;Content&gt;(); } public void\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=650\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-net-core"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=650"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}