2015-11-20 18 views
5

Szukałem rozwiązania przez kilka dni. Żadna z odpowiedzi nie działa w moim przypadku, która ma odwoływać się (ładować?) Złożeń .NET do domeny aplikacji z sesji PowerShell.Ładowanie zależności .NET nie działa

Najpierw ładuję odnośniki (które są wymagane do odniesienia się do wspomnianej biblioteki DLL do pracy [Reflection.Assembly]::LoadFile() lub [Reflection.Assembly]::LoadFrom()), a następnie ładuję moją bibliotekę .NET DLL przez wywołanie Add-Type.

Niestety to nie działa, więc nie mogę utworzyć kilku instancji z tej biblioteki DLL. Otrzymuję te same błędy, gdy używam biblioteki DLL bez odnośników załączonych w normalnym projekcie C#, ale gdy tylko odwołuję się do innych złożeń i przekompiluję, działa bezbłędnie (mogę potwierdzić, że jest to spowodowane odwołanymi złożeniami, ponieważ sprawdziłem to w LinqPad jako dobrze).

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 

trzecia linia tego skryptu PowerShell rzuca:

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 zapytanie (C# program; referencje Connector.dll) - to działa prawidłowo

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 
} 
+0

Jaki jest problem? Nie możesz po prostu załadować zależności/referencje * przed * wywoływanie 'Add-Type'? –

+0

Tak, ładuję je (Add-Type) wcześniej i wygląda na to, że są one ładowane do innego zakresu, tak że główna biblioteka DLL nie rozpoznaje prawidłowo złożeń. Mam ten sam kod w LINQPad i działa. – algorytmus

+0

Czy próbowałeś użyć 'Assembly.LoadFrom'? – SOReader

Odpowiedz

Powiązane problemy