Powershell 2.0运行时异常;无法加载文件或程序集 [英] Powershell 2.0 Runtime exception; could not load file or assembly

查看:961
本文介绍了Powershell 2.0运行时异常;无法加载文件或程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力纠正的场景是在C#编译时找到dll,而在执行c#代码时找不到的场景。这是Powershell 2.0。我们的政策是不要使用GAC。 Powershell函数中的c#代码类似于:

  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(){}

<#
方法和属性定义不包括
#>

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

private double computeDef(double value)
{
<#调用DLLS中加载的方法
#>
}
}
}
@
add-type -ReferencedAssemblies $ ref -TypeDefinition $ cCode -passthru - 语言CSharpVersion3 | out-null
}

调用这个函数时,c#代码被编译出来错误。



我将两个引用的DLLS的路径添加到$ env:path值。在另一个位置安装了第三方DLL路径DLL;该路径也被添加到$ env:path。两个路径的权限对所有用户都是开放的测试目的。



然后我将C#代码作为新对象实例:

  $ myObject = new-object serializedDef.defSerialization 

当我调用函数时:

  $ myObject.setSpec = 35.5 
/ pre>

我收到错误:

 异常设置setSpec :无法加载文件或副本
'func1',Version = 1.0.0.0,Culture = neutral,PublicKeyToken = f0a7c34216660f47'或其依赖关系,系统找不到指定的文件。
在行:1 char:11
+ $ myObject。 <<< setSpec = 35.5
+ CategogyInfo:InvalidOperation:(:) [],RuntimeException
+ FullyQualifedErrorId:PropertyAssignmentException

我的理解是,错误可能与一个DLL引用的方法等有关,无法找到。我如何跟踪这一点?

解决方案

我在回答一中引用发布后建立了一个解决方案(如何使PowerShell添加的类型使用添加的类型 )。它在x87和x64平台上都能很好地工作。谢谢。


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?

解决方案

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.

这篇关于Powershell 2.0运行时异常;无法加载文件或程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆