.Net compiler resolve package dependencies

46 Views Asked by At

I am writing an app in .NET 6.0. It allows the end user to load an arbitrary assembly using Assembly.LoadFrom. Then it uses reflection to call its methods. THAT IS ALL. The challenge is: I have no way to predict what assembly the user is going to load and what dependencies that assembly has. The possibilities are infinite.

I have a project which contains a simple user interface project and a simple "DataAccess" class library project which reads employees and teams from a SQL Server database. This DataAccess.dll is used to test my code. The DataAccess.csproj file contains a package reference PackageReference Include=Microsoft.EntityFrameworkCore.SqlServer Version="7.0.16".

When my code tried to invoke methods in the DataAccess.dll using reflection, the ResolveAssembly event was fired many times, asking me to load all the dependencies. When one of the events was fired, the ResolveEventArgs.Name was "Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5". It is one of the dependencies of Microsoft.EntityFrameworkCore.SqlServer.

However, when my code in the ResolveAssembly event handler searches for Microsoft.Data.SqlClient.dll under the C:\Users[username].nuget\packages\microsoft.data.sqlclient folder, there isn't a 5.0.0.0 subfolder. Instead, there are three folders 5.0.1, 5.0.2 and 5.1.4.

Which one shall I choose? THIS IS THE PROBLEM THAT I NEED YOUR HELP.

The "DataAccess.deps.json" file contains the answer: Microsoft.Data.SqlClient/5.1.4:

"dependencies": [
...
],
"runtime": [
"lib/net6.0/Microsoft.Data.SqlClient.dll": [
"assemblyVersion": "5.0.0.0",
"fileVersion": "5.14.24005.1"
]
]

"5.1.4" is actually the package version, while "5.0.0.0" is the assemblyVersion. But I can't use this file because not all .NET core projects have a deps.json file. Since the end users may use my app to load any arbitrary assembly, I can't make this assumption.

Could someone tell me how I can solve this?

0

There are 0 best solutions below