Skip to content

Could not load type 'System.Data.Common.DbDataSource' from assembly 'Npgsql, Version=7.0.4.0 #2834

Closed as not planned

Description

@RicardoSimoesPFR

After upgrading to .net7 with packages:
Npgsql.EntityFrameworkCore.PostgresSQL 7.0.4
Microsoft.EntityFrameworkCore 7.0.5

Application is unable to launch with error:

System.TypeLoadException: Could not load type 'System.Data.Common.DbDataSource' from assembly 'Npgsql, Version=7.0.4.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension..ctor(NpgsqlOptionsExtension copyFrom) at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension.Clone() at Microsoft.EntityFrameworkCore.Infrastructure.RelationalOptionsExtension.WithConnectionString(String connectionString) at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension.WithConnectionString(String connectionString) at Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsBuilderExtensions.UseNpgsql(DbContextOptionsBuilder optionsBuilder, String connectionString, Action1 npgsqlOptionsAction)
at pfr_core.Configurations.ServiceConfigurations.<>c__DisplayClass0_0.b__2(DbContextOptionsBuilder options) in D:\a\pfr-core\pfr-core\pfr-core\Configurations\ServiceConfigurations.cs:line 104`

Exception thrown here (ServiceConfiguration class)

   serviceCollection.AddDbContext<PfrContext>(options =>
            options.UseNpgsql(configuration.GetConnectionString("pfr_db"),
                o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)));

Activity

  1. roji commented on Aug 14, 2023

    @roji
    Member

    Can you please post a minimal repro for this?

  2. RicardoSimoesPFR commented on Aug 16, 2023

    @RicardoSimoesPFR
    Author

    @roji apologies but I forgot to mention one very important thing:
    This error does not happen locally but rather when the application is being deployed to Azure app services. Both connect to the same database, in the same way, and before the upgrade to .net7.0, it was working correctly.

    I am completely unable to reproduce this error locally.
    The memory dumps from the remote server do not show anything wrong with the Npgsql assemblies.
    The enviroment is configured to accept .net7.0.

    Do you reckon this could be being cause by versioning mismatch? I've been trying to get around this issue and will post any updates.

  3. roji commented on Aug 16, 2023

    @roji
    Member

    Yeah, it does sound like some sort of version mismatch - or possibly you're uploading/running the wrong thing. DbDataSource is inside .NET itself since 7.0, but included inside Npgsql when targeting older TFMs, so that users be able to use batching even on older versions of .NET.

  4. JadynWong commented on Oct 14, 2023

    @JadynWong

    I can reproduce the problem.

    • Create new classlibary with netstandard2.0.
    • The classlibary reference npgsql with 7.0.6.
    • Add test code ad the classlibary
    public static class Test
    {
        public static void Run(string connectionString)
        {
            var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
            var dataSource = dataSourceBuilder.Build();
            dataSource.Dispose();
        }
    }
    • Create a console program with .net7 and reference the classlibary.
    • Add the execute test code section to Program.cs.
    // See https://aka.ms/new-console-template for more information
    using ClassLibrary1;
    
    var connectionString = "Host=localhost;Port=5432;Database=sk_demo;User ID=postgres;Password=xxxxxxx;";
    Test.Run(connectionString);
    • Run the console program.

    image

    Should it be solved in npgsql?

  5. roji commented on Oct 14, 2023

    @roji
    Member

    @JadynWong I can't reproduce the problem with the instructions above - see this branch.

    It's always, always better to simply submit a minimal, runnable repo that reproduces the bug, rather than code snippets and text describing the problem. That saves both of us lots of time.

  6. JadynWong commented on Oct 14, 2023

    @JadynWong

    Hi @roji

    I create a repo https://github.com/JadynWong/npgsql-Issue2834 to reproduce the problem. Oddly enough, it's almost identical to your branch code. I submitted the dotnet runtime environment information.

    I trt run it on windows 11 sandbox (Only installed dotnet-sdk 7.0.402 and Git-2.42.0.2-64-bit).
    screenshot

  7. JadynWong commented on Oct 17, 2023

    @JadynWong

    @JadynWong I can't reproduce the problem with the instructions above - see this branch.

    It's always, always better to simply submit a minimal, runnable repo that reproduces the bug, rather than code snippets and text describing the problem. That saves both of us lots of time.

    I noticed that ConsoleApp1 in your branch is <TargetFramework>net6.0</TargetFramework>. When it changes to <TargetFramework>net7.0</TargetFramework>, you should be able to reproduce the problem, you can also try running my repo.

  8. roji commented on Oct 17, 2023

    @roji
    Member

    @JadynWong thanks, you're right - after making ConsoleApp1 target net7.0, I can indeed see the error. I'm not a expert on nuget/assembly resolution, but it seems that the net7.0 assembly of Npgsql gets used (because ConsoleApp1 targets net7.0); this assembly doesn't contain DbDataSource because it's already in the runtime. But the library is built against the netstandard2.0 assembly of Npgsql which contains DbDataSource, and so it expects it there and crashes when it's not found.

    Which actual class library is being used here, is it feasible to simply have it multitarget net7.0 as well, so that when it's used from net7.0 it uses the right Npgsql version?

    Otherwise I can try to investigate what can be done, but realistically I'm not sure there'll be a good solution here. Note that we've made the decision to drop .NET Framework/Standard for Npgsql 9.0, so this problem will no longer exist in the future in any case...

  9. NinoFloris commented on Oct 17, 2023

    @NinoFloris
    Member

    It's basically this issue dotnet/roslyn#69678

    Maybe we could try this suggestion (but I'm not sure how it helps) dotnet/roslyn#69678 (comment)

  10. roji commented on Oct 17, 2023

    @roji
    Member

    @NinoFloris thanks for the link, makes sense. So this is basically about type-forwarding DbDataSource (and any other similar thing) for newer TFMs to the runtime version, right? This way if a netstandard2.0 that references Npgsql gets used in a net7.0 application, it will find DbDataSource in Npgsql (forwarded to the runtime one)?

    I guess this makes sense - since this problem will occur with net6.0 as well (DbDataSource was introduced in 7.0), we may want to do this even though we're dropping netstandard2.0 in 9.0...

  11. JadynWong commented on Oct 18, 2023

    @JadynWong

    Which actual class library is being used here, is it feasible to simply have it multitarget net7.0 as well, so that when it's used from net7.0 it uses the right Npgsql version?

    It is used in the postgres memory store implementation of semantic-kernel. This repository is currently prioritizes .netstandard 2.0.

    Otherwise I can try to investigate what can be done, but realistically I'm not sure there'll be a good solution here.

    I found that when NpgsqlDataSource is cast to IDisposable it executes fine, the exception is not thrown. It seems that it can find the right dependencies.
    microsoft/semantic-kernel#3177

    Note that we've made the decision to drop .NET Framework/Standard for Npgsql 9.0, so this problem will no longer exist in the future in any case...

    Yes, I noticed that.

  12. roji commented on Oct 28, 2024

    @roji
    Member

    Given that EF 6.0 and 7.0 are now out of support and 9.0 is dropping support for netstandard2.0, I'm going to go ahead and close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions