【问题标题】:SSIS Script component: FileNotFoundException: Could not load file or assembly 'MongoDB.DriverSSIS 脚本组件:FileNotFoundException:无法加载文件或程序集“MongoDB.Driver”
【发布时间】:2022-01-31 11:50:49
【问题描述】:

我正在使用脚本组件将数据插入到 MongoDB。由于 MongoDB 驱动程序未签名,因此无法添加到 GAC,我使用以下方法在运行时从保存所有需要的引用 DLL 的已知位置加载它:

private const string AssembyPath = @"C:\Users\acme\source\repos\import-members-and-optins\lib";
    
static ScriptMain()

{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    // retrieve just a name of this assembly
    var assemblyName = args.Name.Split(',')[0];
    string fullPath = Path.Combine(AssembyPath, string.Format("{0}.dll", assemblyName));
    try
    {
        return Assembly.LoadFile(fullPath);
    }
    catch (Exception ex)
    {
        throw new Exception($"{fullPath} not found", ex);
    }
}

但是,我遇到了以下异常,我什至无法调试它,因为它发生在任务能够运行之前。就像处理程序永远不会执行一样。我已经检查并且我的包在 x86 中运行,所以我应该能够调试它,但我的处理程序永远不会被命中。 :-(

数据流任务中的包验证错误错误 [Upsert Mongo [69]]: System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> System.TypeInitializationException:类型初始化程序 'ScriptMain' 引发了异常。 ---> System.IO.FileNotFoundException: 无法加载文件或程序集 'MongoDB.Driver,版本 = 2.14.1.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。这 系统找不到指定的文件。 在 ScriptMain..cctor()
--- 内部异常堆栈跟踪结束 --- 在 ScriptMain..ctor() --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)

我在这里缺少什么?

【问题讨论】:

  • 为什么要在项目中添加驱动程序?驱动程序安装在机器上并添加到设备管理器中。应用程序在已安装的设备中进行查找以获取驱动程序,而不是应用程序。
  • 嗨@jdweng 我只是添加对MongoDB数据库驱动程序的引用,它是一个独立的dll
  • 您是在执行客户端代码还是服务器代码?与数据库的连接应该在服务器代码中。客户端应该向服务器发出请求,服务器处理与数据库的连接。独立的 dll 可能是服务器代码,它在第一次调用时使用连接字符串连接到数据库。
  • 嗨@jdweng 这是一个在我的本地机器上运行的SSIS包,用于将数据导入MongoDB实例。
  • SSIS 脚本在服务器上运行,而不是在您的机器上。

标签: c# mongodb ssis etl script-component


【解决方案1】:

我认为主要问题是由在触发程序集解析器之前调用的类中全局启动变量引起的。

尽量不要在public class ScriptMain : UserComponent 类中启动任何变量,并将它们移动到适当的方法或PreExecute 方法中。

尝试将您的代码更改为以下内容:

static ScriptMain()

{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string AssembyPath = @"C:\Users\acme\source\repos\import-members-and-optins\lib";
    // retrieve just a name of this assembly
    var assemblyName = args.Name.Split(',')[0];
    string fullPath = Path.Combine(AssembyPath, string.Format("{0}.dll", assemblyName));
    try
    {
        return Assembly.LoadFile(fullPath);
    }
    catch (Exception ex)
    {
        throw new Exception($"{fullPath} not found", ex);
    }
}

另外,请确保没有全局启动其他变量。

有用的资源

【讨论】:

  • 谢谢,仍然想知道我怎么会错过这个,有一个静态文件已内联初始化,一旦我在处理程序初始化后立即将其移至静态构造函数,现在它工作正常。
  • @Oscar 我以前遇到过同样的问题,花了很多时间来解决它。这很棘手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 2019-12-22
  • 2023-04-07
  • 2011-09-29
  • 2023-01-17
  • 1970-01-01
相关资源
最近更新 更多