Skip to content

Commit 699a027

Browse files
WIP Named pipes
1 parent c45fa4c commit 699a027

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/Certify.Client/CertifyApiClient.cs

Lines changed: 57 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;
@@ -76,8 +77,23 @@ public CertifyApiClient(Providers.IServiceConfigProvider configProvider, Shared.
7677
_configProvider = configProvider;
7778

7879
_connectionConfig = connectionConfig ?? new ServerConnection(configProvider.GetServiceConfig());
80+
if (_connectionConfig.Mode == "namedpipe")
81+
{
82+
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}{_baseUri}";
83+
}
84+
else
85+
{
86+
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}{_baseUri}";
87+
}
7988

80-
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}" + _baseUri;
89+
if (_connectionConfig.Mode == "namedpipe")
90+
{
91+
_baseUri = $"http://localhost";
92+
}
93+
else
94+
{
95+
_baseUri = $"{(_connectionConfig.UseHTTPS ? "https" : "http")}://{_connectionConfig.Host}:{_connectionConfig.Port}{_baseUri}";
96+
}
8197

8298
CreateHttpClient();
8399

@@ -107,6 +123,37 @@ private void CreateHttpClient()
107123

108124
_client = new HttpClient(new HttpRetryMessageHandler(_httpClientHandler));
109125
}
126+
#if NET8_0_OR_GREATER
127+
else if (_connectionConfig.Mode == "namedpipe")
128+
{
129+
// https://andrewlock.net/using-named-pipes-with-aspnetcore-and-httpclient/
130+
131+
var httpHandler = new System.Net.Http.SocketsHttpHandler
132+
{
133+
// Called to open a new connection
134+
ConnectCallback = async (ctx, ct) =>
135+
{
136+
// Configure the named pipe stream
137+
var pipeClientStream = new NamedPipeClientStream(
138+
serverName: ".", // this machine
139+
pipeName: "certify-server",
140+
PipeDirection.InOut, // duplex stream
141+
PipeOptions.Asynchronous); // async
142+
143+
// Connect to the server!
144+
await pipeClientStream.ConnectAsync(ct);
145+
146+
return pipeClientStream;
147+
}
148+
};
149+
150+
// Create an HttpClient using the named pipe handler
151+
_client = new HttpClient(httpHandler)
152+
{
153+
BaseAddress = new Uri("http://localhost")
154+
};
155+
}
156+
#endif
110157
else
111158
{
112159
//alternative auth (jwt)
@@ -149,11 +196,18 @@ private void SetAuthContextForRequest(HttpRequestMessage request, AuthContext au
149196
}
150197
}
151198

199+
private Uri FormatEndpointURI(string endpoint)
200+
{
201+
return new Uri($"{_baseUri.TrimEnd('/')}/{endpoint}");
202+
}
203+
152204
private async Task<string> FetchAsync(string endpoint, AuthContext authContext)
153205
{
154206
try
155207
{
156-
using (var request = new HttpRequestMessage(HttpMethod.Get, new Uri(_baseUri + endpoint)))
208+
var endpointURI = FormatEndpointURI(endpoint);
209+
210+
using (var request = new HttpRequestMessage(HttpMethod.Get, endpointURI))
157211
{
158212
SetAuthContextForRequest(request, authContext);
159213

@@ -166,7 +220,7 @@ private async Task<string> FetchAsync(string endpoint, AuthContext authContext)
166220
else
167221
{
168222
var error = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
169-
throw new ServiceCommsException($"Internal Service Error: {endpoint}: {error} ");
223+
throw new ServiceCommsException($"Internal Service Error: {endpointURI}: {error} ");
170224
}
171225
}
172226
}

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)