How Does .NET Resolve Dependencies When a netstandard2.0 Library References a net8.0 Library? #120438
-
I have the following class libraries in my application: Library A: targets both netstandard2.0 and net8.0 I expected the netstandard2.0 version of Library A to be restored (since Library B targets only netstandard2.0), but instead, the net8.0 version of Library A is restored. It appears that because the entry point (the executable) targets net8.0, all dependencies are resolved for net8.0, even if they are not direct dependencies of the executable. Despite this, the application runs fine at runtime with no errors so far. My question: Any insights into how .NET resolves these dependencies and why this works would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Yes, all dependencies used at runtime are picked by the executable, not transitive dependencies.
Target frameworks don't exist in the view of CLR. They are resolved at SDK and NuGet level only.
It always references B(netstandard2.0). It's just B(net8.0) being able to satisfy the reference. In .NET, the referenced assembly used at compile time and the runtime assembly used at runtime are very different. When a library was compiled for net6.0, it needs to reference net6.0 version of the BCL. Then at runtime, net8.0 version of BCL will be used to satisfy the dependency. |
Beta Was this translation helpful? Give feedback.
-
I think the explanation given by @huoyaoyuan makes sense. Thank you! |
Beta Was this translation helpful? Give feedback.
Well it's a typo about library name.
B(netstandard2.0) references A(netstandard2.0), and A(net8.0) is provided. It simply satisfy the reference to A(netstandard2.0). Just like how BCL(net8.0) satisfies reference to BCL(net6.0). Target framework is not a part of assembly identity.