Open
Conversation
Contributor
|
Is it possible to add a test so we don't regress again? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses two issues related to the newly added
DllImportResolverfor.NETnative library loading:Fix IL3000 Warning during Native AOT / Single-File Publish
When publishing projects that reference
Microsoft.ML.OnnxRuntimeas a single file or using Native AOT in .NET 9, the compiler reports anIL3000warning/error becauseDllImportResolveraccessesAssembly.Location. In these deployment models,Assembly.Locationalways returns an empty string or throws.Since
DllImportResolveralready correctly handles the empty string failure and falls back toAppContext.BaseDirectory(which is fully supported), this PR adds the[UnconditionalSuppressMessage]attribute to suppress the build warning statically.Fix
TypeInitializationExceptioninNativeMethodsStatic ConstructorUsers reported a
System.TypeInitializationException: The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception.when initializing the ONNX Runtime environment.This occurs because the
DllImportResolver(registered in the static constructor) is invoked on the first P/Invoke (OrtGetApiBase). If any API within the resolver throws an unhandled exception (for instance,AppContext.BaseDirectorythrowingAppDomainUnloadedExceptionin sandboxed AppDomains orEnvironment.GetEnvironmentVariablethrowingSecurityException), the exception bubbles up and crashes the application with a type initialization failure.This PR wraps the
DllImportResolverlogic in atry-catchblock (specifically handlingAppContext.BaseDirectoryedge cases) so that any resolution failure safely swallows the error and falls back toIntPtr.Zero, allowing the default .NET Platform Invoke mechanism to take over and throw a standardDllNotFoundExceptioninstead of a fatal type initialization crash.A unit test (
TestDllImportResolverDoesNotThrow) has been added toOrtEnvTests.csto verify thatDllImportResolversuccessfully swallows internal exceptions without crashing the initialization process.Motivation and Context
These changes ensure that .NET developers can safely compile Native AOT/Single-File applications without build errors and prevent hard application crashes in environments with restricted permissions.