diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs index e07a99b13f7c70..4148f4ac1cc43b 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicApi.cs @@ -83,8 +83,23 @@ static MsQuicApi() if (OperatingSystem.IsWindows()) { - // Windows ships msquic in the assembly directory. - loaded = NativeLibrary.TryLoad(Interop.Libraries.MsQuic, typeof(MsQuicApi).Assembly, DllImportSearchPath.AssemblyDirectory, out msQuicHandle); +#pragma warning disable IL3000 // Avoid accessing Assembly file path when publishing as a single file + // Windows ships msquic in the assembly directory next to System.Net.Quic, so load that. + // For single-file deployments, the assembly location is an empty string so we fall back + // to AppContext.BaseDirectory which is the directory containing the single-file executable. + string path = typeof(MsQuicApi).Assembly.Location is string assemblyLocation && !string.IsNullOrEmpty(assemblyLocation) + ? System.IO.Path.GetDirectoryName(assemblyLocation)! + : AppContext.BaseDirectory; +#pragma warning restore IL3000 + + path = System.IO.Path.Combine(path, Interop.Libraries.MsQuic); + + if (NetEventSource.Log.IsEnabled()) + { + NetEventSource.Info(null, $"Attempting to load MsQuic from {path}"); + } + + loaded = NativeLibrary.TryLoad(path, typeof(MsQuicApi).Assembly, DllImportSearchPath.LegacyBehavior, out msQuicHandle); } else { @@ -154,7 +169,7 @@ static MsQuicApi() if (version < s_minMsQuicVersion) { - NotSupportedReason = $"Incompatible MsQuic library version '{version}', expecting higher than '{s_minMsQuicVersion}'."; + NotSupportedReason = $"Incompatible MsQuic library version '{version}', expecting higher than '{s_minMsQuicVersion}'."; if (NetEventSource.Log.IsEnabled()) { NetEventSource.Info(null, NotSupportedReason); @@ -178,7 +193,7 @@ static MsQuicApi() // Implies windows platform, check TLS1.3 availability if (!IsWindowsVersionSupported()) { - NotSupportedReason = $"Current Windows version ({Environment.OSVersion}) is not supported by QUIC. Minimal supported version is {s_minWindowsVersion}."; + NotSupportedReason = $"Current Windows version ({Environment.OSVersion}) is not supported by QUIC. Minimal supported version is {s_minWindowsVersion}."; if (NetEventSource.Log.IsEnabled()) { NetEventSource.Info(null, NotSupportedReason);