Ho cercato una soluzione per un paio di giorni. Nel mio caso, nessuna delle risposte funziona come riferimento (carica?) Assembly .NET nel dominio app dalla sessione di PowerShell.Caricamento delle dipendenze di assembly .NET non funzionanti
Prima di caricare i riferimenti (che devono essere referenziati dalla DLL summenzionata per funzionare [Reflection.Assembly]::LoadFile()
o [Reflection.Assembly]::LoadFrom()
), quindi carico la DLL .NET chiamando Add-Type
.
Sfortunatamente non funziona, quindi non posso creare alcune istanze da quella DLL. Sto ottenendo gli stessi errori quando uso DLL senza riferimenti collegati in un normale progetto C# ma non appena faccio riferimento agli altri assembly e ricompilato funziona senza errori (posso confermare che è a causa di assembly referenziati come ho verificato che in LinqPad come bene).
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
terza linea di quello script PowerShell tiri:
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
interrogazione LINQPad (C# Programma; riferimenti Connector.dll) - Questo funziona bene
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
}
Quindi, qual è il problema? Non puoi semplicemente caricare le dipendenze/riferimenti * prima * chiamando 'Aggiungi-Tipo'? –
Sì, li carico prima (Add-Type) e sembra che venga caricato in un ambito diverso in modo che la DLL principale non risolva correttamente gli assembly. Ho lo stesso codice in LINQPad e funziona. – algorytmus
Hai provato a utilizzare 'Assembly.LoadFrom'? – SOReader