c# - 在生产数据库上部署应用程序时无法加载文件或程序集“System.Data”错误

标签 c# mysql dll

问题: 将我的应用程序部署到生产数据库时,出现以下错误:

Could not load file or assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified. at ConsoleApplication2.Database.DBConnect.Initialize(String username, String password, String IP) at ConsoleApplication2.Database.DBConnect..ctor(String username, String password, String IP) in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\DBConnect.cs:line 26 at ConsoleApplication2.Database..ctor() in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\Database.cs:line 20 at ConsoleApplication2.Main..ctor() in C:\Users\Vincent\Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\Main.cs:line 16

我所知道的:实际上并不是 System.Data.dll 有问题。

自从我开始制作一个干净的控制台应用程序以来。 首先只使用 Console.write()。当它起作用时,我创建了一个新类并做了一些常规计算(也没有问题),然后我决定使用 MySQL.data.MySqlClient 创建一个 MySQL 连接;从 Mysql.data.dll 中,这是错误再次发生的时候。我从the mysql site获取了dll .

奇怪的部分: 这个 dll 一直在我的计算机(我在其上创建了应用程序)和 2 个不同的服务器(其中一个服务器是我设置的只是为了测试这些应用程序以查看是否会遇到相同的问题)上运行。

一些规范: 我的电脑运行 Windows 7 32 位。 生产数据库运行 Windows 2003 Service Pack 2,测试服务器和我尝试过的其他服务器也是如此。

我做过的其他一些事情: 我创建了一个安装程序(.msi 和 .exe),用于安装 .net 3.5 service pack 1 和 .net 2.0 service pack 2.0 和 .net 3.0 service pack 2。 我觉得很奇怪,它会安装所有这些 .net 框架,但要安装 3.5,你需要 2.0,这样才有意义(但不确定为什么安装 3.0)

我还尝试在未安装 MySQL 连接器的情况下运行该应用程序(因此只需将 mysql.data.dll 放在应用程序文件夹中),并且我尝试在安装了 MySQL 连接器的情况下运行该应用程序(因此应用程序文件夹中没有 dll)

我尝试将我的.net框架复制到生产数据库(这是在我知道它是关于mysql.data.dll而不是system.dll或system.data.dll之前)

我已经尝试了我能想到的所有方法,但没有任何效果,它在所有其他计算机/数据库上都可以正常工作,只是在我们想要部署应用程序的那台计算机/数据库上不起作用......

发生错误的代码:

        public DBConnect(string username, string password, string IP)
        {
            Console.WriteLine("dbconnect contsrutctor");
            Initialize(username, password, IP);
        }

        private void Initialize(string username, string password, string IP)
        {
            Console.WriteLine("initializing strings");
            string server = IP;
            string database = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" +
            database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";";
            Console.WriteLine("initializing mysqlconnection");
            //MySqlConnection connection = new MySqlConnection(connectionString);
        }

我已评论该行:

MySqlConnection connection = new MySqlConnection(connectionString);

应用程序运行时没有错误,当我取消注释时,错误再次发生。

编辑

我注意到一件奇怪的事情:当它到达创建 MySqlConnection 的方法(而不是行)时,就会抛出错误。如果您查看上面的代码示例,第一个 Console.WriteLine("initializing strings"); 甚至不会显示在我的控制台中。我觉得这很奇怪,因为您希望在 MySqlConnection connection = new MySqlConnection(connectionString); 行上抛出错误,而不是在方法的开头。

编辑2 我以为我找到了答案,所以我发布了它:

"Though i have checked the system.data.dll and system.dll versions and file paths (which were exactly the same on all 3 machines I did find the problem to be with these files.

In my application I've set the "copy local" to true and after this it worked.

I find this extremely odd since I've tried replacing the dll's on the production server with my own and this did not work. also just placing the dll's in the application folder did not help either. I had to specifically specify the application to copy the dll's it self.

anyway it's working properly now although I'm not quite convinced i like this solution...

I definitely do not like this because i need to do this to half of the dll's (yes half not all just about half of them).

any better options are still welcome."

但是我没有提到它在我的测试应用程序上有效,这次真正的应用程序找不到 system.transactions 。所以它仍然不是正确的解决方案,尽管它确实清除了有关 system.data.dll 的错误,但由于不同文件的类似错误,我仍然无法运行该程序。

因此,由于我仍然遇到相同的错误,但在不同的文件上(我无法将“复制本地”设置更改为 true,因为它没有被我引用),我已删除答案并将其放在此处作为编辑,因为问题仍然没有解决。

编辑3 我运行了 Fuslogvw.exe,结果如下:

*** Assembly Binder Log Entry (12/5/2013 @ 8:43:13 AM) ***

The operation failed. Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll Running under executable C:\Program Files\Custommate\email sorteer service\EmailSorteerService.exe --- A detailed error log follows.

=== Pre-bind state information === LOG: User = NAVMATE\Administrator LOG: DisplayName = System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (Fully-specified) LOG: Appbase = file:///C:/Program Files/Custommate/email sorteer service/ LOG: Initial PrivatePath = NULL LOG: Dynamic Base = NULL LOG: Cache Base = NULL LOG: AppName = NULL Calling assembly : EmailSorteerService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Post-policy reference: System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System/System.DLL. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System.EXE. LOG: Attempting download of new URL file:///C:/Program Files/Custommate/email sorteer service/System/System.EXE. LOG: All probing URLs attempted and failed.

这意味着它试图从我运行应用程序的文件夹中加载 dll,我觉得这很奇怪,因为我不知道是什么原因造成的。 (我已确保复制本地设置为 false,因此这不会成为问题)

所以新问题:我如何确保它从正确的位置加载?

最佳答案

一进入方法就抛出异常的原因很简单——JIT编译器只需要解析引用即可。 MySqlConnection 需要 System.Data,并且它是第一个执行此操作的方法,因此此时会引发异常。

就调试问题而言,目标计算机上似乎未正确安装 .NET 框架。你应该尝试How to enable assembly bind failure logging (Fusion) in .NET看看 .NET 实际上在哪里尝试找到该库,以及为什么它会丢弃它找到的任何库。

如果它不能解决您的问题,我会尝试卸载并重新安装 .NET Framework 2.0,然后再安装 3.5。

“复制本地”之所以有效,是因为应用程序不会尝试从全局程序集缓存加载 DLL,而是从应用程序的可执行目录加载。然而,您真正的问题是它在 GAC 中找不到正确的 DLL。

如果 Fusion 显示 MySQL 库尝试加载错误版本的 System.Data,您可以使用应用程序 list 强制它加载不同版本(希望应该兼容)。您可以在此处阅读有关应用程序 list 的信息 - http://msdn.microsoft.com/en-us/library/aa374191(VS.85).aspx

祝你好运。

关于c# - 在生产数据库上部署应用程序时无法加载文件或程序集“System.Data”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20211526/

相关文章:

c# - 如何通过委托(delegate)查询方法属性?

mysql - 从应用程序中选择最新的注释/评论

mysql - 我该如何优化这个查询?

c# - 无法将 dll 程序集引用到 Visual Studio Code 项目

c - 如何混淆非托管dll的函数名

c# - 关于使用 Caliburn.Micro MVVM WPF 进行 View 导航的建议

C# UWP 取消任务 + 给 child 发信号

mysql - 我可以加快这个 mysql 查询吗?

c++ - 从 C++ dll 调用 VB 程序

C# 字符串 - 简单语法问题