No conversion available between HelloWorldComponent and System.Func`2[System.Collections.Generic.IDictionary`2 // Parameter name: signature

大憨熊 提交于 2019-12-01 18:17:11
Ufuk Hacıoğulları

There is a new way to write our middlewares components, which looks like this:

public class HelloWorldComponent : OwinMiddleware
{
    public HelloWorldComponent(OwinMiddleware next) : base(next) { }

    public override Task Invoke(IOwinContext context)
    {
        return context.Response.WriteAsync("Hello, World!");
    }
}

Specifically, the constructor must accept an OwinMiddleware reference as its first parameter, otherwise you get an error because the ctor signature does not match what is expected by the current Owin implementation.

Further, consider the following parameterized usage:

    var param1 = "Hello, World!";
    appBuilder.Use<HelloWorldComponent>(param1)

To properly support this you will want a modified constructor signature:

public class HelloWorldComponent : OwinMiddleware
{
    public HelloWorldComponent(OwinMiddleware next) : base(next) { }

    public override Task Invoke(IOwinContext context, string param1)
    {
        return context.Response.WriteAsync(param1);
    }
}

Thus, allowing us to parameterize our middleware via Use()'s params array.

Inheriting from OwinMiddleware limits you to the Katana implementation of OWIN.
Creating a OwinContext from the passed environment should work for you

class HelloWorldComponent 
    {
        private readonly AppFunc _next;

        public HelloWorldComponent (AppFunc next)
        {
            _next = next;
        }

        public async Task Invoke(IDictionary<string, object> environment)
        {

            var ctx = new OwinContext(environment);
            await ctx.Response.WriteAsync("Hello World");
            await _next(environment);
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!