You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Could not load type 'System.Data.Common.DbDataSource' from assembly 'Npgsql, Version=7.0.4.0 #2834
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)));
@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.
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.
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 informationusingClassLibrary1;varconnectionString="Host=localhost;Port=5432;Database=sk_demo;User ID=postgres;Password=xxxxxxx;";Test.Run(connectionString);
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.
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.
@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...
@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...
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...
Activity
roji commentedon Aug 14, 2023
Can you please post a minimal repro for this?
RicardoSimoesPFR commentedon Aug 16, 2023
@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.
roji commentedon Aug 16, 2023
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.
JadynWong commentedon Oct 14, 2023
I can reproduce the problem.
netstandard2.0.npgsqlwith7.0.6.Program.cs.Should it be solved in npgsql?
roji commentedon Oct 14, 2023
@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.
JadynWong commentedon Oct 14, 2023
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).

JadynWong commentedon Oct 17, 2023
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.roji commentedon Oct 17, 2023
@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...
NinoFloris commentedon Oct 17, 2023
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)
roji commentedon Oct 17, 2023
@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...
JadynWong commentedon Oct 18, 2023
It is used in the postgres memory store implementation of semantic-kernel. This repository is currently prioritizes .netstandard 2.0.
I found that when
NpgsqlDataSourceis cast toIDisposableit executes fine, the exception is not thrown. It seems that it can find the right dependencies.microsoft/semantic-kernel#3177
Yes, I noticed that.
roji commentedon Oct 28, 2024
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.