Skip to content

Commit 18ddd57

Browse files
authored
Avoid multiple exceptions at startup from MsQuic support tests (dotnet#49973)
1 parent 561274a commit 18ddd57

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Internal/MsQuicApi.cs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/libraries/System.Net.Quic/src/System/Net/Quic/Interop/Interop.MsQuic.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/libraries/System.Net.Quic/src/System/Net/Quic/Interop/MsQuicNativeMethods.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ internal struct NativeApi
5454
internal IntPtr DatagramSend;
5555
}
5656

57+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
58+
internal delegate uint MsQuicOpenDelegate(
59+
out NativeApi* registration);
60+
5761
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
5862
internal delegate uint SetContextDelegate(
5963
IntPtr handle,

0 commit comments

Comments
 (0)