【问题标题】:PowerShell fails to call a simple function from an ASP.NET DLLPowerShell 无法从 ASP.NET DLL 调用简单函数
【发布时间】:2010-11-30 00:01:39
【问题描述】:

我正在尝试使用 PowerShell v2 调用这个非常简单的 .NET 4 函数。它的定义如下:

公共共享功能 currentSchoolYear() 作为字符串 NM4.SiteAdmin.Logic.Subscription 的成员

让我们添加包含该函数的 DLL 文件的路径。这是一个ASP.NET MVC Web 应用程序 DLL 文件。

PS> Add-Type -Path C:\xxx\bin\xxxWebApp.dll

让我们尝试一下(失败!):

PS >
[NM4.SiteAdmin.Logic.Subscription]::currentSchoolYear()
异常调用“currentSchoolYear”
带有“0”参数:“类型

的初始化器 'NM4.SiteAdmin.Logic.Subscription'
抛出异常。”在 line:1 char:54
+ [NM4.SiteAdmin.Logic.Subscription]::currentSchoolYear
+ CategoryInfo : 未指定: (:) [],
方法调用异常
+ FullyQualifiedErrorId : DotNetMethodException

让我们从 PowerShell 观察课程:

PS >
[NM4.SiteAdmin.Logic.Subscription]

IsPublic IsSerial 名称
基本类型

-------- -------- ---- -------- True False 订阅
系统对象

让我们从 PowerShell 中观察函数

PS >
[NM4.SiteAdmin.Logic.Subscription]::currentSchoolYear


成员类型:方法
重载定义:{静态字符串
currentSchoolYear()} TypeNameOfValue
:
System.Management.Automation.PSMethod
值:静态字符串
currentSchoolYear() 名称
: currentSchoolYear IsInstance
: 是的

为什么哦,为什么?对我来说,这似乎是文档。

【问题讨论】:

    标签: .net powershell


    【解决方案1】:

    我使用此命令获取有关错误的更多详细信息:

    $错误 |格式列表-force

    然后抛出这个更精确的例子:

    System.NullReferenceException:对象
    引用未设置为
    的实例 目的。

    NM4.SiteAdmin.GlobalFunctions.EstMachineProduction()

    C:\xxx\Old_App_Code\DataModel\GlobalFunctions.vb:line
    17

    我发现这是对 web.config 的调用失败:

    返回
    ConfigurationManager.ConnectionStrings.Item("test_DBNM4").ConnectionString

    PowerShell 加载的 DLL 文件可能不在其通常的 Web 上下文中。我硬编码了一个 ConnectionString 来测试它(临时的,不是一个好的安全实践),它正在工作。

    【讨论】:

      【解决方案2】:

      这听起来像是静态构造函数中的异常OR 无法加载 NM4.SiteAdmin.Logic.Subscription 所依赖的类型。我假设“C:\xxx\bin\xxxWebApp.dll”和朋友不在 GAC 中,并且您没有从“C:\xxx\bin\”运行,因此 powershell 无法解决依赖关系。尝试从“C:\xxx\bin\”运行它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 1970-01-01
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多