dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.47k stars 4.77k forks source link

Compressed Request doesn't work in .NET 6 but it is ok in .Net Core 2.2 #77586

Closed predavasile88 closed 2 years ago

predavasile88 commented 2 years ago

Is there an existing issue for this?

Describe the bug

Recently I worked on migration a web app from .Net Core 2.2 to Net 6. During the testing phases I found an issue with a request that cannot be handled by the web app in .NET 6.

The error is: Unable to translate bytes at index from specified code page to Unicode

The request is created from .Net Framework App and is something like this:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Extensions.Compression.Core.Compressors;
using System.Net.Http.Extensions.Compression.Core.Models;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Client_Net_Framewok
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            await CreateRessource(compressed: true).ConfigureAwait(false);
        }
        private static async Task CreateRessource(bool compressed)
        {
            var ressourceContent = JsonConvert.SerializeObject(new Address()
            {
                Street = "Main Street"
            });

            var httpClient = new HttpClient()
            {
                BaseAddress = new Uri("http://localhost:11791/"), //net 2.2
                //BaseAddress = new Uri("http://localhost:5272/"),
                Timeout = Timeout.InfiniteTimeSpan
            };
            var strContent = new StringContent(ressourceContent, Encoding.UTF8, "application/json");
            var compressedContent = new CompressedContent(strContent, new GZipCompressor());
            var response = compressed ?
                            await httpClient.PostAsync("/api/Address", compressedContent).ConfigureAwait(false)
                            : await httpClient.PostAsync("/api/Address", strContent).ConfigureAwait(false);

            var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
        }
    }

    public class Address
    {
        public string Street { get; set; }
    }
}

Expected Behavior

IMHO the functionality that works in .Net Core 2.2 should work also in .Net 6. No exception should occur.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

6.0.401

Anything else?

No response

ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Issue Details
### Is there an existing issue for this? - [X] I have searched the existing issues ### Describe the bug Recently I worked on migration a web app from .Net Core 2.2 to Net 6. During the testing phases I found an issue with a request that cannot be handled by the web app in .NET 6. The error is: _Unable to translate bytes __ at index __ from specified code page to Unicode_ The request is created from .Net Framework App and is something like this: ``` using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Extensions.Compression.Core.Compressors; using System.Net.Http.Extensions.Compression.Core.Models; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Client_Net_Framewok { internal class Program { static async Task Main(string[] args) { Console.WriteLine("Hello, World!"); await CreateRessource(compressed: true).ConfigureAwait(false); } private static async Task CreateRessource(bool compressed) { var ressourceContent = JsonConvert.SerializeObject(new Address() { Street = "Main Street" }); var httpClient = new HttpClient() { BaseAddress = new Uri("http://localhost:11791/"), //net 2.2 //BaseAddress = new Uri("http://localhost:5272/"), Timeout = Timeout.InfiniteTimeSpan }; var strContent = new StringContent(ressourceContent, Encoding.UTF8, "application/json"); var compressedContent = new CompressedContent(strContent, new GZipCompressor()); var response = compressed ? await httpClient.PostAsync("/api/Address", compressedContent).ConfigureAwait(false) : await httpClient.PostAsync("/api/Address", strContent).ConfigureAwait(false); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); } } public class Address { public string Street { get; set; } } } ``` ### Expected Behavior IMHO the functionality that works in .Net Core 2.2 should work also in .Net 6. No exception should occur. ### Steps To Reproduce _No response_ ### Exceptions (if any) _No response_ ### .NET Version 6.0.401 ### Anything else? _No response_
Author: predavasile88
Assignees: -
Labels: `area-System.Net.Http`, `untriaged`
Milestone: -
stephentoub commented 2 years ago

Can you share a standalone repro?

predavasile88 commented 2 years ago

@stephentoub , I just found that the compressed requires a custom middleware to handle the compressed requests (no matter if is v2.2 or v6). My issue was represented by the fact that I was missing the custom middleware in .Net 6. I think this issue can be solved.

stephentoub commented 2 years ago

Thanks. Closing.