在 .NET 标准中创建 OWIN 中间件

Creating OWIN middleware in .NET standard

TL;DR;

有没有办法在 .NET Standard 中创建向后兼容 Microsoft.Owin 的 Owin 中间件?

详情

我创建了一个简单的中间件,例如:

public class RequestTrackingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestTrackingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        var sw = Stopwatch.StartNew();
        await _next(context);
        sw.Stop();
        Console.WriteLine(sw);
    }

我在 "classic" ASP.NET 应用程序中添加了 Owin 在板上。使用:

public override void Configuration(IAppBuilder app)
{
    app.Use<FinaiRequestTrackingMiddleware>();
}

但我最终得到:

No conversion available between Microsoft.AspNet.Identity.Owin.IdentityFactoryMiddleware`2[....] and Microsoft.AspNetCore.Http.RequestDelegate.

有什么想法吗?

是的,你可以,但这不是 OWIN 中间件,那是 ASP.NET 核心中间件。以这篇博客 post http://benfoster.io/blog/how-to-write-owin-middleware-in-5-different-steps (step 3 is the most relevant). You can then use that OWIN middleware in Katana or ASP.NET Core (via the OWIN bridge) see https://codeopinion.com/migrate-from-katana-asp-net-core/ 为例。