Question

I try to build a parametered middleware, but I only get http 504 error. This code works fine without the parameter

any idea?

public void Configuration(IAppBuilder app)
{
    app.Use<MyMiddleware>("Hello");
}

class MyMiddleware : OwinMiddleware
{
    public MyMiddleware(OwinMiddleware next, string message)
    : base(next)
    {
        Message = message;
    }

    public string Message  { get; set; }

    public override async Task Invoke(IOwinContext context)
    {
        context.Response.ContentLength = Message.Length;
        context.Response.ContentType = "text/html";
        await context.Response.WriteAsync(Message);
        await Next.Invoke(context);
    }
}
Was it helpful?

Solution

Ok I found the issue

ContentLength should be evaluated in Utf-8 like this:

Encoding.UTF8.GetBytes(Message).Length

c# uses utf-16 encoding by default

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top