Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

'((System.Net.Http.HttpBaseStream)stream).Length' threw an exception of type 'System.NotSupportedException'

$
0
0

I'm getting this error from a behavior I don't understand:

Exception screenshot

I've looked online to try to find a solution but nothing worked or applied to my case.

It says that Stream.Length threw an exception but as I found online, people said to check the .CanSeek method and for each stream open I checked and they all returned true.

Here's the proof that it reads the length of the stream:

Debug output ( Sorry for the quality, screenshots were removing the debug output window ).

And this is right before returning the item, in this case a user, and everything works before it.

Here's my code for this method:

public async Task<RegistrationResponse> RegisterAsync(RegistrationRequest body){    // Create a new instance of the IHttpClientFactory    var httpClient = _httpClientFactory.CreateClient("default");    // Create a MemoryStream to be able to use compression    using (var memoryContentStream = new MemoryStream())    {        await JsonSerializer.SerializeAsync(memoryContentStream, body);        // CAN SEEK returns true        var canSeek = memoryContentStream.CanSeek;        memoryContentStream.Seek(0, SeekOrigin.Begin);        // Use HttpRequestMessage instead of the .PostAsync shortcut but either could be used        using (var httpRequest = new HttpRequestMessage(                   HttpMethod.Post,"/api/Accounts/register"))        {            // Add headers to the call            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));            httpRequest.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));            using (var compressedMemoryContentStream = new MemoryStream())            {                // Compress the content                using (var gzipStream = new GZipStream(                           compressedMemoryContentStream, CompressionMode.Compress))                {                    memoryContentStream.CopyTo(gzipStream);                    gzipStream.Flush();                    compressedMemoryContentStream.Position = 0;                    // CAN SEEK returns true                    var canSeek2 = compressedMemoryContentStream.CanSeek;                    // Create a stream                    using (var streamContent = new StreamContent(compressedMemoryContentStream))                    {                        // Add headers to the call                        streamContent.Headers.ContentType =                            new MediaTypeHeaderValue("application/json");                        streamContent.Headers.ContentEncoding.Add("gzip");                        httpRequest.Content = streamContent;                        var response = await httpClient.SendAsync(                            httpRequest,                            HttpCompletionOption.ResponseHeadersRead);                        response.EnsureSuccessStatusCode();                        // THIS IS WHERE IT DOESN'T WORK ANYMORE, even thought .Content shows the length of the stream                        var stream = await response.Content.ReadAsStreamAsync();                        var user = await JsonSerializer.DeserializeAsync<RegistrationResponse>(stream);                        return user;                    }                }            }        }    }}

Here's another picture for the stack errors:

Stacks Locals

UPDATE

Here's the code that calls the stream method:

async Task ValidSubmit(){    try    {        var catId = _configuration["OtherCategory"];        _processing = true;        var newRequest = new RegistrationRequest()                {                    FirstName = RegistrationRequest.FirstName,                    LastName = RegistrationRequest.LastName,                    CategoryId = Guid.Parse(catId),                    PhoneNumber = RegistrationRequest.PhoneNumber,                    Password = RegistrationRequest.Password                };        // RegistrationRequest.CategoryId = Guid.Parse(this.CategoryId);        IsSuccess = true;        await AuthenticationService.RegisterAsync(newRequest);        // SuccessNotification();        _navigationManager.NavigateTo("code-verification");    }    catch (Exception e)    {        throw;    }}

And what Exception e throws here is:

"Cannot access a closed stream"

at >System.ThrowHelper.ThrowObjectDisposedException_StreamClosed(StringobjectName)at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)at System.IO.Compression.DeflateStream.PurgeBuffers(Boolean disposing)at System.IO.Compression.DeflateStream.Dispose(Boolean disposing)at System.IO.Stream.Close()at System.IO.Compression.GZipStream.Dispose(Boolean disposing)at System.IO.Stream.Close()at Website.UI.Services.AuthenticationService.d__2.MoveNext()inC:\Users\something\source\repos\Website.Website\Website.UI\Services\AuthenticationService.cs:line75at Website.UI.Components.Pages.PageComponents.SignUpComponent.d__8.MoveNext()inC:\Users\something\source\repos\Website.Website\Website.UI\Components\Pages\PageComponents\SignUpComponent.razor:line114


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>