Creating a ServiceBase class for client apps

1 week ago 15
ARTICLE AD BOX

I have multiple client applications that call my backend API to get data and this creates a situation where I have duplicate code in my client apps. I'm now attempting to create an ApiServiceBase class where I can move all common code into one place and consume them from there.

It seems pretty simple but it's my first attempt in creating and consuming such a base class for a service that I'm clearly making some beginner mistakes. I have created and used base classes for POCO objects and thought creating and consuming them in services shouldn't be that different.

I created my base class and turned it into a NuGet package. The base class is fairly simple at this point and it looks something like this:

public class ApiServiceBase { IHttpClientFactory _httpFactory; public ApiServiceBase(IConfiguration config, IHttpClientFactory httpFactory) { // Read settings from config and inject HttpClientFactory _httpFactory = httpFactory; } public async Task<Account> GetUserAccount() { // Simple API call that returns User account for current user } public async Task<T> GetAsync<T>(string url) { // Code that handles read operation logic } public async Task<TResponse> PostAsync<TRequest, TResponse>(string url, TRequest data) { // Code that handles write operation logic } }

As I said, I created a NuGet package for this simple base class which I install in my client apps i.e. Blazor WASM and .NET MAUI. Then in my individual client apps, I create an ApiService class that implements this base class like this:

public class ApiService : ApiServiceBase, IApiService { public ApiService(IConfiguration config, IHttpClientFactory httpFactory) : base(config, httpFactory) { } public async Task<SomeData> GetSomeData() { var data = await GetAsync("/myendpoint"); // This GetAsync() method is in ApiServiceBase return data; } }

In the Program.cs, I have the following for ApiService:

... builder.Services.AddSingleton<IApiService, ApiService>(); ...

I then simply call my API method in ApiService in my client app. In the below example, I show how I do it in my ViewModel for my home page in my .NET MAUI app:

public partial class HomeViewModel : BaseViewModel { private readonly IApiService _myApi; public HomeViewModel(IApiService myApi) { _myApi = myApi; } internal async Task InitAsync() { var data = await _myApi.GetSomeData(); } }

This should call the GetSomeData() method in local ApiService which should utilize the general purpose GetAync<T>() method in ApiServiceBase. Similarly, I want to be able to call the GetUserAccount() method in ApiServiceBase directly from the HomeViewModel.

When I run my client app, I get no errors but it seems to just sit there and do nothing. It doesn't crash or throw errors but I never hit my break points where I call the GetSomeData() method in my ApiService.

I have two questions:

What am I missing so far that my client app just hangs without crashing. Clearly, there's a problem with the way I implemented this. I also want to create an interface for ApiServiceBase. In my initial attempts, it keeps telling me that ApiService does not implement the methods that are in ApiServiceBase. For example, it tells me something like ApiService doesn't implement GetUserAccount() method which is in ApiServiceBase and also referenced in the IApiServiceBase interface.

I'd appreciate some pointers here. As I said, this is my first attempt in creating a base class to use in a service and it looks like I'm making some beginner mistakes.

Read Entire Article