HTTP

Uno.Extensions.Http allows for the registration of API endpoints as multiple typed HttpClient instances. In this centralized location for accessing web resources, the lifecycle of the corresponding HttpMessageHandler objects is managed. Added clients can optionally be configured to use the platform-native handler. Additional functionality is provided to clear cookies or log diagnostic messages in responses. This library uses Microsoft.Extensions.Http for any HTTP related work.

For additional documentation on HTTP requests, read the references listed at the bottom.

Installation

Http is provided as an Uno Feature. To enable Http support in your application, add Http to the <UnoFeatures> property in the Class Library (.csproj) file.

Important

This walkthrough assumes you created your app using the Single Project template. If you used a different template, please refer to our Migrating Projects to Single Project documentation.

For more information about UnoFeatures refer to our Using the Uno.Sdk docs.

Register Endpoints

Web resources exposed through an API are defined in the application as clients. These client registrations include type arguments and endpoints to be used for the client. The endpoint is defined in the EndpointOptions class. While it uses the platform-native HTTP handler by default, this value can be configured.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    var builder = this.CreateBuilder(args)
        .Configure(host => {
            host
            .UseHttp((context, services) =>
            {
                services
                .AddClient<IShowService, ShowService>(context, "configsectionname");
            });
        });
    ...
}
Tip

If configuration sections are already used elsewhere, continuing to use that approach offers uniformity and broader accessibility of endpoint options. Consider whether this type of access is needed before using the alternate method below.

EndpointOptions can also be loaded from a specific instance.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    var builder = this.CreateBuilder(args)
        .Configure(host => {
            host
            .UseHttp((context, services) =>
            {
                services
                .AddClient<IShowService, ShowService>(context,
                    new EndpointOptions
                    {
                        Url = "https://ch9-app.azurewebsites.net/"
                    }
                    .Enable(nameof(EndpointOptions.UseNativeHandler)));
            });
        });
    ...
}

Custom Endpoint Options

EndpointOptions is a base class that provides a Url property. This property is used to specify the URL of the endpoint. Subclassing EndpointOptions allows for custom options beyond the Url such as a proxy, timeout, and adding headers. Using this method, the HttpClient associated with the endpoint can be configured from a single section in appsettings.json.

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    var appBuilder = this.CreateBuilder(args)
        .Configure(hostBuilder =>
        {
            hostBuilder.UseHttp((ctx, services) => {
                services.AddClientWithEndpoint<IShowService, ShowService, CustomEndpointOptions>();
            });
        });
    ...
}

For more information about configuring HttpClient with custom endpoint options, see the Configure HttpClient with Custom Endpoint Options tutorial.

Refit

Similarly, Refit endpoints can be registered as services and configured in a similar way.

private IHost Host { get; set; }

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    var builder = this.CreateBuilder(args)
        .Configure(host => {
            host
            .UseHttp((context, services) =>
            {
                services
                .AddRefitClient<IChuckNorrisEndpoint>(context);
            });
        });
    ...
}

In this case, the endpoint options will be loaded from configuration section ChuckNorrisEndpoint which can be defined as the following JSON:

{
  "ChuckNorrisEndpoint": {
    "Url": "https://api.chucknorris.io/",
    "UseNativeHandler": true
  }
}

For more information on using Refit, see the Quickly Create a Strongly-Typed REST Client for an API tutorial.

References