Skip to content

Commit 6327608

Browse files
WIP Named pipes
1 parent c45fa4c commit 6327608

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/Certify.Client/CertifyApiClient.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Pipes;
45
using System.Net;
56
using System.Net.Http;
67
using System.Net.Http.Headers;
@@ -77,7 +78,14 @@ public CertifyApiClient(Providers.IServiceConfigProvider configProvider, Shared.
7778

7879
_connectionConfig = connectionConfig ?? new ServerConnection(configProvider.GetServiceConfig());
7980

80-
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}" + _baseUri;
81+
if (_connectionConfig.Mode == "namedpipe")
82+
{
83+
_baseUri = $"http://localhost";
84+
}
85+
else
86+
{
87+
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}{_baseUri}";
88+
}
8189

8290
CreateHttpClient();
8391

@@ -107,6 +115,37 @@ private void CreateHttpClient()
107115

108116
_client = new HttpClient(new HttpRetryMessageHandler(_httpClientHandler));
109117
}
118+
#if NET8_0_OR_GREATER
119+
else if (_connectionConfig.Mode == "namedpipe")
120+
{
121+
// https://andrewlock.net/using-named-pipes-with-aspnetcore-and-httpclient/
122+
123+
var httpHandler = new System.Net.Http.SocketsHttpHandler
124+
{
125+
// Called to open a new connection
126+
ConnectCallback = async (ctx, ct) =>
127+
{
128+
// Configure the named pipe stream
129+
var pipeClientStream = new NamedPipeClientStream(
130+
serverName: ".", // this machine
131+
pipeName: "certify-server",
132+
PipeDirection.InOut, // duplex stream
133+
PipeOptions.Asynchronous); // async
134+
135+
// Connect to the server!
136+
await pipeClientStream.ConnectAsync(ct);
137+
138+
return pipeClientStream;
139+
}
140+
};
141+
142+
// Create an HttpClient using the named pipe handler
143+
_client = new HttpClient(httpHandler)
144+
{
145+
BaseAddress = new Uri("http://127.0.0.2")
146+
};
147+
}
148+
#endif
110149
else
111150
{
112151
//alternative auth (jwt)
@@ -149,11 +188,18 @@ private void SetAuthContextForRequest(HttpRequestMessage request, AuthContext au
149188
}
150189
}
151190

191+
private Uri FormatEndpointURI(string endpoint)
192+
{
193+
return new Uri($"{_baseUri.TrimEnd('/')}/{endpoint}");
194+
}
195+
152196
private async Task<string> FetchAsync(string endpoint, AuthContext authContext)
153197
{
154198
try
155199
{
156-
using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri + endpoint)))
200+
var endpointURI = FormatEndpointURI(endpoint);
201+
202+
using (var request = new HttpRequestMessage(HttpMethod.Get, endpointURI))
157203
{
158204
SetAuthContextForRequest(request, authContext);
159205

@@ -166,7 +212,7 @@ private async Task<string> FetchAsync(string endpoint, AuthContext authContext)
166212
else
167213
{
168214
var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
169-
throw new ServiceCommsException($"Internal Service Error: {endpoint}: {error} ");
215+
throw new ServiceCommsException($"Internal Service Error: {endpointURI}: {error} ");
170216
}
171217
}
172218
}

src/Certify.Server/Certify.Server.Core/Certify.Server.Core/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
var builder = WebApplication.CreateBuilder(args);
55

6+
#if DEBUG
7+
// https://learn.microsoft.com/en-us/aspnet/core/grpc/interprocess-namedpipes?view=aspnetcore-8.0
8+
builder.WebHost.ConfigureKestrel(opts => opts.ListenNamedPipe("certify-service"));
9+
#endif
10+
611
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
712
{
813
builder.Services.AddSystemd()

src/Certify.Server/Certify.Server.Hub.Api/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public void ConfigureServices(IServiceCollection services)
207207

208208
backendServiceConnectionConfig.Authentication = "jwt";
209209
backendServiceConnectionConfig.ServerMode = "v2";
210+
//backendServiceConnectionConfig.Mode = "namedpipe";
210211

211212
System.Diagnostics.Debug.WriteLine($"Public API: connecting to background service {serviceConfig.Host}:{serviceConfig.Port}");
212213

0 commit comments

Comments
 (0)