Skip to content
This repository was archived by the owner on Jan 24, 2021. It is now read-only.
This repository was archived by the owner on Jan 24, 2021. It is now read-only.

Request body is always empty in .NET Core 2.2 using TestServer as an host #2997

@thomasraynal

Description

@thomasraynal
  • I have written a descriptive issue title
  • I have verified that I am running the latest version of Nancy
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Hello,

When using TestServer (Microsoft.AspNetCore.TestHost) with Owin to host a Nancy module in .NET Core 2.2, the request body is always empty.

It works fine when using Kestrel.

TestServer with Nancy works fine in .Net Core 3.0.

I didn't find any TestServer related issues on Nancy Github, nor on the web.

Consider the following (I can put it on Github for convenience, with a branch for each environnement):

    public class Tests
    {
        public class TestStartup
        {
            public void Configure(IApplicationBuilder app)
            {
                app.UseOwin(conf => conf.UseNancy());
            }

#if NETCOREAPP3_0
            public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<KestrelServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                });


            }

#endif
        }

        public class Module : NancyModule
        {
            public Module()
            {
                Post("/", args =>
                {
                    var str = Request.Body.AsString();
                    Assert.IsNotEmpty(str);

                    return str;
                });
            }
        }

        [Test]
        public async Task Test1()
        {
            var builder = new WebHostBuilder()
                .UseStartup<TestStartup>();

            using (var server = new TestServer(builder))
            {
#if NETCOREAPP3_0
                server.AllowSynchronousIO = true;
#endif
                using (var client = server.CreateClient())
                {

                    var str = "string";

                    var httpContent = new StringContent(str);

                    var response = await client.PostAsync("/", httpContent);

                    Assert.IsNotEmpty(await response.Content.ReadAsStringAsync());
                }
            }
        }

        [Test]
        public async Task Test2()
        {

            Task.Run(() =>
            {
                var host = new WebHostBuilder()
               .UseKestrel()
               .UseUrls("http://localhost:8080")
               .UseStartup<TestStartup>()
               .Build();

                host.Run();
            });


            var str = "string";

            var httpContent = new StringContent(str);

            var client = new HttpClient();

            var response = await client.PostAsync("http://localhost:8080", httpContent);

            Assert.IsNotEmpty(await response.Content.ReadAsStringAsync());

        
        }
  • Nancy version: 2.0.0
  • Nancy host
    • Nancy.Hosting.Aspnet
    • Nancy.Hosting.Self
    • Nancy.Owin (TestServer)
    • Other:

.NET Core 2.2 config:

<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
<PackageReference Include="Nancy" Version="2.0.0" />
<PackageReference Include="Nancy.Owin" Version="2.0.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />

.NET Core 3.0 config:

<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0" />
<PackageReference Include="Nancy" Version="2.0.0" />
<PackageReference Include="Nancy.Owin" Version="2.0.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions