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

Weird using block in AndroidClientHandler prevents Polly from working! #4476

Closed
ysmoradi opened this issue Mar 28, 2020 · 2 comments
Closed
Assignees
Labels
Area: HTTP Issues with sockets / HttpClient.

Comments

@ysmoradi
Copy link

Important note:
Note that if you register http client and polly using

services.AddHttpClient();

it will use HttpClientHandler by default, even if you choose android client handler by followings:

<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>

and http client handler has no issue and works fine with Polly on Xamarin.Android

Steps to Reproduce

  1. Create a new Xamarin.Android project wih following nuget packages:
    <PackageReference Include="Microsoft.Extensions.Http.Polly">
      <Version>3.1.3</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug">
      <Version>3.1.3</Version>
    </PackageReference>
    <PackageReference Include="System.Net.Http">
      <Version>4.3.4</Version>
    </PackageReference>
  1. Then invoke following code wherever you prefer
ServiceCollection services = new ServiceCollection();

services.AddSingleton<AndroidClientHandler>();

IAsyncPolicy<HttpResponseMessage> policy = Policy.Handle<HttpRequestException>()
    .OrResult<HttpResponseMessage>((response) =>
    {
        return !response.IsSuccessStatusCode;
    })
    .WaitAndRetryAsync(new[]
    {
        TimeSpan.FromSeconds(1),
        TimeSpan.FromSeconds(1)
    });

services.AddHttpClient("Default")
    .SetHandlerLifetime(Timeout.InfiniteTimeSpan)
    .ConfigurePrimaryHttpMessageHandler<AndroidClientHandler>()
    .AddPolicyHandler(policy);

services.AddLogging(loggingBuilder =>
{
    loggingBuilder.AddDebug();
});

await using var serviceProvider = services.BuildServiceProvider();

var response = await serviceProvider.GetRequiredService<IHttpClientFactory>()
    .CreateClient("Default")
    .PostAsync("https://google.com", new StringContent(string.Empty));

Expected Behavior

response object must be created with a status code equal to 405 MethodNotAllowed
This is a behavior of HttpClientHandler across .net framework, .net core and even Xamarin.Android

Actual Behavior

You'll get ObjectDisposedException because of the following using block

using (var stream = await request.Content.ReadAsStreamAsync ().ConfigureAwait (false)) 
{
     await stream.CopyToAsync(httpConnection.OutputStream, 4096, cancellationToken).ConfigureAwait(false);
     if (stream.CanSeek)
          stream.Seek (0, SeekOrigin.Begin);
}

If you remove that using block, the problem goes away. There is one question to ask here, why do we dispose stream while we're seeking to its beginning for the next usages?
I'm sure that there somethings that I'm not aware of, but it's a good idea to take a look at this!

Version Information

Microsoft Visual Studio Enterprise 2019
Version 16.5.1
VisualStudio.16.Release/16.5.1+29920.165
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Enterprise

Architecture Diagrams and Analysis Tools 00433-90000-00004-AA054
Microsoft Architecture Diagrams and Analysis Tools

Visual C++ 2019 00433-90000-00004-AA054
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019 16.5.236.49856
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 16.5.236.49856
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.5.236.49856
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.5.236.49856
Azure Functions and Web Jobs Tools

BusinessObjectEditor 1.0
Information about my package

C# Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

DevExpress.DeploymentTool 1.0
A useful tool for deploying DevExpress assemblies.

DevExpress.ExpressApp.Design.DynamicPackage 1.0
DevExpress VSIX Package

DevExpress.ExpressApp.DesignPackage 1.0
DevExpress VSIX Package

Extensibility Message Bus 1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub.VisualStudio 2.11.89.33912
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension 1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30207.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.25+gdacdb9b7a1
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.5.512 (7a72495)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Snapshot Debugging Extension 1.0
Snapshot Debugging Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.62003.05170
Microsoft SQL Server Data Tools

StylerPackage Extension 1.0
StylerPackage Visual Stuido Extension Detailed Info

TypeScript Tools 16.0.20225.2001
TypeScript Tools for Microsoft Visual Studio

UnoPlatformPackage Extension 1.0
UnoPlatformPackage Visual Studio Extension Detailed Info

Visual Basic Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.8.0.0 for F# 4.7 16.5.0-beta.20104.8+7c4de19faf36647c1ef700e655a52350840c6f03
Microsoft Visual F# Tools 10.8.0.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview) 1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for Containers 1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog 1.0
Information about my package

VisualStudio.Foo 1.0
Information about my package

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Web Compiler 1.12.394
Compiler for LESS, Sass and CoffeeScript files

Xamarin 16.5.000.521 (d16-5@b3ea41e)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.5.0.470 (remotes/origin/d16-5@681de3fd6)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.5.49 (0904f41)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 10.2.0.100 (d16-5/988c811)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: c0c5c78
Java.Interop: xamarin/java.interop/d16-5@fc18c54
ProGuard: xamarin/proguard@905836d
SQLite: xamarin/sqlite@46204c4
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-5@9f4ed4b

Xamarin.iOS and Xamarin.Mac SDK 13.14.1.39 (30e8706)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Log File

N/A

@ysmoradi ysmoradi added the Area: App Runtime Issues in `libmonodroid.so`. label Mar 28, 2020
@grendello grendello added Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). and removed Area: App Runtime Issues in `libmonodroid.so`. labels Mar 31, 2020
@grendello grendello self-assigned this Mar 31, 2020
@grendello grendello added this to the Under Consideration milestone Mar 31, 2020
@cytoph
Copy link

cytoph commented Mar 8, 2021

I have the exact same problem. Would be nice if this issue would be looked at again.
But at least your workaround (removing the using statement) works for me for the moment.

@jpobst jpobst added Area: HTTP Issues with sockets / HttpClient. and removed Area: Mono.Android Issues with the Android API binding (Mono.Android.dll). labels Sep 6, 2022
@DuncanMT
Copy link

I have just run into this problem as well, it's frustrating that this issue is also present in the new AndroidMessageHandler class. Is the only workaround to implement a custom Message Handler ourselves at this point?

jonathanpeppers pushed a commit that referenced this issue Mar 11, 2024
…eHandler (#8764)

Fixes: #2901
Fixes:  #4476
Fixes: #7086

Neither the `SocketsHttpHandler` nor the `iOS/macOS` `NSUrlSessionHandler` dispose the content stream,
let's follow suit.
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: HTTP Issues with sockets / HttpClient.
Projects
None yet
Development

No branches or pull requests

5 participants