Skip to content
This repository has been 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

Open
5 of 8 tasks
thomasraynal opened this issue Dec 11, 2019 · 0 comments
Open
5 of 8 tasks

Comments

@thomasraynal
Copy link

thomasraynal commented Dec 11, 2019

  • 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" />
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant