Skip to content

App Services' Client

App Services behave as an ordinary Controller, meaning that their methods are exposed using endpoints, with routes built conventionally, as exposed here.

The Suite framework allows you to auto-generate the App Service's client, with which you will be able to invoke IAppServices, through http. Yo can do so by injecting IAppServiceProxy<TAppService> and using its Client property.

In the following example you can see how to inject an auto-generated App Service client for InspectionsAppService, using its contract interface named IInspectionsAppService.

C#
1
2
3
4
5
6
7
public ClientHostedService(
    IAppServiceProxy<IInspectionsAppService> inspectionsAppServiceProxy, 
    ILogger<ClientHostedService> logger)
{
    this.inspectionsAppServiceProxy = inspectionsAppServiceProxy;
    this.logger = logger;
}

It's important to follow these rules for the App Service's to be able have its clients generated properly:

  • Do create an contract interface of your App Service class, to be able to inject auto-generated proxy client. The contract must implement IAppService.
  • Do name your interface as same as your App Service class, plus a leading I. For example, given an App Service called ServiceOrdersAppService, the contract interface must be called IServiceOrdersAppService.
  • Do define async methods only, sync methods are not supported.

Configuring underlying HttpClient

As mentioned before, auto-generated client uses HttpClient under the hood, which is obtained through IHttpClientFactory. To provide additional configuration the underlying HttpClient used by the auto-generated client feature, you can either particularize for all clients, by providing a default configuration, or you can narrow down particularization to apply just to a specific one.

Let's see an example of a module providing customizations to the underlying HttpClient used by App Service client.

C#
// ...
public override void SetupModule(IModuleBuilder builder)
{
    base.SetupModule(builder);
    builder.DependsOn<HttpModule, HttpModuleOptions>(options =>
    {
        // Configure default client.
        options.AppServiceProxyOptions.UseDefaultHttpClient("basic-sample-client");

        // Configure specific client.
        options.AppServiceProxyOptions.UseHttpClient<IInspectionsAppService>(httpClientBuilder =>
        {
            httpClientBuilder.AddSuiteHttpClientBehavior();
        });
    });
}

public override void ConfigureServices(
    IServiceCollection services, 
    ModuleConfigurationContext context)
{
    this.basicSampleUrl = "http://basic-sample";
    base.ConfigureServices(services, context);

    services.AddHttpClient("basic-sample-client")
        .AddSuiteHttpClientBehavior()
        .ConfigureHttpClient(client =>
        {
            client.BaseAddress = new Uri(this.basicSampleUrl);
        });
}
// ...

From the example, "basic-sample-client" settings are defined in ConfigureServices method to be used as default for any App Service. The HttpClient settings for IInspectionsAppService's client are defined inline, in the Setup method. If you prefer to provide the HttpClient settings either general or for specific App Service clients, you must add them using AddHttpClient method, supplying the name of the App Service being configured, using kebab-case format. For example, if we were configuring the IInspectionsAppService through IHttpClientFactory, we would do as follows:

C#
// ...
public override void ConfigureServices(
    IServiceCollection services, 
    ModuleConfigurationContext context)
{
    this.basicSampleUrl = "http://basic-sample";
    base.ConfigureServices(services, context);

    services.AddHttpClient("basic-sample-client")
        .AddSuiteHttpClientBehavior()
        .ConfigureHttpClient(client =>
        {
            client.BaseAddress = new Uri(this.basicSampleUrl);
        });


    services.AddHttpClient("i-inspections-app-service")
        .AddSuiteHttpClientBehavior()
        .ConfigureHttpClient(client =>
        {
            client.BaseAddress = new Uri("http://inspections");
        });
}
/// ...