Skip to content

Commit

Permalink
Remove httpclient customization
Browse files Browse the repository at this point in the history
Make websocket handler scoped
  • Loading branch information
Farid Kadyrov committed Apr 22, 2020
1 parent df971bb commit 2382b6c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
12 changes: 9 additions & 3 deletions App2/App3Client.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
using System.Net.Http;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace App2
{
public class App3Client
{
private readonly HttpClient _client;
private readonly IConfiguration _config;

public App3Client(HttpClient client)
public App3Client(HttpClient client, IConfiguration config)
{
_client = client;
_config = config;
}

public async Task SendAsync(string message)
{
await _client.PostAsync("/ping", new StringContent(message, Encoding.UTF8, "application/json"));
var uri = new Uri(_config["Dependency"] + "/ping");

await _client.PostAsync(uri, new StringContent(message, Encoding.UTF8, "application/json"));
}
}
}
5 changes: 1 addition & 4 deletions App2/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddApplicationInsightsTelemetry();

services.AddControllers();
services.AddHttpClient<App3Client>(client =>
{
client.BaseAddress = new Uri(Configuration["Dependency"]);
});
services.AddHttpClient<App3Client>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion App2/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
}
},
"AllowedHosts": "*",
"Dependency": "http://localhost:5002/app3"
"Dependency": "http://localhost:5002"
}
9 changes: 7 additions & 2 deletions Origin1/App2Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Origin1
{
public class App2Client
{
private readonly HttpClient _client;
private readonly IConfiguration _config;

public App2Client(HttpClient client)
public App2Client(HttpClient client, IConfiguration config)
{
_client = client;
_config = config;
}

public async Task SendAsync(string message)
{
var resp = await _client.PostAsync("/ping", new StringContent(message, Encoding.UTF8, "application/json"));
var uri = new Uri(_config["Dependency"] + "/ping");

var resp = await _client.PostAsync(uri, new StringContent(message, Encoding.UTF8, "application/json"));
Console.WriteLine(await resp.Content.ReadAsStringAsync());
}
}
Expand Down
7 changes: 2 additions & 5 deletions Origin1/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddApplicationInsightsTelemetry();

services.AddControllers();
services.AddHttpClient<App2Client>(client =>
{
client.BaseAddress = new Uri(Configuration["Dependency"]);
});
services.AddSingleton<ReproWebSocketHandler>();
services.AddHttpClient<App2Client>();
services.AddScoped<ReproWebSocketHandler>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
20 changes: 13 additions & 7 deletions Origin1/WebSockets/ReproWebSocketMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Net.WebSockets;
using System.Text;
Expand All @@ -13,20 +14,17 @@ namespace Origin1.WebSockets
public class ReproWebSocketMiddleware
{
private readonly ILogger<ReproWebSocketHandler> _logger;
private readonly ReproWebSocketHandler _handler;
private readonly RequestDelegate _next;

public ReproWebSocketMiddleware(
ILogger<ReproWebSocketHandler> logger,
ReproWebSocketHandler handler,
RequestDelegate next)
{
_logger = logger;
_handler = handler;
_next = next;
}

public async Task Invoke(HttpContext context)
public async Task Invoke(HttpContext context, ReproWebSocketHandler handler)
{
try
{
Expand All @@ -37,7 +35,7 @@ public async Task Invoke(HttpContext context)
}

WebSocket socket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);
await Receive(socket, context.RequestAborted);
await Receive(socket, handler, context.RequestAborted);
}
catch (Exception ex)
{
Expand All @@ -46,7 +44,8 @@ public async Task Invoke(HttpContext context)
}


private async Task Receive(WebSocket socket, CancellationToken contextRequestAborted) //, Action<WebSocketReceiveResult, string> handleMessage)
private async Task Receive(WebSocket socket, ReproWebSocketHandler handler,
CancellationToken contextRequestAborted) //, Action<WebSocketReceiveResult, string> handleMessage)
{
while (socket.State == WebSocketState.Open)
{
Expand Down Expand Up @@ -82,8 +81,15 @@ private async Task Receive(WebSocket socket, CancellationToken contextRequestAbo
{
ArrayPool<byte>.Shared.Return(bytes);
}

var acitvity = new Activity("Test");
acitvity.Start();

Console.WriteLine("Activity: {0}", acitvity.Id);

await _handler.HandleAsync(serializedMessage);
await handler.HandleAsync(serializedMessage);

acitvity.Stop();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Origin1/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
}
},
"AllowedHosts": "*",
"Dependency": "http://localhost:5001/app2"
"Dependency": "http://localhost:5001"
}

0 comments on commit 2382b6c

Please sign in to comment.