RepoDB icon indicating copy to clipboard operation
RepoDB copied to clipboard

Could not load file or assembly 'RepoDb.PostgreSql.dll'

Open Swoorup opened this issue 2 years ago • 2 comments

Bug Description

When launching a dotnet executable using dotnet run -- -c appsettings.json from a project directory, it no longer finds RepoDb.PostgreSql

Exception Message:

System.AggregateException: One or more errors occurred. (Could not load file or assembly '/Users/swoorup.joshi/work/gp-suburb-api/src/WebApi/RepoDb.PostgreSql.dll'. The system cannot find the file specified.
)
 ---> System.IO.FileNotFoundException: Could not load file or assembly '/Users/swoorup.joshi/work/gp-suburb-api/src/WebApi/RepoDb.PostgreSql.dll'. The system cannot find the file specified.

File name: '/Users/swoorup.joshi/work/gp-suburb-api/src/WebApi/RepoDb.PostgreSql.dll'
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at RepoDb.Reflection.Compiler.GetSetToUnknownNpgsqlParameterExpression(ParameterExpression commandParameterExpression, DbField dbField)
   at RepoDb.Reflection.Compiler.GetDataEntityParameterAssignmentExpression(ParameterExpression commandParameterExpression, Int32 entityIndex, Expression entityExpression, ParameterExpression propertyExpression, DbField dbField, ClassProperty classProperty, ParameterDirection direction, IDbSetting dbSetting)
   at RepoDb.Reflection.Compiler.GetPropertyFieldExpression(ParameterExpression commandParameterExpression, Expression entityExpression, FieldDirection fieldDirection, Int32 entityIndex, IDbSetting dbSetting)
   at RepoDb.Reflection.Compiler.GetIndexDbParameterSetterExpression(Type entityType, ParameterExpression commandParameterExpression, Expression entitiesParameterExpression, IEnumerable`1 fieldDirections, Int32 entityIndex, IDbSetting dbSetting)
   at RepoDb.Reflection.Compiler.CompileDataEntityListDbParameterSetter(Type entityType, IEnumerable`1 inputFields, IEnumerable`1 outputFields, Int32 batchSize, IDbSetting dbSetting)
   at RepoDb.Reflection.FunctionFactory.CompileDataEntityListDbParameterSetter(Type entityType, IEnumerable`1 inputFields, IEnumerable`1 outputFields, Int32 batchSize, IDbSetting dbSetting)
   at RepoDb.FunctionCache.DataEntityListDbParameterSetterCache.Get(Type entityType, String cacheKey, IEnumerable`1 inputFields, IEnumerable`1 outputFields, Int32 batchSize, IDbSetting dbSetting)
   at RepoDb.FunctionCache.GetDataEntityListDbParameterSetterCompiledFunction(Type entityType, String cacheKey, IEnumerable`1 inputFields, IEnumerable`1 outputFields, Int32 batchSize, IDbSetting dbSetting)
   at RepoDb.Contexts.Providers.MergeAllExecutionContextProvider.CreateInternal(Type entityType, IDbConnection connection, IEnumerable`1 entities, IEnumerable`1 dbFields, String tableName, IEnumerable`1 qualifiers, Int32 batchSize, IEnumerable`1 fields, String commandText)
   at RepoDb.Contexts.Providers.MergeAllExecutionContextProvider.CreateAsync(Type entityType, IDbConnection connection, IEnumerable`1 entities, String tableName, IEnumerable`1 qualifiers, Int32 batchSize, IEnumerable`1 fields, String hints, IDbTransaction transaction, IStatementBuilder statementBuilder, CancellationToken cancellationToken)
   at RepoDb.DbConnectionExtension.MergeAllAsyncInternalBase[TEntity](IDbConnection connection, String tableName, IEnumerable`1 entities, IEnumerable`1 qualifiers, Int32 batchSize, IEnumerable`1 fields, String hints, Nullable`1 commandTimeout, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder, CancellationToken cancellationToken)
   at RepoDb.DbConnectionExte

Library Version:

Example: RepoDb 1.12.10 and RepoDb.PostgreSql 1.1.5

Swoorup avatar Mar 04 '22 03:03 Swoorup

@Swoorup I think, I have made an explanation to this in the gitter chat in the past. Is this issue still necessary to be fixed? Please advise as we are now moving towards the new release.

mikependon avatar Aug 31 '22 07:08 mikependon

@mikependon Is it possible to add ${workingDirectory}/bin/${Configuration}/${targetFramework}/ to the search path of the dll?

I think it's common to launch projects from directory using dotnet run which contains the project csproj or fsproj file using dotnet, but dlls are in those bin/{debug/release} folder.

Swoorup avatar Sep 05 '22 04:09 Swoorup

@mikependon can you, please describe the issue also here? I think we still need the fix as we cannot debug the project with VisualStudio right now..

jbrezina avatar Jan 16 '23 09:01 jbrezina

@jbrezina sure.

@mikependon can you, please describe the issue also here? I think we still need the fix as we cannot debug the project with VisualStudio right now..

The main reason to this is the fact that RepoDb.dll is loading the RepoDb.PostgreSql.dll on-the-fly before generating an AOT compilation of the code itself. That process is failing if the RepoDb.PostgreSql.dll is not placed where the RepoDb.dll is located. This only happens when you manually run the solution, not when you are running the built/compiled solution.

Why on-the-fly loading? It is because that RepoDb.dll is a core and is the base of everything, whilst RepoDb.PostgreSql.dll is just an extension and is not directly referenced by the core (it is the other way around).

mikependon avatar Jan 16 '23 14:01 mikependon

I think the source directory is not read correctly as the RepoDb.PostgreSql.dll library is trying to be loaded from the project directory and not the bin directory. I can see both libraries are located in the same (bin) directory.

edit: I can see that there is no path given to the library, could you use something like AppDomain.BaseDirectory to build the path to the library? Or something like AsseblyLoadContext? Or any other method to get the exact location of the file? By default, the working directory in VisualStudio is set to the project directory, not the binaries directory so it is not possible to load the library while debugging the project within VisualStudio.

jbrezina avatar Jan 16 '23 15:01 jbrezina

this is quite annoying as it breaks dotnet watch

anpin avatar Mar 10 '23 10:03 anpin

In the fixes, we have completely eliminated the dynamic loading of the extended library. To resolved the issue, we have extended the IDbHelper object with the DynamicHandler method and used that as a passthrough method when passing a notification between the core library and extended library.

The fixes to this will be available in the next version of the library >= v1.13.1-alpha2.

mikependon avatar Mar 13 '23 21:03 mikependon