11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . IO . Pipes ;
45using System . Net ;
56using System . Net . Http ;
67using 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 }
0 commit comments