C# 加载.NET程序集依赖项不起作用
几天来我一直在寻找解决办法。在我的案例中,所有答案都不起作用,即从PowerShell会话将程序集.NET引用(加载?)到应用程序域 我首先加载引用(上述DLL需要引用的引用才能正常工作C# 加载.NET程序集依赖项不起作用,c#,powershell,dll,.net-assembly,C#,Powershell,Dll,.net Assembly,几天来我一直在寻找解决办法。在我的案例中,所有答案都不起作用,即从PowerShell会话将程序集.NET引用(加载?)到应用程序域 我首先加载引用(上述DLL需要引用的引用才能正常工作[Reflection.Assembly]::LoadFile()或[Reflection.Assembly]::LoadFrom()),然后通过调用添加类型加载.NET DLL 不幸的是,这不起作用,所以我无法从该DLL创建几个实例。当我在一个普通的C#项目中使用没有附加引用的DLL时,我也会遇到同样的错误,但
[Reflection.Assembly]::LoadFile()
或[Reflection.Assembly]::LoadFrom()
),然后通过调用添加类型
加载.NET DLL
不幸的是,这不起作用,所以我无法从该DLL创建几个实例。当我在一个普通的C#项目中使用没有附加引用的DLL时,我也会遇到同样的错误,但只要我引用了其他程序集并重新编译,它就会正常工作(我可以确认这是因为在LinqPad中也检查了引用的程序集)
PowerShell:
[System.Reflection.Assembly]::LoadFile((Get-Item -Path ".\System.Data.SQLite.dll" ).FullName)
Add-Type -Path (Get-Item -Path ".\Connector.dll" ).FullName -ReferencedAssemblies (Get-Item -Path ".\System.Data.SQLite.dll" ).FullName -PassThru | Out-Null
$certMGT = New-Object Connector
PowerShell脚本的第三行抛出:
New-Object : Exception calling ".ctor" with "0" argument(s): "Failed to find or load the registered .Net Framework Data Provider."
At C:\Repos\Connector\bin\Installer.ps1:306 char:20
+ $certMGT = New-Object Connector
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
PSMessageDetails :
Exception : System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "Failed to find or load the registered .Net Framework
Data Provider." ---> System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.
at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
at Connector.Entity.ConnectorDBEntities..ctor(String connectionString)
at Connector.DBManager..ctor()
at Connector.DAL.ConfigurationDAL..ctor()
at Connector.ConnectorConfig..ctor()
at Connector.ConnectorCertMGT..ctor()
--- End of inner exception stack trace ---
at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArgumen
ts)
at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, Object[] arguments)
at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type, ConstructorInfo[] constructors, Object[] args)
TargetObject :
CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
LinqPad查询(C#Program;references Connector.dll)——这很好用
void Main()
{
Assembly.LoadFile(@"C:\Repos\Connector\bin\System.Data.SQLite.dll");
Connector connector = new Connector();//this also throws exactly the same error if I do not LoadFile as in above line
}
那么,有什么问题?在调用
Add Type
之前,您不能加载依赖项/引用吗?是的,我确实在调用之前加载了它们(Add Type),并且它似乎加载到了不同的范围,因此主DLL无法正确解析程序集。我在LINQPad中使用了相同的代码,它可以正常工作。您是否尝试过使用Assembly.LoadFrom
?嗯。。。这就是我想问你的。您说过您正在使用LoadFile
加载依赖程序集。在这种情况下,应使用AFAIR,LoadFrom
。请粘贴您的代码。为什么不为System.Data.SQLite.dll使用添加类型?