Adding an http client to a .net project
- Add the nuget package
Microsoft.Extensions.Http
- In your
Startup.cs
/Program.cs
, add
using System.Net;
using Microsoft.Extensions.DependencyInjection;
// ... in ConfigureDomainServices, or where-have-you
string acvApiHost = configuration.GetSection("Services").GetSection("AcvApi")["Host"];
if (String.IsNullOrEmpty(acvApiHost))
throw new ValidationException("Services__AcvApi__Host"); // Core.CustomExceptions
string acvApiToken = configuration.GetSection("Services").GetSection("AcvApi")["Token"];
if (String.IsNullOrEmpty(acvApiToken))
throw new ValidationException("Services__AcvApi__Token");
services.AddHttpClient<AcvApiService>(c =>
{
c.BaseAddress = new Uri(acvApiHost);
c.DefaultRequestVersion = HttpVersion.Version10; // it can't handle chunked
c.DefaultRequestHeaders.TransferEncodingChunked = false;
c.DefaultRequestHeaders.Add("Authorization", $"AcvAuth {acvApiToken}");
c.DefaultRequestHeaders.Add("User-Agent", _userAgent);
});