7

Do we create/use an HttpClient in a .NET MAUI app, the same way we do, say, in a web or API app? Specifically, I add my HttpClient's to my app in Program.cs using IHttpFactory -- see below:

builder.Services.AddHttpClient("NamedClient1", config =>
{
    var url = currentState == "production"
        ? "https://my-production-url"
        : "https://sandbox-url";
    config.BaseAddress = new Uri(url);
});

I understand the idea with .NET MAUI is to standardize the way we handle things. When I tried adding the line builder.Services in my .NET MAUI app, IntelliSense does not suggest AddHttpClient though.

Sam
  • 26,817
  • 58
  • 206
  • 383
  • Maybe `builder.Services.AddSingleton(...);`. I see code with `services.AddSingleton<>`, where `services` is a `ServiceCollection`, throughout Maui sources. – ToolmakerSteve May 29 '22 at 22:44
  • 6
    I think you might need to install the `Microsoft.Extensions.Http` nuget package to get the `AddHttpClient` extension method. – Michal Diviš May 30 '22 at 04:55

3 Answers3

4

HttpClient in mobile development is a little different from Web. The devices handle HttpClients themselves pretty well, and we don't have to worry much about Disposing it correctly by using nuget packages (Microsoft.Extensions.Http). So you can just fine use AddSingleton instead, or even use static readonly of the HttpClient. The .NET MAUI team made sure HttpClient class is configured this way.

Source from Microsoft:

The .NET MAUI templates map the HttpClient class to code that utilizes to the native networking stack of each platform. This enables an application to take advantage of platform-specific network configuration and optimization features

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Zecret
  • 360
  • 1
  • 4
  • 19
  • Its useful to also provide a link to the microsoft web page. And given that the question is about the **code** needed, showing source code is important. – ToolmakerSteve Jul 06 '23 at 20:52
4

Use as follows for .Net6 and .Net7:

    builder.Services.AddScoped(sp => new HttpClient { });
Matias Masso
  • 1,670
  • 3
  • 18
  • 28
-3

Temperature.xaml.cs

using System.Diagnostics.Metrics;
using System.Text.Json;
using System.Xml.Serialization;

namespace TPMaui3.Views;

public partial class vTemperature : ContentPage
{
    public vTemperature()
    {
        InitializeComponent();
    }

    private void AfficheTemperature(object sender, EventArgs e)
    {
        LireTemperature();
    }

    private async void LireTemperature()
    {
        Uri uri = new("http://meteorestsrvmobile.lab3il.fr/RestServiceMeteo.svc/xml/1");

        try
        {
            HttpClient client = new();
            HttpResponseMessage response = await client.GetAsync(uri);

            if (response.IsSuccessStatusCode)
            {
                String content = await response.Content.ReadAsStringAsync();
                content = content.Substring(content.IndexOf("<XMLDataResult>") + 15);
                content = content.Substring(0, content.IndexOf("</XMLDataResult>"));
                lbTemp.Text = content + " °C";
            }
        }
        catch (Exception ex)
        {
            await this.DisplayAlert("Error", ex.Message, "OK");
        }
    }
}

vMeteo.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TPMaui3.Views.vMeteo"
    Title="Météo"
>
    <VerticalStackLayout Spacing="20" Margin="10" >
        <Label
            Text="Météo"
            x:Name="lbMeteo"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Primary}"
        />
        <Label
            Text="?"
            x:Name="lbTemperature"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbPression"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbHumidite"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbPrecipitationJour"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbTemperatureRessentie"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbVentDirection"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
        <Label
            Text="?"
            x:Name="lbVentVitesse"
            VerticalOptions="Center"
            HorizontalOptions="Center" 
            FontSize="Medium" 
            TextColor="{StaticResource Secondary}"
        />
    </VerticalStackLayout>
</ContentPage>

vMeteo.xaml.cs

using System.Diagnostics.Metrics;
using System.Text.Json;
using TPMaui3.Models;

namespace TPMaui3.Views;

public partial class vMeteo : ContentPage
{
    public vMeteo()
    {
        InitializeComponent();
        Meteo();
    }

    private async void Meteo()
    {
        Uri uri = new("http://meteorestsrvmobile.lab3il.fr/RestServiceMeteo.svc/jsontmps");
        try
        {
            HttpClient client = new();
            HttpResponseMessage response = await client.GetAsync(uri);
            JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();
                result = result.Replace("{\"JSONDataTmpsResult\":", "");
                result = result.Replace("}}", "}");

                var vMeteo = Newtonsoft.Json.JsonConvert.DeserializeObject<CMeteo>(result);
                lbMeteo.Text = "Relevé à " + vMeteo.DateReleve.ToString();
                lbTemperature.Text = "Temp: " + vMeteo.Temperature.ToString() + " °C";
                lbHumidite.Text = "Hum: " + vMeteo.Humidite.ToString() + " %";
                lbPression.Text = "Pres: " + vMeteo.Pression.ToString() + " hPa";
                lbTemperatureRessentie.Text = "TR: " + vMeteo.TemperatureRessentie.ToString() + " °C";
                lbPrecipitationJour.Text = "PrecJ: " + vMeteo.PrecipitationJour.ToString() + " mm";
                lbVentVitesse.Text = "Vit: " + vMeteo.VentVitesse.ToString() + " m/s";
                lbVentDirection.Text = "Vent: " + vMeteo.VentDirection.ToString();
            }
        }
        catch (Exception ex)
        {
            await this.DisplayAlert("Error", ex.Message, "OK");
        }
    }
}

CMeteo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TPMaui3.Views;

namespace TPMaui3.Models;

internal class CMeteo
{
    public CMeteo()
    {
    }

    public string DateReleve { get; set; }
    public string Temperature { get; set; }
    public string Pression { get; set; }
    public string Humidite { get; set; }
    public string PrecipitationJour { get; set; }
    public string TemperatureRessentie { get; set; }
    public string VentDirection { get; set; }
    public string VentVitesse { get; set; }
}
Jacob Smit
  • 2,206
  • 1
  • 9
  • 19
gamzeml
  • 7
  • 1