Skip to content

IHttpClientFactory using simple, named and typed clients approach

License

Notifications You must be signed in to change notification settings

zeecorleone/OpenWeatherAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenWeatherAPI C# IHttpClientFactory License

Introduction

Welcome to the OpenWeatherAPI repository! This project demonstrates how to call a third-party API (OpenWeather API in this case) using the three approaches of IHttpClientFactory in .NET: simple/classic, named, and typed clients.

Features

  • IHttpClientFactory: Use the factory to manage HttpClient instances effectively.
  • Three Approaches: Learn the basic/classic IHttpClientFactory client in controller, the "named" HTTP client, and the "typed" HTTP client.
  • OpenWeather API Integration: Fetch weather data using the OpenWeather API.

Medium Article

I have written a Medium article on this topic: Use IHttpClientFactory Right Way!. In this article, I discuss the benefits of using IHttpClientFactory, the problems it solves (e.g., socket exhaustion), and the three approaches to use HttpClient with code examples just like in this repository.

Getting Started

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/zeecorleone/OpenWeatherAPI.git
    cd OpenWeatherAPI
  2. Restore dependencies

    dotnet restore
  3. Set up your OpenWeather API key

    • Set your OpenWeather API Key in environment variable named OPENWEATHER_API_KEY
  4. Run the application

    dotnet run

Usage

Once the application is running, you can use endpoints (mentioned below) to fetch weather data from the OpenWeather API.

Example Endpoints:

  • Without Using HttpClientFactory: https://localhost:7272/weatherforecast/GetOpenWeather
  • Using HttpClientFacotry (simple): https://localhost:7272/weatherforecast/GetOpenWeatherV1
  • Using HttpClientFacotry (named): https://localhost:7272/weatherforecast/GetOpenWeatherV2
  • Using HttpClientFacotry (typed): https://localhost:7272/weatherforecast/GetOpenWeatherV3

Future Plans

I plan to extend this example in future articles. Potential topics include:

  • Handling third-party API call failures.
  • Implementing retry policies.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss what you would like to change.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

IHttpClientFactory using simple, named and typed clients approach

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages