Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnSendingHeaders-Callback called twice #130

Open
kryz opened this issue Oct 23, 2017 · 2 comments
Open

OnSendingHeaders-Callback called twice #130

kryz opened this issue Oct 23, 2017 · 2 comments
Milestone

Comments

@kryz
Copy link

kryz commented Oct 23, 2017

In my Owin Middleware I subscribe a callback to the OnSendingHeaders. In my UnitTests (see below), the callback is sometimes called twice. Most of the time, the first TestCase succeeds and the second fails. Sometimes both succeed, sometimes both fail. Also on production I have the problem, that the callback is sometimes called more than once.

My expectation would have been, that this callback is only called once. The documentation doesn’t give a hint for this behavior, nor does it exclude it. Therefor my question: Is this behavior on purpose? Or is this a bug?

public class MyMiddleware
{
    public void Initialize(Func<IDictionary<string, object>, Task> next) { }

    public int Count { get; private set; }

    public async Task Invoke(IDictionary<string, object> env)
    {
        IOwinContext context = new OwinContext(env);
        context.Response.OnSendingHeaders(dummy =>
        {
            Count++;
        }, null);

        await context.Response.WriteAsync("something");
    }
}


[TestFixture]
public class MyMiddlewareTests
{
    [TestCase(1), TestCase(2)]
    public async Task TheTest(int dummy)
    {
        var myMiddleware = new MyMiddleware();
        using (var server = TestServer.Create(app => { app.Use(myMiddleware); }))
        {
            var response = await server.HttpClient.GetAsync("/");
            Assert.AreEqual(1, myMiddleware.Count);
        }
    }
}
@muratg muratg added this to the Backlog milestone Oct 27, 2017
@kryz
Copy link
Author

kryz commented Dec 11, 2017

Update: We realized that we have a different problem on production, i.e. I can only confirm that the callback is called twice with the testserver.

@kryz kryz closed this as completed Dec 11, 2017
@kryz kryz reopened this Dec 11, 2017
@Aarskin
Copy link

Aarskin commented Aug 6, 2018

FWIW, I am also seeing this - it causes a lot of uncertainty when trying to work with System.Diagnostics.Activity (on the second invocation, Activity.Current may be null).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants