How-To: Register an Endpoint for HTTP Requests
When working with a complex application, centralized registration of your API endpoints is a good practice. This allows you to easily change the endpoint for a given service, and to easily add new services.
Step-by-step
Important
This guide assumes you used the template wizard or dotnet new unoapp
to create your solution. If not, it is recommended that you follow the Creating an application with Uno.Extensions documentation to create an application from the template.
1. Installation
Add
HttpRefit
(orHttpKiota
if using Kiota-generated clients) to the<UnoFeatures>
property in the Class Library (.csproj) file.<UnoFeatures> Material; Extensions; - Http; + HttpRefit; Toolkit; MVUX; </UnoFeatures>
Note
As of Uno Platform 6.0, Http no longer includes Uno.Extensions.Http.Refit. Use HttpRefit for Refit-based clients or HttpKiota for Kiota-generated clients. Migrating to Uno Platform 6.0
2. Enable HTTP
- Call the appropriate method to register a HTTP client with the
IHostBuilder
which implementsIHttpClient
:Use .UseHttpRefit() for Refit clients
Use .UseHttpKiota() for Kiota clients
protected override void OnLaunched(LaunchActivatedEventArgs args) { var appBuilder = this.CreateBuilder(args) .Configure(hostBuilder => { hostBuilder.UseHttp(); hostBuilder.UseHttpRefit(); // or UseHttpKiota() }); ... }
3. Register Endpoints
The
AddRefitClient
orAddKiotaClient
extension method is used to register a client with the service collection when using Refit or Kiota respectively in Uno.Extensions.While these extension methods can take a delegate as its argument, the recommended way to configure the HTTP client is to specify a configuration section name. This allows you to configure the added HTTP client using the
appsettings.json
file.protected override void OnLaunched(LaunchActivatedEventArgs args) { var appBuilder = this.CreateBuilder(args) .Configure(hostBuilder => { hostBuilder.UseHttp(services => services.AddRefitClient<IShowService>("ShowService") // For Kiota: // hostBuilder.UseHttpKiota(services => // services.AddKiotaClient<IShowService>("ShowService") // ); ); }); ... }
Ultimately, your service will be based on the functionality provided by the web API, but the
IShowService
interface will be implemented by Refit or Kiota and injected into your application at runtime. You will make requests to the registered endpoint through this interface. In this case, the service interface will look something like this:public interface IShowService { Task<Show> GetShowAsync(); }
The endpoint is defined in the
appsettings.json
file. While the default behavior is to use the platform-native HTTP handler, this can be configured.{ "ShowService": { "Url": "https://ch9-app.azurewebsites.net/", "UseNativeHandler": true } }
4. Use the Service to Request Data
Since you registered the service with the service collection, you can now inject the
IShowService
implementation into your view models and use it to request information about a show from the endpoint:public class ShowViewModel : ObservableObject { private readonly IShowService _showService; public ShowViewModel(IShowService showService) { _showService = showService; } public async Task LoadShowAsync() { var show = await _showService.GetShowAsync(); ... } ... }