[英]SSIS Script component: FileNotFoundException: Could not load file or assembly 'MongoDB.Driver
I'm using a script component to upsert data to MongoDB.我正在使用脚本组件将数据插入到 MongoDB。 As MongoDB driver is not signed, and thus can't be added to GAC, I using the following method to load it at runtime from the known location where all needed reference DLL are saved:由于 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);
}
}
However, I'm getting the following exception, and I'm not even able to debug it, as it's happening before the task is able to run.但是,我得到了以下异常,我什至无法调试它,因为它发生在任务能够运行之前。 It's like the handler is never executed.就像处理程序永远不会执行一样。 I have checked and my package is running in x86, so I should be able to debug it, but my handler is never hit.我已经检查过,我的 package 正在 x86 中运行,所以我应该能够调试它,但我的处理程序永远不会被击中。 :-( :-(
Package Validation Error Error at Data Flow Task [Upsert Mongo [69]]: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. Package 数据流任务中的验证错误错误 [Upsert Mongo [69]]:System.Reflection.TargetInvocationException:调用目标已引发异常。 ---> System.TypeInitializationException: The type initializer for 'ScriptMain' threw an exception. ---> System.TypeInitializationException:“ScriptMain”的类型初始化程序引发了异常。 ---> System.IO.FileNotFoundException: Could not load file or assembly 'MongoDB.Driver, Version=2.14.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. ---> System.IO.FileNotFoundException:无法加载文件或程序集 'MongoDB.Driver,Version=2.14.1.0,Culture=neutral,PublicKeyToken=null' 或其依赖项之一。 The system cannot find the file specified.该系统找不到指定的文件。 at ScriptMain..cctor()在 ScriptMain..cctor()
--- End of inner exception stack trace --- at ScriptMain..ctor() --- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) --- 内部异常堆栈跟踪结束 --- 在 ScriptMain..ctor() --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
What I'm missing here?我在这里缺少什么?
I think that the main issue is caused by initiating variables globally within the class which is called before the assembly resolver is fired.我认为主要问题是由在启动程序集解析器之前调用的 class 中全局启动变量引起的。
Try not to initiate any variable within the public class ScriptMain: UserComponent
class and mover them into the appropriate methods or int the PreExecute
method.尽量不要在public class ScriptMain: UserComponent
class 中启动任何变量,并将它们移动到适当的方法或 int PreExecute
方法中。
Try changing your code to the following:尝试将您的代码更改为以下内容:
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);
}
}
Also, make sure that no other variables are initiated globally.此外,请确保没有全局启动其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.