How to Add Generated HttpClient to ASP.NET Core Dependency Injection (Right Way)
Tags: openapi, swagger, dependencyinjection, codegeneration
Published: 2019-07-06 12:41:52

---
title: How to Add Generated HttpClient to ASP.NET Core Dependency Injection (Right Way)
published: true
tags: open-api,swagger,dependency-injection,code-generation
canonical_url: 
description: This How-To describes how to inject dependency to HttpClient you generated for PetStore API with OpenAPI (Swagger) Connected Service to ASP.NET Core application. 
cover_image: https://raw.githubusercontent.com/dmitry-pavlov/openapi-connected-service/master/docs/img/OpenAPI-Swagger-Connected-Service.gif
---

This How-To describes how to inject dependency to HttpClient you generated for PetStore API with [OpenAPI (Swagger) Connected Service](https://marketplace.visualstudio.com/items?itemName=dmitry-pavlov.OpenAPIConnectedService) to ASP.NET Core application. The sample code is available in [here](https://github.com/dmitry-pavlov/openapi-connected-service/tree/master/docs/samples/Samples.AspNetCoreMvc.ClientInjectedToStartup).

> [OpenAPI (Swagger) Connected Service](https://marketplace.visualstudio.com/items?itemName=dmitry-pavlov.OpenAPIConnectedService) is a Visual Studio extension to generate C# HttpClient code for [OpenAPI (formerly Swagger)](https://swagger.io/docs/specification/about/) web services with [NSwag](https://github.com/RSuter/NSwag). Simply put, it is like kind old [Add Service Reference](https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide) for WCF or [Add Web Reference](https://docs.microsoft.com/en-us/sharepoint/dev/general-development/step-2-adding-a-web-reference) for WSDL, but for JSON API we all deal with nowadays.

Follow **5 steps** below to achieve this:

![](https://cdn-images-1.medium.com/max/816/1*HNl2C0p5ZEnAlu3Hq9Oevw.gif)

**Step 1.** Generate C# HttpClient class for [PetStore API](http://petstore.swagger.io/) in your ASP.NET Core MVCweb application using [OpenAPI (Swagger) Connected Service](https://marketplace.visualstudio.com/items?itemName=dmitry-pavlov.OpenAPIConnectedService). See [Getting Started](https://github.com/dmitry-pavlov/openapi-connected-service/) section to install and run this Visual Studio extension.

**Step 2.** Add new file PetStoreClient.cs and make sure the class is marked as partial and has the same name and namespace as generated public partial class Client in PetStoreClient.Generated.cs. Also define the interface IPetStoreClient. The idea behind using the interface is to separate the methods you see where your client is used from what you don’t want to expose. In this sample we just expose the method to retrieve the number of pets sold by the store. So we define the method GetSoldPetsCount, which internally uses generated method for [GET pet/findPetsByStatus](http://petstore.swagger.io/#/pet/findPetsByStatus) endpoint. That is how it might look like:

{% gist https://gist.github.com/dmitry-pavlov/6315ed63971d65f351fe81ed2deeaf88 %}

**Step 3.** Now let’s use [HttpClientFactory](https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests) to inject the IPetStoreClient. Go to Startup.cs and add this to ConfigureServices method:

{% gist https://gist.github.com/dmitry-pavlov/5b83c2f43ddedb701801c31f38857f6f %}

**Step 4.** In Index action in HomeController.cs let’s inject IPetStoreClient and pass the number of sold pets to the View:

{% gist https://gist.github.com/dmitry-pavlov/fe46fe1bb6e81098fa92cff1a8313937 %}

**Step 5.** To display the number of sold pets on the default site page add this code to View\Home\Index.cshtml:

{% gist https://gist.github.com/dmitry-pavlov/cbfe01b624dab355ed83210dda693582 %}

That’s it! We have injected generated client for [PetStore API](http://petstore.swagger.io/) to ASP.NET Core application via the [dependency injection (DI)](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) software design pattern.

![](https://cdn-images-1.medium.com/max/1024/1*m99BauIfEi7LFs6DsvwzMg.png)

Enjoy coding with the [Coding Machine](https://medium.com/@dmitry.pavlov)…