Skip to content

HTTP Module

Suite Modules composing an application can provide http features to be used either by the Suite Modules themselves or by the user.

Said features are provided mainly as delegating handlers which can be applied to an HttpClient instance afterwards, building up an outgoing calling pipeline with the proper behavior provided by one or more of the Suite Modules. Said handlers are added through contributors, which are being invoked during the HttpClient creation, to provide the expected behavior.

Using http clients

To use an HttpClient with the behavior provided by the Suite modules, the recommended approach is to use the SuiteHttpClient HttpClientFactory which is integrated to Microsoft Dependency Injection. The way to go is to let the Suite framework to configure the

C#
public class SampleModule : SuiteModule {
    public override void ConfigureServices(IServiceCollection services,
        ModuleConfigurationContext context)
    {
        // add sample client with all suite behavior
        services.AddSuiteHttpClient("sd-client", c =>
        {
            c.BaseAddress = new Uri("http://PositionService/");
        });
    }
}

First thing to note is that we are using here IHttpClientFactory ot build and configure HttpClient instances. A the moment of creating the HttpClient instance you will need to specify some basic values, such as BaseAddress for example, or any other value that you may need.

The outcome of the AddSuiteHttpClient method is an IHttpClientBuilder instance which already has all the suite behavior plugged into it, but you can go on providing additional configurations.

After configuring the HttpClient as said before, you can inject and IHttpClientFactory and create the HttpClient out of it, by invoking CreateClient method, or by injecting the typed client in your services if you prefer to use typed clients.

C#
1
2
3
4
5
6
// ...

var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
httpClientFactory.CreateClient("sd-client").GetAsync("/api/someMethod");

// ...

You may also need to use just one of the available behaviors provided by the Suite, not all of them. If you are in such situation, and you are very confident of which of these you really need, then you can add them by specifying individually. Let's see an example:

C#
public class SampleModule : SuiteModule {
    public override void ConfigureServices(IServiceCollection services,
        ModuleConfigurationContext context)
    {
        // add sample client with all suite behavior
        services.AddHttpClient("sd-client", c =>
        {
            c.BaseAddress = new Uri("http://PositionService/");
        }).AddServiceDiscovery();
    }
}

In the previous example we are adding only Service Discovery behavior to an an IHttpClientBuilder, which is being defined an configured through the IHttpClientFactory.

Note

The recommended way to configure and use HttpClients either for Suite modules or feature modules, is through named AddSuiteHttpClient or typed
AddSuiteHttpClient<TClient> methods. Each module can, and should, define its own HttpClients to suite its needs.

Note

These extension methods are defined in ITsynch.Suite.Http.Extensions.dll assembly.

Defining an HttpClient with specific behaviors

If you want to add specific behaviors, you can use extension methods provided by each Suite module. You must be aware of them by reading the module documentation to know how to configure and use the provided behavior. In some cases it might be possible / needed to configure the module through its options before it can be used.

Important

To avoid race conditions and improper behavior when configuring specific behaviors you must be aware of the correct order in which they need to be added. We encourage you to use the conventional behavior provided by the extensions methods that do this for you adding all the available behavior whenever is possible.

Using typed client for feature modules

For features providing integration to external modules which need to be accessed through HppClient such as Rest APIs, we encourage you to provide a Typed HttpClient with the proper and pre-configured behavior.

As a client of an external API we expect in the future to be able to inject its Typed client and use it explicit methods rather than wiring and configuring http requests. The following is a rule of thumb:

Important

Each module or feature exposing an HTTP Rest API to interact with must provide its corresponding typed HttpClient to be used from the client modules being integrated.