Skip to content

Fix HttpClientHandler.CreateNativeHandler implementation #116707

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

Merged
merged 3 commits into from
Jun 17, 2025

Conversation

jkoritzinsky
Copy link
Member

Fix #116698

@jkoritzinsky jkoritzinsky added this to the 10.0.0 milestone Jun 16, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the implementation of HttpClientHandler.CreateNativeHandler to resolve #116698 by adjusting the native call signature and its invocation.

  • Modified the native handler call by adding a null parameter to CallNative.
  • Updated the extern method signature to include a parameter with an UnsafeAccessorType attribute, with conditional using directives for various target platforms.
Comments suppressed due to low confidence (2)

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs:341

  • Changing the CallNative invocation to pass a null argument may have platform-specific implications; please verify that the native implementation correctly handles a null parameter on all target platforms.
return (HttpMessageHandler)CallNative(null);

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs:344

  • The updated extern method signature now accepts a parameter annotated with UnsafeAccessorType. Ensure that this change aligns with the expected native binding on all platforms and that the attribute usage is properly resolved.
static extern GetHttpMessageHandlerReturnType CallNative([UnsafeAccessorType(GetHttpMessageHandlerType)] object? _);

@danmoseley
Copy link
Member

should/can the message be more helpful than " Invalid usage of UnsafeAccessorAttribute." ?

Copy link
Contributor

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

@akoeplinger
Copy link
Member

looks like there are build failures

Comment on lines 14 to 15
#if TARGET_ANDROID
using GetHttpMessageHandlerReturnType = object;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is we need to match the signature here:
https://github.com/dotnet/android/blob/04c44a87357285ae740ab0ed8976e8ff0645865a/src/Mono.Android/Android.Runtime/AndroidEnvironment.cs#L337

If that method returned HttpMessageHandler, then we wouldn't need this split.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning object might be intentional, but I don't see a commit mentioning it. It could allow apps to trim away System.Net.Http.HttpMessageHandler if unused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could allow apps to trim away System.Net.Http.HttpMessageHandler if unused.

I do not see why it would make a difference. The method below has HttpMessageHandler cast that will keep the type around.

#if TARGET_ANDROID
using GetHttpMessageHandlerReturnType = object;
#elif TARGET_IOS || TARGET_MACCATALYST || TARGET_TVOS
using GetHttpMessageHandlerReturnType = System.Net.Http.HttpMessageHandler;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we unify the return type between the two to avoid this ifdef?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonathanpeppers what do you think about us unifying the return type?

I'd like the simplicity of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying it out here:

There is an app size regression test for Android "hello world", so I don't see an issue unifying -- assuming that passes.

jonathanpeppers added a commit to dotnet/android that referenced this pull request Jun 16, 2025
Context: dotnet/runtime#116707

To prevent a `#if TARGET_ANDROID` in dotnet/runtime:

    #if TARGET_ANDROID
    using GetHttpMessageHandlerReturnType = object;
    #elif TARGET_IOS || TARGET_MACCATALYST || TARGET_TVOS
    using GetHttpMessageHandlerReturnType = System.Net.Http.HttpMessageHandler;
    #endif

Let's unify and use `System.Net.Http.HttpMessageHandler` as the return
type for `GetHttpMessageHandler()`.
jonathanpeppers added a commit to dotnet/android that referenced this pull request Jun 17, 2025
Context: dotnet/runtime#116707

To prevent a `#if TARGET_ANDROID` in dotnet/runtime:

    #if TARGET_ANDROID
    using GetHttpMessageHandlerReturnType = object;
    #elif TARGET_IOS || TARGET_MACCATALYST || TARGET_TVOS
    using GetHttpMessageHandlerReturnType = System.Net.Http.HttpMessageHandler;
    #endif

Let's unify and use `System.Net.Http.HttpMessageHandler` as the return
type for `GetHttpMessageHandler()`.

Removed a comment from Mono days, when we wouldn't want a cyclic
reference between `Mono.Android.dll` and `System.Net.Http.dll`. This
shouldn't be a problem anymore
@jkoritzinsky
Copy link
Member Author

/ba-g wasm failures unrelated

@jkoritzinsky jkoritzinsky merged commit c154588 into dotnet:main Jun 17, 2025
79 of 86 checks passed
@jkoritzinsky
Copy link
Member Author

/backport to release/10.0-preview6

Copy link
Contributor

Started backporting to release/10.0-preview6: https://github.com/dotnet/runtime/actions/runs/15717970523

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating the native http handler throws BadImageFormatException
7 participants