(Bug?) Exception from Linqpad, running to completion in Visual Studio/Command Prompt
Hello, I don´t know if this is the correct place to post a (possible) bug. Please kindly redirect me to the right place if there is a better place to report it.
If I run this little program from Visual Studio (simplified from the original just to show the problem):
using Dapper; using System.Data.OleDb; namespace Helper { public class Program { public static void Main(string[] args) { try { var path = "D:\\Helper\\bin\\Debug\\net9.0\\TestDB.accdb"; Console.WriteLine($"Database: {path}"); Console.WriteLine($"Row count: {AccessHelper.Query(path, "SELECT * FROM [Table]").Count()}"); } catch (Exception E) { Console.WriteLine($"Exception: {E.ToString()}"); } } } public static class AccessHelper { public static IEnumerable<dynamic> Query(string dbPath, string query) { IEnumerable<dynamic> res; using (var con = new OleDbConnection( @$"Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=false;Data Source={dbPath}")) { con.Open(); res = con.Query(query); con.Close(); } return res; } } }
This is (Visual Studio/CMD) output
Database: D:\Helper\bin\Debug\net9.0\TestDB.accdb Row count: 2
Given this is Linqpad file
<Query Kind="Program"> <Reference Relative="bin\Debug\net9.0\Helper.dll">D:\Helper\bin\Debug\net9.0\Helper.dll</Reference> <Namespace>Helper</Namespace> </Query> void Main() { Program.Main([]); } // You can define other methods, fields, classes and namespaces here
This is Linqpad's output
Database: D:\Helper\bin\Debug\net9.0\TestDB.accdb Exception: System.TypeInitializationException: The type initializer for 'System.Data.OleDb.OleDbConnection' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.OleDb.OleDbConnectionFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.ProviderBase.DbConnectionPoolCountersNoCounters' threw an exception. ---> System.TypeInitializationException: The type initializer for 'CreationData' threw an exception. ---> System.PlatformNotSupportedException: Performance Counters are not supported on this platform. at System.Diagnostics.CounterCreationData..ctor(String counterName, String counterHelp, PerformanceCounterType counterType) at System.Data.ProviderBase.DbConnectionPoolCounters.CreationData..cctor() --- End of inner exception stack trace --- at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName) at System.Data.ProviderBase.DbConnectionPoolCounters..ctor() at System.Data.ProviderBase.DbConnectionPoolCountersNoCounters..ctor() at System.Data.ProviderBase.DbConnectionPoolCountersNoCounters..cctor() --- End of inner exception stack trace --- at System.Data.ProviderBase.DbConnectionFactory..ctor() at System.Data.OleDb.OleDbConnectionFactory..ctor() at System.Data.OleDb.OleDbConnectionFactory..cctor() --- End of inner exception stack trace --- at System.Data.OleDb.OleDbConnection..cctor() --- End of inner exception stack trace --- at System.Data.OleDb.OleDbConnection.get_ConnectionFactory() at System.Data.OleDb.OleDbConnection.ConnectionString_Set(DbConnectionPoolKey key) at System.Data.OleDb.OleDbConnection.ConnectionString_Set(String value) at System.Data.OleDb.OleDbConnection.set_ConnectionString(String value) at System.Data.OleDb.OleDbConnection..ctor(String connectionString) at Helper.AccessHelper.Query(String dbPath, String query) at Helper.Program.Main(String[] args)
On a related note, Linq To DB also started to fail in the same way within Linqpad (it used to work), using the same Access driver, and same connection string. Maybe it's related to a recent update to Visual Studio 17.13. But the funny thing is, from within VS it does works, and is also works if called directly (from CMD for instance), and LinqPad is referencing the same exact code and running it as is (no new code, just using the same .dll that works externally), so it seems Linqpad is doing something under the hood differently.
Any ideas?
(All relevant files attached, even binaries as generated by Visual Studio)
Visual Studio version data
Microsoft Visual Studio Enterprise 2022 Versión 17.13.0 VisualStudio.17.Release/17.13.0+35806.99 Microsoft .NET Framework Versión 4.8.09032 Versión instalada: Enterprise Herramientas de arquitectura y modelado 00482-10000-00261-AA121 Diagramas y herramientas de análisis de Microsoft Architecture Visual C++ 2022 00482-10000-00261-AA121 Microsoft Visual C++ 2022 ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools Administrador de paquetes NuGet 6.13.1 Administrador de paquetes NuGet de Visual Studio. Para obtener más información acerca de NuGet, visite https://docs.nuget.org/ ASA Service Provider 1.0 ASP.NET and Web Tools 17.13.124.35287 ASP.NET and Web Tools Avalonia Toolkit 0.1.2 Create Avalonia boilerplate code. AvaloniaPackage Extension 1.0 AvaloniaPackage Visual Studio Extension Detailed Info Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 17.13.124.35287 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio CodeRush for Roslyn 24.2.4.0 DevExpress CodeRush for Roslyn package. CodeRush for Roslyn Tool Windows 24.2.4.0 DevExpress CodeRush for Roslyn Tool Windows package. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Dotfuscator Community Edition 6.5.3+7f0b0a9d18 PreEmptive Protection - Dotfuscator CE Herramientas de Azure App Service v3.0.0 17.13.124.35287 Herramientas de Azure App Service v3.0.0 Herramientas de C# 4.13.0-3.25105.4+400efa2d94b217101a0ca61e84a3812fdc2fec52 Los componentes de C# utilizados en el IDE. En función del tipo de proyecto y la configuración, se podría utilizar una versión diferente del compilador. Herramientas de TypeScript 17.0.31211.2001 Herramientas de TypeScript para Microsoft Visual Studio Herramientas de Visual Basic 4.13.0-3.25105.4+400efa2d94b217101a0ca61e84a3812fdc2fec52 Los componentes de Visual Basic utilizados en el IDE. En función del tipo de proyecto y la configuración, se podría utilizar una versión diferente del compilador. Herramientas de Visual F# 17.13.0-beta.25056.5+63a09289745da5c256e7baf5f4194a750b1ec878 Herramientas de Microsoft Visual F# Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics Microsoft Azure Tools para Visual Studio 2.9 Compatibilidad con proyectos de Azure Cloud Services Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Razor (ASP.NET Core) 17.13.3.2507801+90f580c3fa0908ad7d593a4ca7622e78a61aa927 Proporciona servicios de lenguajes para ASP.NET Core Razor. SQL Server Data Tools 17.13.3.1 Microsoft SQL Server Data Tools ToolWindowHostedEditor 1.0 Hosting json editor into a tool window Visual Studio IntelliCode 2.2 Desarrollo asistido por AI para Visual Studio.
Linqpad version data
Host runtime version: 8.0.13 Default query runtime version: 9.0.2 Default query reference assembly version: 9.0.2 Roslyn Version: 4.12.0-3.24572.7 FSharp.Compiler.Service version: 43.8.101.0 NuGet client version: 6.7.1.1 Windows DevDrive Status: Unavailable Results rendering engine: Edge Chromium Engine 133.0.3065.69 (WebView2)
Comments
I don't have 'Microsoft.ACE.OLEDB.12.0' on my machine, so I can't test this properly but looking at the files included in Helper.7z you have two copies of
System.Diagnostics.PerformanceCounter.dll
and one of them is a lot smaller and the constructor in CounterCreationData iswhereas the other is
So the smaller looks like a non-windows version and being used by LinqPad whereas the latter is the windows version.
Not sure of the exact reason by LinqPad picks the wrong dll but I suspect it is related to the shadowing where it attempts to copy all the necessary dlls to a shadow folder and runs from that folder.
I suspect if you delete this non-windows dll from your Helper\bin\Debug\net9.0 folder, then LinqPad will work.
Not sure how you are supposed to tell which is the correct version as the version numbers are the same.
@sgmoore
'Microsoft.ACE.OLEDB.12.0' is Microsoft Access Database Engine 2016 Redistributable (https://www.microsoft.com/en-us/download/details.aspx?id=54920). I have to use it for connecting to some legacy databases.
But to the point: That's it! I renamed it and it worked!
But it's a problem nonetheless.
It makes Linq To DB fail as well too (if you use it in the same linqpad script), and it should work out of the box.
But it is as you said. I will use the workaround until linqpad refines the "dll shadowing logic" and it becomes obsolete!
Thank you very much!!! And thanks to @JoeAlbahari for such a wonderful tool