borisdj / EFCore.BulkExtensions

Entity Framework EF Core efcore Bulk Batch Extensions with BulkCopy in .Net for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, MySQL, SQLite
https://codis.tech/efcorebulk
Other
3.61k stars 578 forks source link

a bit of a problem when using library with net 8.0 #1402

Open nafberger opened 6 months ago

nafberger commented 6 months ago

hi borris, we updated to .net 8 and since then we have a problem.

nafberger commented 6 months ago

like this .

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field. at RuntimeType[] System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at RuntimeType[] System.Reflection.RuntimeModule.GetDefinedTypes() at IEnumerable System.Reflection.RuntimeAssembly.get_DefinedTypes() at IImplementationTypeSelector Scrutor.TypeSourceSelector.InternalFromAssemblies(IEnumerable assemblies)+(Assembly asm) => { } [0] at bool System.Linq.Enumerable+SelectManySingleSelectorIterator<TSource, TResult>.MoveNext() at bool System.Linq.Enumerable+WhereSelectEnumerableIterator<TSource, TResult>.MoveNext() at void Scrutor.LifetimeSelector.Scrutor.ISelector.Populate(IServiceCollection services, RegistrationStrategy strategy) at void Scrutor.ServiceTypeSelector.Scrutor.ISelector.Populate(IServiceCollection services, RegistrationStrategy registrationStrategy) at void Scrutor.ImplementationTypeSelector.Scrutor.ISelector.Populate(IServiceCollection services, RegistrationStrategy registrationStrategy) at void Scrutor.TypeSourceSelector.Populate(IServiceCollection services, RegistrationStrategy registrationStrategy) at IServiceCollection Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.Populate(IServiceCollection services, ISelector selector, RegistrationStrategy registrationStrategy) at IServiceCollection Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.Scan(IServiceCollection services, Action action) at IServiceCollection WebApi.Extensions.AddServicesExtensions.AutoRegisterScoped(IServiceCollection services) in C:/Users/NaftalyWeinberger/source/repos/inventory-fba/WebApi/Extensions/AddServicesExtensions.cs:line 83 at IServiceCollection WebApi.Extensions.AddServicesExtensions.AddServices(IServiceCollection services) in C:/Users/NaftalyWeinberger/source/repos/inventory-fba/WebApi/Extensions/AddServicesExtensions.cs:line 35 at void WebApi.Startup.ConfigureServices(IServiceCollection services) in C:/Users/NaftalyWeinberger/source/repos/inventory-fba/WebApi/Startup.cs:line 82 at object RuntimeMethodHandle.InvokeMethod(object target, Void* arguments, Signature sig, bool isConstructor) at object System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(object obj, IntPtr args) at object System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(object obj, Span copyOfArgs, BindingFlags invokeAttr) at object System.Reflection.MethodBaseInvoker.InvokeWithOneArg(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) at object System.Reflection.RuntimeMethodInfo.Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) at object Microsoft.AspNetCore.Hosting.MethodInfoExtensions.InvokeWithoutWrappingExceptions(MethodInfo methodInfo, object obj, object[] parameters) at IServiceProvider Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(object instance, IServiceCollection services) at IServiceProvider Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(object instance, IServiceCollection services)+Startup(IServiceCollection serviceCollection) at IServiceProvider Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(object instance, IServiceCollection services) at Func<IServiceCollection, IServiceProvider> Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Build(object instance)+(IServiceCollection services) => { } at void Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services, object instance) at IWebHostBuilder Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType)+(HostBuilderContext context, IServiceCollection services) => { } at void Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider() at IHost Microsoft.Extensions.Hosting.HostBuilder.Build() at async Task WebApi.Program.Main(string[] args) in C:/Users/NaftalyWeinberger/source/repos/inventory-fba/WebApi/Program.cs:line 23 System.TypeLoadException: Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.

nafberger commented 6 months ago

it maybe has to do with scrutor whith aseemply scaning.

I tried to download your library and I ripped out microsoft sql data client completely, but it didn't help.

we use PostgreSQL. can we please just use the PostgreSQL part without anything that touches the sqlclient library. or do u have any other solution. any help will be greatly appreciated

nafberger commented 6 months ago

it seems like its the same issue as here. https://github.com/dotnet/SqlClient/issues/1930

borisdj commented 6 months ago

Try the latest version 8.0.2, it uses Microsoft.Data.SqlClient 5.1.5 Also instead of main nuget you can use specific one per provider, so EFCore.BulkExtensions.PostgreSql

If the problem remains can you write a test where the issue would be reproducible.

As for the removing SqlClient client it could be done in principle, but you would need to uninstall that nuget from the source, excluce Batch folder with all files (not need for Bulk ops), also remove SqlBulkCopyOptions and in Config replace it with just KeepIdentity.

nafberger commented 6 months ago

thanks @borisdj, we solved it by excluding Microsoft.Data.SqlClient from our assembly scanning code in which we use scrutor for DI.

I can provide code example if someone needs help.

btw, I tried 5.1.5 it didn't help, I even tried removing Microsoft.Data.SqlClient 5.1.5 completely from the solution but it didn't help because it comes in hidden, as a transitive package, anyway by excluding it the issue is solved. thanks for your reply.

you can close the issue if u want,

ivansager commented 6 months ago

@nafberger so did you exclude the assmbly like this. I am running into the same issue. var dataTypes = AppDomain.CurrentDomain.GetAssemblies().Where(x=>x.FullName != "Microsoft.Data.SqlClient")

nafberger commented 6 months ago
public static readonly IReadOnlyCollection<string> ExcludedAssemblies = [ "Microsoft.Data.SqlClient" ];

private static ILifetimeSelector ApplicationDependenciesTypeSelector<TService>(ITypeSourceSelector scan) where TService : class
{
    return scan
        .FromApplicationDependencies(assembly => !ExcludedAssemblies.Contains(assembly.GetName().Name))
        .AddClasses(classes => classes
            .AssignableTo<TService>()
            .WithoutAttribute<SkipAutoRegistrationAttribute>()
        )
        .UsingRegistrationStrategy(RegistrationStrategy.Skip)
        .AsSelfWithInterfaces();
}
nafberger commented 6 months ago

this is our code, hope that helps

ivansager commented 6 months ago

Thank you @nafberger this is most helpful. Be well