Skip to content

Add default support for WasmHttpHandler using mono-wasm selector #161

Closed

Description

@jeromelaban

Add support for default http client support through mono/mono#10152

Activity

  1. added
    kind/enhancement
    platform/wasm 🌐
    on Aug 30, 2018
  2. jeromelaban commented on Nov 28, 2018

    @jeromelaban
    MemberAuthor

    Here's a sample code to use the mono backdoor for setting the default HttpMessageHandler:

    var httpMessageHandler = Type
      .GetType("System.Net.Http.HttpClient, System.Net.Http")
      .GetField(
        "GetHttpMessageHandler",
        BindingFlags.Static | BindingFlags.NonPublic
      );
    
    httpMessageHandler.SetValue(
      null,
      (Func<HttpMessageHandler>)(() => new Uno.UI.Wasm.WasmHttpHandler())
    );
  3. MelbourneDeveloper commented on Feb 18, 2019

    @MelbourneDeveloper

    @jeromelaban , I can't get HttpClient to work. In .NET Core, this code works:

                var httpClient = new HttpClient();
                var response = await httpClient.GetAsync("http://services.groupkt.com/country/get/all");
                var data = await response.Content.ReadAsStringAsync();

    I tried this in Uno:

            private static void Main(string[] args)
            {
    
                var httpMessageHandler = Type
                  .GetType("System.Net.Http.HttpClient, System.Net.Http")
                  .GetField(
                    "GetHttpMessageHandler",
                    BindingFlags.Static | BindingFlags.NonPublic
                  );
    
                httpMessageHandler.SetValue(
                  null,
                  (Func<HttpMessageHandler>)(() => new Uno.UI.Wasm.WasmHttpHandler())
                );
    
                App.HttpClient = new HttpClient();
    
    
                Windows.UI.Xaml.Application.Start(_ => _app = new App());
            }

    Exception:

    System.NullReferenceException: Object reference not set to an instance of an object. at CameraCaptureUISample.Wasm.Program.Main (System.String[] args) <0x1cc8f18 + 0x00054> in <4b48b7a454c54034a15176c7464ed9b3>:0

    and

        static void Main(string[] args)
        {
                App.HttpClient = new HttpClient(new WasmHttpHandler());
                Windows.UI.Xaml.Application.Start(_ => _app = new App());
        }
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                var response = await App.HttpClient.GetAsync("http://services.groupkt.com/country/get/all");
                button.Text = (await response.Content.ReadAsStringAsync());
            }
            catch(Exception ex)
            {
                button.Text = ex.ToString();
            }
        }

    Exception:

    System.Exception: NetworkError when attempting to fetch resource. at Uno.Threading.FastTaskCompletionSource1[T].GetResult () <0x2699888 + 0x0005e> in <4f734568ee5d4fc882e3c02b78c0cf28>:0 at Uno.Threading.FastTaskCompletionSource1[T].CreateAsyncTask () <0x25cd998 + 0x000a4> in <4f734568ee5d4fc882e3c02b78c0cf28>:0 at Uno.UI.Wasm.WasmHttpHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) <0x2588430 + 0x0057e> in <98b54d8c3c2044068638992450330dc8>:0 at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) <0x25737a8 + 0x00182> in <73181a0c3a9143aba3405a65287fbca2>:0 at CameraCaptureUISample.MainPage.MainPage_Loaded (System.Object sender, Windows.UI.Xaml.RoutedEventArgs e) <0x25626e8 + 0x000f0> in :0

  4. D-R-R commented on Mar 4, 2019

    @D-R-R

    In addition to getting the above method with making Uno.UI.Wasm.WasmHttpHandler the default via reflection working, I've also had success with the WasmHttpMessageHandler that ships with Mono:
    https://github.com/mono/mono/blob/master/sdks/wasm/framework/src/WebAssembly.Net.Http/WasmHttpMessageHandler.cs

    There is code to configure it automatically (so no need for private reflection), although that didn't work for me out-of-the-box:
    https://github.com/mono/mono/blob/master/mcs/class/System.Net.Http/System.Net.Http/HttpClient.wasm.cs

    It turned out that it was being linked away and I just needed to add this to LinkerConfig.xml to preserve it:

    	<assembly fullname="WebAssembly.Net.Http">
    		<type fullname="*" />
    	</assembly>
    
  5. 12 remaining items

  6. modified the milestones: HighPriority, 3.1 on Sep 1, 2020
  7. modified the milestones: 3.1, 3.2 on Sep 14, 2020
  8. added
    project/non-ui ⚙️
    on Sep 21, 2020
  9. removed this from the 3.2 milestone on Oct 19, 2020
  10. self-assigned this
    on Oct 26, 2020
  11. added this to the 3.3 milestone on Nov 3, 2020
  12. modified the milestones: 3.3, 3.4 on Nov 11, 2020
  13. added a commit that references this issue on Jan 17, 2024
  14. added a commit that references this issue on Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/enhancementplatform/wasm 🌐project/non-ui ⚙️

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions