Skip to content

Instantly share code, notes, and snippets.

@ichiroku11
Last active April 30, 2018 06:57
Show Gist options
  • Save ichiroku11/43198454586d99637284d6e6b74c5290 to your computer and use it in GitHub Desktop.
Save ichiroku11/43198454586d99637284d6e6b74c5290 to your computer and use it in GitHub Desktop.
ASP.NET CoreのMiddlewareを試す
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace WebApp {
public class Startup {
public void ConfigureServices(IServiceCollection services) {
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
// 最初に呼び出されるミドルウェア
app.Use(async (context, next) => {
context.Response.ContentType = "text/plain";
// 次を呼び出す前の処理
await context.Response.WriteAsync("Middleware 1 before\n");
// 次のミドルウェアを呼び出す
await next.Invoke();
// 次を呼び出した後の処理
await context.Response.WriteAsync("Middleware 1 after\n");
});
// 2つ目のミドルウェア
app.Use(async (context, next) => {
await context.Response.WriteAsync("\tMiddleware 2 before\n");
await next.Invoke();
await context.Response.WriteAsync("\tMiddleware 2 after\n");
});
// 最後に呼び出されるミドルウェア
app.Run(async context => {
await context.Response.WriteAsync("\t\tMiddleware 3\n");
});
}
}
}
// 実行結果
/*
Middleware 1 before
Middleware 2 before
Middleware 3
Middleware 2 after
Middleware 1 after
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace WebApp {
// 最初に呼び出されるミドルウェア
public class Middleware1 {
private readonly RequestDelegate _next;
public Middleware1(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context) {
context.Response.ContentType = "text/plain";
// 次を呼び出す前の処理
await context.Response.WriteAsync("Middleware 1 before\n");
// 次のミドルウェアを呼び出す
await _next(context);
// 次を呼び出した後の処理
await context.Response.WriteAsync("Middleware 1 after\n");
}
}
// 2つ目のミドルウェア
public class Middleware2 {
private readonly RequestDelegate _next;
public Middleware2(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context) {
await context.Response.WriteAsync("\tMiddleware 2 before\n");
await _next(context);
await context.Response.WriteAsync("\tMiddleware 2 after\n");
}
}
// 最後に呼び出されるミドルウェア
public class Middleware3 {
// 使わないけど
private readonly RequestDelegate _next;
public Middleware3(RequestDelegate next) {
_next = next;
}
public async Task InvokeAsync(HttpContext context) {
await context.Response.WriteAsync("\t\tMiddleware 3\n");
}
}
public class Startup {
public void ConfigureServices(IServiceCollection services) {
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
// 最初に呼び出されるミドルウェア
app.UseMiddleware<Middleware1>();
// 2つ目のミドルウェア
app.UseMiddleware<Middleware2>();
// 最後に呼び出されるミドルウェア
app.UseMiddleware<Middleware3>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment