简体   繁体   English

PowerShell无法从ASP.NET DLL调用简单函数

[英]PowerShell fails to call a simple function from an ASP.NET DLL

I'm trying to call this really simple .NET 4 function with PowerShell v2. 我正在尝试使用PowerShell v2调用此非常简单的.NET 4函数。 Its definition look like: 其定义如下:

Public Shared Function currentSchoolYear() As String Member of NM4.SiteAdmin.Logic.Subscription 公共共享函数currentSchoolYear()作为NM4.SiteAdmin.Logic.Subscription的字符串成员

Let's add the path to the DLL file containing that function. 让我们将路径添加到包含该函数的DLL文件中。 It's an ASP.NET MVC web application DLL file. 这是一个ASP.NET MVC Web应用程序DLL文件。

PS > Add-Type -Path C:\\xxx\\bin\\xxxWebApp.dll PS>添加类型-路径C:\\ xxx \\ bin \\ xxxWebApp.dll

Let's try it (FAIL!): 让我们尝试一下(失败!):

PS > PS>
[NM4.SiteAdmin.Logic.Subscription]::currentSchoolYear() [NM4.SiteAdmin.Logic.Subscription] :: currentSchoolYear()
Exception calling "currentSchoolYear" 异常调用“ currentSchoolYear”
with "0" argument(s): "The type 与“ 0”参数:“的类型
initializer for 的初始值设定项
'NM4.SiteAdmin.Logic.Subscription' 'NM4.SiteAdmin.Logic.Subscription'
threw an exception." At line:1 char:54 引发异常。“在第1行:char:54
+ [NM4.SiteAdmin.Logic.Subscription]::currentSchoolYear + [NM4.SiteAdmin.Logic.Subscription] :: currentSchoolYear
<<<< () <<<<()
+ CategoryInfo : NotSpecified: (:) [], + CategoryInfo:未指定:(:) [],
MethodInvocationException MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException + FullyQualifiedErrorId:DotNetMethodException

Let's observe the class from PowerShell: 让我们观察PowerShell中的类:

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

IsPublic IsSerial Name IsPublic IsSerial名称
BaseType BASETYPE

-------- -------- ---- -------- True False Subscription -------- -------- ---- --------真假订阅
System.Object System.Object的

Let's observe the function from PowerShell 让我们观察一下PowerShell中的功能

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


MemberType : Method MemberType:方法
OverloadDefinitions : {static string OverloadDefinitions:{静态字符串
currentSchoolYear()} TypeNameOfValue currentSchoolYear()} TypeNameOfValue
:
System.Management.Automation.PSMethod System.Management.Automation.PSMethod
Value : static string 值:静态字符串
currentSchoolYear() Name currentSchoolYear()名称
: currentSchoolYear IsInstance :currentSchoolYear IsInstance
: True :真

Why oh why? 为什么哦为什么? It seems like the doc to me. 在我看来,这就像是医生。

I used this command to get more details about the error: 我使用此命令来获取有关该错误的更多详细信息:

$error | $ error | Format-List -force 格式列表力

Which then throwed this more precise example: 然后抛出这个更精确的例子:

System.NullReferenceException: Object System.NullReferenceException:对象
reference not set to an instance of an 引用未设置为的实例
object. 宾语。
at
NM4.SiteAdmin.GlobalFunctions.EstMachineProduction() NM4.SiteAdmin.GlobalFunctions.EstMachineProduction()
in
C:\\xxx\\Old_App_Code\\DataModel\\GlobalFunctions.vb:line C:\\ XXX \\ Old_App_Code \\ DataModel的\\ GlobalFunctions.vb:行
17 17

And I discovered it was a call to the web.config that failed: 我发现这是对web.config的调用失败:

Return 返回
ConfigurationManager.ConnectionStrings.Item("test_DBNM4").ConnectionString ConfigurationManager.ConnectionStrings.Item( “test_DBNM4”)。的ConnectionString

DLL files loaded by PowerShell are probably not in their usual web context. PowerShell加载的DLL文件可能不在其通常的Web上下文中。 I hardcoded a ConnectionString to test it (temporary, not a good security practice), and it's working. 我对ConnectionString进行了硬编码以对其进行测试(临时的,不是很好的安全实践),并且它可以正常工作。

It sounds like an exception in the static constructor OR a type on which NM4.SiteAdmin.Logic.Subscription depends could not be loaded. 这听起来像是静态构造函数中的异常, 或者是无法加载NM4.SiteAdmin.Logic.Subscription所依赖的类型。 I'm assuming "C:\\xxx\\bin\\xxxWebApp.dll" and friends aren't in GAC and you're not running from "C:\\xxx\\bin\\" so therefore powershell can't resolve the dependencies. 我假设“ C:\\ xxx \\ bin \\ xxxWebApp.dll”和朋友不在GAC中,并且您不是从“ C:\\ xxx \\ bin \\”运行,因此Powershell无法解决依赖关系。 Try running this from "C:\\xxx\\bin\\". 尝试从“ C:\\ xxx \\ bin \\”运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM