{"id":654,"date":"2019-09-25T17:34:05","date_gmt":"2019-09-25T08:34:05","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=654"},"modified":"2019-09-25T17:34:05","modified_gmt":"2019-09-25T08:34:05","slug":"middleware-10","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=654","title":{"rendered":"middleware-10"},"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.Extensions.Logging;\r\nusing Microsoft.AspNetCore;\r\nusing System;\r\n\r\nnamespace WebApplication1\r\n{  \r\n    public class Startup\r\n    {\r\n        public void ConfigureServices(IServiceCollection services)\r\n        {\r\n        }\r\n\r\n        public void Configure(IApplicationBuilder app)\r\n        {\r\n            app.UseMiddleware(typeof(TerminalMiddleware));\r\n        }\r\n    }\r\n\r\n    public class TerminalMiddleware\r\n    {\r\n        DateTime _date = DateTime.Now;\r\n\r\n        public TerminalMiddleware(RequestDelegate next)\r\n        {\r\n        }\r\n\r\n        public async Task Invoke(HttpContext context, ILogger&lt;TerminalMiddleware&gt; log)\r\n        {\r\n            log.LogDebug($\"Request: {context.Request.Path}\");\r\n            context.Response.Headers.Add(\"Content-Type\", \"text\/plain\");\r\n            await context.Response.WriteAsync($\"Middleware is singleton. Keep refreshing the page. You will see that the date does not change {_date}.\");\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 : Middleware is singleton. Keep refreshing the page. You will see that the date does not change 2019-09-25 \uc624\ud6c4 5:32:41.<\/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.Extensions.Logging; using Microsoft.AspNetCore; using System; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app) { app.UseMiddleware(typeof(TerminalMiddleware)); } } public class TerminalMiddleware { DateTime _date = DateTime.Now; public TerminalMiddleware(RequestDelegate next) { } public async\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=654\">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-654","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\/654","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=654"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/654\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/654\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}