npgsql: Using Npgsql 5.0.7 via nuget on .NET 4.8 gives MissingMethodException in ChannelReader

Exception:

Method not found: 'System.Threading.Tasks.ValueTask`1<Boolean> System.Threading.Channels.ChannelReader`1.WaitToReadAsync(System.Threading.CancellationToken)'.

Stacktrace:

   at Npgsql.ConnectorPool.<>c__DisplayClass38_0.<<Rent>g__RentAsync|0>d.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at Npgsql.ConnectorPool.<>c__DisplayClass38_0.<Rent>g__RentAsync|0()
   at Npgsql.ConnectorPool.Rent(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass41_0.<<Open>g__OpenAsync|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlConnection.Open()
   at SD.LLBLGen.Pro.Gui.Controls.WizardPages.MetaDataRetrievalWizard_Step_ConnectionData.TestConnectionData(Boolean showSuccess) in C:\MyProjects\VS.NET Projects\LLBLGen Pro v5.9\Designer\Gui\Controls\WizardPages\MetaDataRetrievalWizard_Step_ConnectionData.cs:line 245

It’s in my driver, where I obtain the factory using NpgsqlFactory.Instance as I now directly reference the Nuget package. I compile against 4.8 (against 4.7.2 same issue). I copy all necessary files to the output folder, app using the driver is running on .net 4.8, I checked. All I do is obtaining a connection from the factory and then call Open.

I am using this route because Npgsql doesn’t ship an msi anymore so I have to reference the Npgsql package directly to make my users not have to fight where to get the files.

I have no idea what to do nor what I’m doing wrong. It looks like I have to reference another package somewhere but which one is a blur to me.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (22 by maintainers)

Most upvoted comments

OK, took a look.

After building everything successfully, running NpgsqlRepro.exe failed with:

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Looking at the actual System.Runtime.CompilerServices.Unsafe.dll deployed in Drivers\PostgreSQL, it had a version of 6.0.0, so I amended the binding redirects. When that didn’t work, I noticed that the <dependentAssembly> directives are outside of the <assemblyBinding> node - so I moved them in. At this point the program connected, but threw another runtime binding redirect exception for System.Text.Json, after I added that everything worked…

There already was a 6-month gap there, I’d like to make sure this gets closed this time 😉

Great, happy to lay this to rest 😃 Binding redirects are a small piece of hell, but at least it’s possible to make stuff work, with enough pain.

@roji alright! 👍 😃 Glad it’s sorted. Sorry I had the binding redirects at the wrong spot… 😦 So the end result is: it can work, with enough binding redirects.

v6 works, at least you don’t get the missing method exception but then you get the system.buffers crap.

Loading postgresql driver assembly...
Done!
Instantiating driver instance...
Done!
Npgsql version: Npgsql, Version=6.0.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7
Obtaining connection...
Done!
Opening connection...
Exception: System.IO.FileLoadException
Message: Could not load file or assembly 'System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Stacktrace:
   at Npgsql.Internal.NpgsqlReadBuffer..ctor(NpgsqlConnector connector, Stream stream, Socket socket, Int32 size, Encoding textEncoding, Encoding relaxedTextEncoding, Boolean usePool)
   at Npgsql.Internal.NpgsqlConnector.<RawOpen>d__198.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.Internal.NpgsqlConnector.<<Open>g__OpenCore|193_1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.Internal.NpgsqlConnector.<Open>d__193.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.ConnectorPool.<OpenNewConnector>d__30.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.ConnectorPool.<<Get>g__RentAsync|27_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlConnection.<<Open>g__OpenAsync|45_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlConnection.Open()
   at NpgsqlRepro.Program.Main(String[] args) in C:\Users\frans.SD\Downloads\Npgsql_3886Repro\NpgsqlRepro\Program.cs:line 44

------------------
Inner exception:

Press any key to continue . . .

Will now as a final test see if the redirects fix it, just for completeness

Npgsql version: Npgsql, Version=5.0.7.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7 So that’s not v6. Will check with that.

I also got this error message with Npgsql 5.0.7 on .NET 4.8. In my case I had an old version installed in GAC (Npgsql 4.0, using the msi) which disturbed 5.0.x. Uninstalling the old version helped in my case.

@roji

@FransBouma sorry for not writing earlier on this… It would definitely be a shame for you to stay back on 4.0.

Any chance you could put together a minimal repro without LLBLGen Pro (just a console program using Assembly.LoadFrom)? I can try to investigate from there…

Well, I managed to repro it with a console app and 2 assemblies 😃 I’ve attached the repro below.

  • NpgsqlRepro is the console app which you should run. It has in its Bin\Debug folder a Drivers\PostgreSQL folder. This is required as the driver dll will end up there. It also contains a probing path declaration in its app.config file so fusion knows where to find dlls that are referenced indirectly by dlls in one of these folders.
  • DriverBase, this is the base library for all drivers
  • PostgreSQLDriver, this is the postgresql driver which references Npgsql. It derives a class from a class in DriverBase. It has a couple of post build commands to copy the dlls it uses to the Bin\Debug\Drivers\Postgresql folder

NpgsqlRepro now loads as we do in our main app, but here I use hardcoded strings, the PostgreSQL driver dll, gets the factory, creates a connection using the factory and then tries to open the connection. The connection string is specified but it’s not used: it crashes before that inside the open method of the NpgsqlConnection… There’s little code so it’s easy to follow.

Hopefully it sheds some light what’s going on and what to do to fix it (wherever that is, my code, your code, some config file… )

Npgsql_3886Repro.zip