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 ;
@@ -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 }
0 commit comments