Architecting your app http service – Consuming rest APIs

WhatsApp Image 2018-04-01 at 3.33.18 PM

Do you ever wonder if the code you are writing is good? If it is the best you can do? How do you know if the classes you are building are extensible? If the architecture you created for your app will actually make it’s maintenance less scary? Well, I believe that these thoughts never go away, it is an intrinsic part of being a developer. To question yourself is to always know that there is room for learning and to improve your code!

With these thoughts in mind I decided to share a few tips and tricks on how to architect your app’s http services. Things like data caching, http requests retry policy, off-line sync, local vs remote requests all of the architecture to consume Rest apis from your app!

1. Http Service Architecture

This is the base and most important part of our work, we will give some special attention to it. As always, all the code that we will be using here is available on GitHub check it out. Parts of this sample are brought to you by Hotel 360 app, I have been checking out this code that Microsoft built and I strongly suggest everyone to check it out, there are a lot of awesome examples there!

Let’s draw the idea of the solution presented in the GitHub sample app code:

Screen Shot 2018-04-01 at 1.53.29 PM

1.1. RequestService.cs

We have this as a generic service that will have all the common methods that are used on API requests like Get, Post and Put; also other helper methods to setup the request headers and to handle responses from the APIs. The code on this class was put here to avoid code repetition. The way RequestService is built makes it completely extensible, if we need any new services from the apis that we consume, we will just have to create a  new specific service and inject RequestService into it.

1.2. PersonService.cs

This is the specific request service where we are going to inject the generic service (RequestsService). In this class we decide which api methods we will use and do some other things like building the url.

1.3. ViewModels

Well, view models are the core of our MVVM approach, if you need more information on them check this link out, we are basically going to inject on the view models the specific request service, like PersonService, MonkeyService or any other services we need to use on our view models.

2. HttpClient as Singleton

Long story short, every time you create a instance of the HttpClient class you are also creating connections that will be held in TIME_WAIT state for up to 240 seconds, this can cause a socket exhaustion between your app and your API. Please check out this blog post that will give you all of the detailed information you need to know!

3. Native HttpClient Implementation

Let the target platforms handle the HttpClient implementation, instead of the mono stack. It is much faster and very simple to set up.


VS for mac: iOS project Options -> iOS Build -> HttpClient implementation -> NSUrlSession (iOS7+)

VS for windows: iOS project properties -> iOS Build -> HttpClient implementation -> NSUrlSession (iOS7+)

Check out this link to learn more about it in the iOS target platform.


VS for mac: Android project Options -> Android Build -> HttpClient implementation -> NSUrlSession (iOS7+)

VS for windows: Android project properties -> Android Options -> Advanced -> HttpClient implementation -> AndroidClientHandler (iOS7+)

Check out this link o learn more about it in the Android target platform.

4. .ConfigureAwait(false);

If you don’t know about this yet, you should definitely check it out here, you don’t want your app crashing due do deadlocks =O


  • Make sure all your API urls are using HTTPS or else the HttpClient Implementation on step 3 will throw exceptions on you.
  • This is a personal tip from Causer, Keep an eye on the community, keep an eye on how people code and do it your self (practice). These are the best ways, in my opinion, to improve your coding skills and become a better developer.

Special Thanks

I would like to give special thanks to my good friends @dylanberry for helping out with ideas and discussions for blog posts and @nosmirck with his amazing helps with banners! Both from the @TOMobileDev user group, a bunch of awesome Xamarin and C# developers!
throw new CauserException();


6 thoughts on “Architecting your app http service – Consuming rest APIs

  1. I see you don’t monetize your blog, don’t waste your traffic, you
    can earn extra bucks every month because you’ve got
    hi quality content. If you want to know how to make extra money, search for:
    Ercannou’s essential adsense alternative


  2. Hi Daniel. I found this article and repo to be a helpful start in my generic REST service. What would you suggest for handling things like Unauthorised outside of the RequestService class? I want to handle an unauthorised login but without using caught exceptions if possible.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s