SYNT4X
SYNT4X

Reputation: 210

Why do I get a NullReferenceException when creating an Instance of System.Net.Http.HttpClient()?

In our Xamarin Forms App we have to load much data from multiple API´s. The easiest way is to use HttpClient directly fro the PCL, but I get this Error when there are several hundreds of them.

With this Page I could reproduce this issue in a fresh PCL Project.

using System;
using Xamarin.Forms;

namespace HTTPTest
{
public class HTTPTestPage : ContentPage
{
    int Index = 0;

    public HTTPTestPage()
    {
        Content = new Label
        {
            Text = "HTTP Test"
        };
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        StartLoading();
    }

    void StartLoading()
    {
        for (int i = 0; i < 400; i++)
        {
            if (string.IsNullOrEmpty(LoadStringFromEndpoint("https://google.de")))
            {
                break;
            }
        }
    }

    public string LoadStringFromEndpoint(string url)
    {
        System.Diagnostics.Debug.WriteLine("LoadFromEndpoint: " + url);
        try
        {
            Index++;
            var client = new System.Net.Http.HttpClient();
            return client.GetStringAsync(url).Result;
        }
        catch (NullReferenceException ex)
        {
            System.Diagnostics.Debug.WriteLine($"{ex.Message} at Index: {Index}");
            System.Diagnostics.Debug.WriteLine(ex.StackTrace);
        }
        return string.Empty;
    }
 }
}

And This is the result:

LoadFromEndpoint: https://google.de
Object reference not set to an instance of an object at Index: 123
  at System.Net.Http.RuntimeOptions.Read () [0x0002c] in /Users/builder/data/lanes/3985/9d6e1ab1/source/xamarin-macios/src/ObjCRuntime/RuntimeOptions.cs:229 
  at System.Net.Http.RuntimeOptions.GetHttpMessageHandler () [0x00000] in /Users/builder/data/lanes/3985/9d6e1ab1/source/xamarin-macios/src/ObjCRuntime/RuntimeOptions.cs:265 
  at System.Net.Http.HttpClient.GetDefaultHandler () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.121/src/mono/mcs/class/System.Net.Http/HttpClientEx.cs:36 
  at System.Net.Http.HttpClient..ctor () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.4.0.121/src/mono/mcs/class/System.Net.Http/HttpClientEx.cs:24 
  at HTTPTest.HTTPTestPage.LoadStringFromEndpoint (System.String url) [0x00020] in [...]HTTPTest/HTTPTestPCL/HTTPTestPage.xaml.cs:41 

If I get this Exception once and loading a file afterwards, I will also get a System.IO.Exception (too many open files). Is there a way to fix this (preferably without not using it)?

Upvotes: 1

Views: 2169

Answers (1)

Zroq
Zroq

Reputation: 8392

You shouldn't be creating that many instances of HttpClient. This is an object that allows multiple connections at the same time. So define your object outside your LoadStringFromEndpoint.

"The default HttpClient is the simplest way in which you can start sending requests. A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for all your requests."

More info

Upvotes: 6

Related Questions