Skip to content

Commit

Permalink
Use explicit full-path for loading MsQuic.dll on Windows (#109963)
Browse files Browse the repository at this point in the history
* Use explicit full-path for loading MsQuic.dll on Windows

* Suppress IL3000 in MsQuicApi constructor

A call to `Assembly.Location` was added in a recent fix. It has `IL30000` suppressed via `#pragma warning disable`, but that only applies to the compilation of the library itself. Consumers will hit it when doing something like publishing their app as NativeAOT.

This change adds an `[UnconditionalSuppressMessage]` to the `MsQuicApi` static constructor such that `IL30000` should also be suppressed for apps consuming the runtime.

This was caught in an aspnetcore deps flow PR coming from runtime.

---------

Co-authored-by: Elinor Fung <elfung@microsoft.com>
  • Loading branch information
rzikm and elinor-fung authored Nov 20, 2024
1 parent 0d62887 commit 6be24fd
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ private MsQuicApi(QUIC_API_TABLE* apiTable)
internal static bool Tls13ClientMayBeDisabled { get; }

#pragma warning disable CA1810 // Initialize all static fields in 'MsQuicApi' when those fields are declared and remove the explicit static constructor
[UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file",
Justification = "The code handles the Assembly.Location being null/empty by falling back to AppContext.BaseDirectory")]
static MsQuicApi()
{
bool loaded = false;
Expand All @@ -89,8 +91,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
{
Expand Down

0 comments on commit 6be24fd

Please sign in to comment.