Skip to content

Commit d7d2696

Browse files
authored
[Breaking change]: Specifying DllImportSearchPath.AssemblyDirectory by itself only searches in the assembly directory (#46073)
* [Breaking change]: Specifying DllImportSearchPath.AssemblyDirectory by itself only searches in the assembly directory Fixes #45911 * Xrefs
1 parent 95b5932 commit d7d2696

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

docs/core/compatibility/10.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
4848
| [X509Certificate and PublicKey key parameters can be null](cryptography/10.0/x509-publickey-null.md) | Behavioral/source incompatible change | Preview 3 |
4949
| [Environment variable renamed to DOTNET_OPENSSL_VERSION_OVERRIDE](cryptography/10.0/version-override.md) | Behavioral change | Preview 1 |
5050

51+
## Interop
52+
53+
| Title | Type of change | Introduced version |
54+
|------------------------------------------------------------------------------------------------------------------------------------|-------------------|--------------------|
55+
| [Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory](interop/10.0/search-assembly-directory.md) | Behavioral change | Preview 5 |
56+
5157
## Networking
5258

5359
| Title | Type of change | Introduced version |
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: "Breaking change - Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory"
3+
description: "Learn about the breaking change in .NET 10 Preview 5 where specifying DllImportSearchPath.AssemblyDirectory as the only search flag restricts the search to the assembly directory."
4+
ms.date: 5/9/2025
5+
ai-usage: ai-assisted
6+
ms.custom: https://github.com/dotnet/docs/issues/45911
7+
---
8+
9+
# Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory
10+
11+
Starting in .NET 10, if you specify <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> as the only search flag, the runtime searches exclusively in the assembly directory. This change affects the behavior of P/Invokes and the <xref:System.Runtime.InteropServices.NativeLibrary> class.
12+
13+
## Version introduced
14+
15+
.NET 10 Preview 5
16+
17+
## Previous behavior
18+
19+
When <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> was specified as the only search flag, the runtime searched the assembly directory first. If the library was not found, it fell back to the operating system's default library search behavior.
20+
21+
Example:
22+
23+
```csharp
24+
[DllImport("example.dll", DllImportSearchPath = DllImportSearchPath.AssemblyDirectory)]
25+
public static extern void ExampleMethod();
26+
```
27+
28+
In this case, the runtime would search the assembly directory and then fall back to the OS search paths.
29+
30+
## New behavior
31+
32+
When <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> is specified as the only search flag, the runtime searches only in the assembly directory. It does not fall back to the operating system's default library search behavior.
33+
34+
The previous code example would now only search the assembly directory for *example.dll*. If the library is not found there, a <xref:System.DllNotFoundException> will be thrown.
35+
36+
## Type of breaking change
37+
38+
This is a [behavioral change](../../categories.md#behavioral-change).
39+
40+
## Reason for change
41+
42+
The fallback behavior when specifying <xref:System.Runtime.InteropServices.DllImportSearchPath.AssemblyDirectory?displayProperty=nameWithType> caused confusion and was inconsistent with the design of search flags. This change ensures clarity and consistency in behavior.
43+
44+
## Recommended action
45+
46+
If fallback behavior is required, avoid specifying an explicit <xref:System.Runtime.InteropServices.DllImportSearchPath>. By default, when no flags are specified, the runtime searches the assembly directory and then falls back to the operating system's default library search behavior.
47+
48+
Example:
49+
50+
```csharp
51+
[DllImport("example.dll")]
52+
public static extern void ExampleMethod();
53+
```
54+
55+
## Affected APIs
56+
57+
- P/Invokes using <xref:System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute>
58+
- <xref:System.Runtime.InteropServices.NativeLibrary.Load*?displayProperty=fullName>
59+
- <xref:System.Runtime.InteropServices.NativeLibrary.TryLoad*?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ items:
4242
items:
4343
- name: Environment variable renamed to DOTNET_ICU_VERSION_OVERRIDE
4444
href: globalization/10.0/version-override.md
45+
- name: Interop
46+
items:
47+
- name: Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory
48+
href: interop/10.0/search-assembly-directory.md
4549
- name: Networking
4650
items:
4751
- name: Streaming HTTP responses enabled by default in browser HTTP clients
@@ -1808,6 +1812,10 @@ items:
18081812
href: globalization.md
18091813
- name: Interop
18101814
items:
1815+
- name: .NET 10
1816+
items:
1817+
- name: Specifying DllImportSearchPath.AssemblyDirectory only searches the assembly directory
1818+
href: interop/10.0/search-assembly-directory.md
18111819
- name: .NET 9
18121820
items:
18131821
- name: CET supported by default

0 commit comments

Comments
 (0)