Dynamic C# API Clients

ABP can dynamically create C# API client proxies to call remote HTTP services (REST APIs). In this way, you don’t need to deal with HttpClient and other low level HTTP features to call remote services and get results.

Service Interface

Your service/controller should implement an interface that is shared between the server and the client. So, first define a service interface in a shared library project. Example:

  1. public interface IBookAppService : IApplicationService
  2. {
  3. Task<List<BookDto>> GetListAsync();
  4. }

Your interface should implement the IRemoteService interface to be automatically discovered. Since the IApplicationService inherits the IRemoteService interface, the IBookAppService above satisfies this condition.

Implement this class in your service application. You can use auto API controller system to expose the service as a REST API endpoint.

Client Proxy Generation

First, add Volo.Abp.Http.Client nuget package to your client project:

  1. Install-Package Volo.Abp.Http.Client

Then add AbpHttpClientModule dependency to your module:

  1. [DependsOn(typeof(AbpHttpClientModule))] //add the dependency
  2. public class MyClientAppModule : AbpModule
  3. {
  4. }

Now, it’s ready to create the client proxies. Example:

  1. [DependsOn(
  2. typeof(AbpHttpClientModule), //used to create client proxies
  3. typeof(BookStoreApplicationContractsModule) //contains the application service interfaces
  4. )]
  5. public class MyClientAppModule : AbpModule
  6. {
  7. public override void ConfigureServices(ServiceConfigurationContext context)
  8. {
  9. //Create dynamic client proxies
  10. context.Services.AddHttpClientProxies(
  11. typeof(BookStoreApplicationContractsModule).Assembly
  12. );
  13. }
  14. }

AddHttpClientProxies method gets an assembly, finds all service interfaces in the given assembly, creates and registers proxy classes.

Endpoint Configuration

RemoteServices section in the appsettings.json file is used to get remote service address by default. Simplest configuration is shown below:

  1. {
  2. "RemoteServices": {
  3. "Default": {
  4. "BaseUrl": "http://localhost:53929/"
  5. }
  6. }
  7. }

See the “AbpRemoteServiceOptions” section below for more detailed configuration.

Usage

It’s straightforward to use. Just inject the service interface in the client application code:

  1. public class MyService : ITransientDependency
  2. {
  3. private readonly IBookAppService _bookService;
  4. public MyService(IBookAppService bookService)
  5. {
  6. _bookService = bookService;
  7. }
  8. public async Task DoIt()
  9. {
  10. var books = await _bookService.GetListAsync();
  11. foreach (var book in books)
  12. {
  13. Console.WriteLine($"[BOOK {book.Id}] Name={book.Name}");
  14. }
  15. }
  16. }

This sample injects the IBookAppService service interface defined above. The dynamic client proxy implementation makes an HTTP call whenever a service method is called by the client.

IHttpClientProxy Interface

While you can inject IBookAppService like above to use the client proxy, you could inject IHttpClientProxy<IBookAppService> for a more explicit usage. In this case you will use the Service property of the IHttpClientProxy<T> interface.

Configuration

AbpRemoteServiceOptions

AbpRemoteServiceOptions is automatically set from the appsettings.json by default. Alternatively, you can use Configure method to set or override it. Example:

  1. public override void ConfigureServices(ServiceConfigurationContext context)
  2. {
  3. context.Services.Configure<AbpRemoteServiceOptions>(options =>
  4. {
  5. options.RemoteServices.Default =
  6. new RemoteServiceConfiguration("http://localhost:53929/");
  7. });
  8. //...
  9. }

Multiple Remote Service Endpoints

The examples above have configured the “Default” remote service endpoint. You may have different endpoints for different services (as like in a microservice approach where each microservice has different endpoints). In this case, you can add other endpoints to your configuration file:

  1. {
  2. "RemoteServices": {
  3. "Default": {
  4. "BaseUrl": "http://localhost:53929/"
  5. },
  6. "BookStore": {
  7. "BaseUrl": "http://localhost:48392/"
  8. }
  9. }
  10. }

AddHttpClientProxies method can get an additional parameter for the remote service name. Example:

  1. context.Services.AddHttpClientProxies(
  2. typeof(BookStoreApplicationContractsModule).Assembly,
  3. remoteServiceConfigurationName: "BookStore"
  4. );

remoteServiceConfigurationName parameter matches the service endpoint configured via AbpRemoteServiceOptions. If the BookStore endpoint is not defined then it fallbacks to the Default endpoint.

As Default Services

When you create a service proxy for IBookAppService, you can directly inject the IBookAppService to use the proxy client (as shown in the usage section). You can pass asDefaultServices: false to the AddHttpClientProxies method to disable this feature.

  1. context.Services.AddHttpClientProxies(
  2. typeof(BookStoreApplicationContractsModule).Assembly,
  3. asDefaultServices: false
  4. );

Using asDefaultServices: false may only be needed if your application has already an implementation of the service and you do not want to override/replace the other implementation by your client proxy.

If you disable asDefaultServices, you can only use IHttpClientProxy<T> interface to use the client proxies (see the related section above).