ASP.NET Core Development - middleware (Middleware)

ASP.NET Core development, the development and use of middleware (Middleware).

Middleware is assembled into a pipeline to process the software application component requests and responses.

Each component is transmitted to the requesting whether to select the next component in the conduit, and to perform certain operations in the pipeline after the call and prior to the next component.

Specifically, as shown:

 

Development of middleware (Middleware)

Today we achieved a record ip middleware.

1. Create a new asp.net core project, choose an empty template.

Then add a Microsoft.Extensions.Logging.Console project

NuGet command line, use the official source.

Install-Package Microsoft.Extensions.Logging.Console -Pre

2. Create a new class: RequestIPMiddleware.cs

复制代码
    public class RequestIPMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger _logger;

        public RequestIPMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            _next = next;
            _logger = loggerFactory.CreateLogger<RequestIPMiddleware>();
        }

        public async Task Invoke(HttpContext context)
        {            
            _logger.LogInformation("User IP: " + context.Connection.RemoteIpAddress.ToString());
            await _next.Invoke(context);
        }
    }
复制代码

 

3.再新建一个:RequestIPExtensions.cs

复制代码
    public static class RequestIPExtensions
    {
        public static IApplicationBuilder UseRequestIP(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RequestIPMiddleware>();
        }
    }
复制代码

这样我们就编写好了一个中间件。

使用中间件(Middleware)

1.使用

在 Startup.cs 添加 app.UseRequestIP()

复制代码
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
        {
            loggerfactory.AddConsole(minLevel: LogLevel.Information);
            app.UseRequestIP();//使用中间件
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
复制代码

然后运行程序,我选择使用Kestrel 。

访问:http://localhost:5000/

成功运行。

这里我们还可以对这个中间件进行进一步改进,增加更多的功能,如限制访问等。

 

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。

 
分类:  ASP.NET Core
标签:  ASP.NET Core

 

 

ASP.NET Core开发,开发并使用中间件(Middleware)。

中间件是被组装成一个应用程序管道来处理请求和响应的软件组件。

每个组件选择是否传递给管道中的下一个组件的请求,并能之前和下一组分在管道中调用之后执行特定操作。

具体如图:

 

开发中间件(Middleware)

今天我们来实现一个记录ip 的中间件。

1.新建一个asp.net core项目,选择空的模板。

然后为项目添加一个 Microsoft.Extensions.Logging.Console

NuGet 命令行 ,请使用官方源。

Install-Package Microsoft.Extensions.Logging.Console -Pre

2.新建一个类: RequestIPMiddleware.cs

复制代码
    public class RequestIPMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger _logger;

        public RequestIPMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        {
            _next = next;
            _logger = loggerFactory.CreateLogger<RequestIPMiddleware>();
        }

        public async Task Invoke(HttpContext context)
        {            
            _logger.LogInformation("User IP: " + context.Connection.RemoteIpAddress.ToString());
            await _next.Invoke(context);
        }
    }
复制代码

 

3.再新建一个:RequestIPExtensions.cs

复制代码
    public static class RequestIPExtensions
    {
        public static IApplicationBuilder UseRequestIP(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RequestIPMiddleware>();
        }
    }
复制代码

这样我们就编写好了一个中间件。

使用中间件(Middleware)

1.使用

在 Startup.cs 添加 app.UseRequestIP()

复制代码
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
        {
            loggerfactory.AddConsole(minLevel: LogLevel.Information);
            app.UseRequestIP();//使用中间件
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
复制代码

然后运行程序,我选择使用Kestrel 。

访问:http://localhost:5000/

成功运行。

这里我们还可以对这个中间件进行进一步改进,增加更多的功能,如限制访问等。

 

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。

Guess you like

Origin www.cnblogs.com/webenh/p/11574710.html
Recommended