Powershell 2.0 Runtime exception; could not load file or assembly

自古美人都是妖i 提交于 2019-12-13 02:56:09

问题


The scenario that I am trying to rectify is one in which dlls are found when C# is compiled but not found when the c# code is executed. This is Powershell 2.0. Our policy is to not use the GAC. The c# code in a Powershell function is similar to this:

function functionDef
{
  [System.Reflection.Assembly]::LoadFrom("c:\myDir\func1.dll")
  [System.Reflection.Assembly]::LoadFrom("c:\myDir\func2.dll")

  $ref = @("c:\myDir\func1.dll","c:\myDir\func2.dll")

  $cCode = @"
    using System;
    using func1;
    using func2;

    namespace serializedDef
    {
       public class defSerialization
       {

          public defSerialization () {}

   <# 
             method and properyty defs not included
          #>

          public double setSpec {
  set { computeDef(value)}
  get {return spefDef}
          }

   private double computeDef ( double value)
   {
            <# calls to methods in DLLS loaded above
             #>
   }
       }
    }
 "@
 add-type -ReferencedAssemblies $ref -TypeDefinition $cCode -passthru - Language CSharpVersion3 | out-null
}

When this function is invoked, the c# code is compiled with out error.

I add the path to the two referenced DLLS to the $env:path value. There is a 3rd party DLL path DLL installed at another location; that path too is added to the $env:path. The permissions on both paths are wide open to everyone for testing purposes.

I then instance the C# code as a new object:

 $myObject = new-object serializedDef.defSerialization

When I invoke the function:

 $myObject.setSpec = 35.5

I get the error:

Exception setting "setSpec": "Could not load  file or assmbly
'func1', Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0a7c34216660f47' or one of its dependencies. The system cannot find the file specified."
At line:1 char:11
+ $myObject. <<< setSpec = 35.5
  + CategogyInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifedErrorId  : PropertyAssignmentException

My understanding is that the error is likely is related to a method or the like that one of the DLLs is referencing which can not be found. How do I track this down?


回答1:


.NET assemblies are not loaded from the path. The app base dir is the PowerShell install dir and you don't really want to copy your assemblies there. I recommend hooking the AppDomain.AssemblyResolve event. This event will get called when the CLR can't find an assembly, at this point you can provide the full path to the assembly in question. Look at this SO post for more info.




回答2:


I modeled a solution after the posting referenced in Answer one (How can I get PowerShell Added-Types to use Added Types). It works perfectly on both x87 and x64 platforms. Thank-you.



来源:https://stackoverflow.com/questions/4259727/powershell-2-0-runtime-exception-could-not-load-file-or-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!