【发布时间】: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