@@ -18,23 +18,8 @@ internal class MsQuicApi : IDisposable
1818
1919 private readonly IntPtr _registrationContext ;
2020
21- private unsafe MsQuicApi ( )
21+ private unsafe MsQuicApi ( MsQuicNativeMethods . NativeApi * registration )
2222 {
23- MsQuicNativeMethods . NativeApi * registration ;
24-
25- try
26- {
27- uint status = Interop . MsQuic . MsQuicOpen ( out registration ) ;
28- if ( ! MsQuicStatusHelper . SuccessfulStatusCode ( status ) )
29- {
30- throw new NotSupportedException ( SR . net_quic_notsupported ) ;
31- }
32- }
33- catch ( DllNotFoundException )
34- {
35- throw new NotSupportedException ( SR . net_quic_notsupported ) ;
36- }
37-
3823 MsQuicNativeMethods . NativeApi nativeRegistration = * registration ;
3924
4025 RegistrationOpenDelegate =
@@ -138,7 +123,7 @@ private unsafe MsQuicApi()
138123
139124 internal static bool IsQuicSupported { get ; }
140125
141- static MsQuicApi ( )
126+ static unsafe MsQuicApi ( )
142127 {
143128 // MsQuicOpen will succeed even if the platform will not support it. It will then fail with unspecified
144129 // platform-specific errors in subsequent callbacks. For now, check for the minimum build we've tested it on.
@@ -150,14 +135,30 @@ static MsQuicApi()
150135
151136 // TODO: try to initialize TLS 1.3 in SslStream.
152137
153- try
154- {
155- Api = new MsQuicApi ( ) ;
156- IsQuicSupported = true ;
157- }
158- catch ( NotSupportedException )
138+ // TODO: Consider updating all of these delegates to instead use function pointers.
139+
140+ if ( NativeLibrary . TryLoad ( Interop . Libraries . MsQuic , out IntPtr msQuicHandle ) )
159141 {
160- IsQuicSupported = false ;
142+ try
143+ {
144+ if ( NativeLibrary . TryGetExport ( msQuicHandle , "MsQuicOpen" , out IntPtr msQuicOpenAddress ) )
145+ {
146+ MsQuicNativeMethods . MsQuicOpenDelegate msQuicOpen = Marshal . GetDelegateForFunctionPointer < MsQuicNativeMethods . MsQuicOpenDelegate > ( msQuicOpenAddress ) ;
147+ uint status = msQuicOpen ( out MsQuicNativeMethods . NativeApi * registration ) ;
148+ if ( MsQuicStatusHelper . SuccessfulStatusCode ( status ) )
149+ {
150+ IsQuicSupported = true ;
151+ Api = new MsQuicApi ( registration ) ;
152+ }
153+ }
154+ }
155+ finally
156+ {
157+ if ( ! IsQuicSupported )
158+ {
159+ NativeLibrary . Free ( msQuicHandle ) ;
160+ }
161+ }
161162 }
162163 }
163164
0 commit comments