Created
February 8, 2018 14:18
-
-
Save MatthewDavidCampbell/73a603e544c6a6413096994ac5dc3ed7 to your computer and use it in GitHub Desktop.
HttpClientFactory netstandard2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Hosting; | |
namespace HttpClientEndpoint | |
{ | |
public class BackgroundThing : IHostedService, IDisposable | |
{ | |
private readonly IHttpClientFactory _httpClientFactory; | |
public BackgroundThing( | |
IHttpClientFactory httpClientFactory | |
) { | |
_httpClientFactory = httpClientFactory; | |
} | |
public void Dispose() | |
{ | |
} | |
public async Task StartAsync(CancellationToken cancellationToken) | |
{ | |
Console.WriteLine("Starting connections"); | |
for(int i = 0; i<10; i++) | |
{ | |
var client = _httpClientFactory.CreateClient("AspNetMonsters"); | |
var result = await client.GetAsync(""); | |
Console.WriteLine(result.StatusCode); | |
} | |
Console.WriteLine("Connections done"); | |
} | |
public Task StopAsync(CancellationToken cancellationToken) | |
{ | |
return Task.CompletedTask; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
<LangVersion>7.1</LangVersion> | |
</PropertyGroup> | |
<ItemGroup> | |
<Folder Include="wwwroot\" /> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" /> | |
<PackageReference Include="Microsoft.Extensions.Http" Version="2.1.0-alpha-1" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TCP 172.26.215.21:59419 104.42.226.176:https TIME_WAIT | |
TCP 172.26.215.21:59420 waws-prod-bay-017:http ESTABLISHED | |
TCP 172.26.215.21:59421 waws-prod-bay-017:https ESTABLISHED | |
TCP 172.26.215.21:59423 ec2-54-159-86-114:https ESTABLISHED | |
TCP 172.26.215.21:59426 ec2-34-227-148-88:https ESTABLISHED | |
TCP 172.26.215.21:59565 sdcsrvfs01:microsoft-ds ESTABLISHED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
namespace HttpClientEndpoint | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BuildWebHost(args).Run(); | |
} | |
public static IWebHost BuildWebHost(string[] args) => | |
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() | |
.Build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
namespace HttpClientEndpoint | |
{ | |
public class Startup | |
{ | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc(); | |
services.AddHttpClient("GitHubClient", client => { | |
client.BaseAddress = new Uri("https://api.github.com/"); | |
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); | |
client.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactoryTesting"); | |
}); | |
services.AddHttpClient("AspNetMonsters", client => { | |
client.BaseAddress = new Uri("http://aspnetmonsters.com"); | |
}); | |
services.AddSingleton<IHostedService, BackgroundThing>(); | |
} | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} else | |
{ | |
app.UseExceptionHandler("/Error"); | |
} | |
app.UseMvcWithDefaultRoute(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment