首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用实例方法时的Powershell MethodInvocationException

调用实例方法时的Powershell MethodInvocationException
EN

Stack Overflow用户
提问于 2018-01-09 02:10:12
回答 1查看 4.4K关注 0票数 1

场景和问题代码

在创建封装上述方法的公共类的新实例后,我将在自定义程序集库中调用一个公共方法。

代码语言:javascript
复制
$openApiDocumentGenerator = New-Object Microsoft.OpenApi.CSharpComment.Reader.OpenApiDocumentGenerator
$openApiDocumentGenerator.GenerateSerializedOpenApiDocuments( $VisualStudioXmlDocument, $AssemblyPaths, $OpenApiSpecVersion, $OpenApiFormat, $ConfigurationXmlDocument)

通过执行以下操作加载了我的自定义程序集和依赖项,

代码语言:javascript
复制
Add-Type -AssemblyName "System.Web"
Add-Type -AssemblyName "System.Xml.Linq"
Add-Type -Path $newtonsoftJsonAssemblyPath
Add-Type -Path $sharpYamlAssemblyPath
Add-Type -Path $microsoftOpenApiReadersAssemblyPath
Add-Type -Path $microsoftOpenApiAssemblyPath
Add-Type -Path $csharpCommentReaderAssemblyPath

调用GenerateSerializedOpenApiDocuments方法后出现的异常

代码语言:javascript
复制
System.Management.Automation.MethodInvocationException: Exception calling "GenerateSerializedOpenApiDocuments" with "5" argument(s): "Could not load file or assembly 'Microsoft.OpenApi.CSharpComment.Reader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.OpenApi.CSharpComment.Reader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName)
   at Microsoft.OpenApi.CSharpComment.Reader.AppDomainCreator`1..ctor()
   at Microsoft.OpenApi.CSharpComment.Reader.OpenApiDocumentGenerator.GenerateSerializedOpenApiDocuments(XDocument annotationXmlDocument, IList`1 contractAssemblyPaths, OpenApiSpecVersion openApiSpecVersion, OpenApiFormat openApiFormat, XDocument configurationXmlDocument)
   at CallSite.Target(Closure , CallSite , Object , XDocument , Object , Object , Object , XDocument )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

到目前为止我试过什么?

  1. 确保CLRVersion of PowerShell与我的自定义程序集和依赖项相匹配,即.Net Framework4.6.2 PS D:> $psversiontable名称值- PSVersion 5.1.15063.786 PSEdition桌面PSCompatibleVersions {1.0,2.0,3.0,4.0.} BuildVersion 10.0.15063.786 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
  2. 确保我的自定义程序集(下面由++表示)与它的所有依赖项一起加载(由-下面表示)。为了获得加载程序集的列表,我在生成上述异常的代码行之前利用了[System.AppDomain]::CurrentDomain.GetAssemblies()。 {FullName=mscorlib;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b77a5c561934e089} {FullName=Microsoft.PowerShell.ConsoleHost;Version= 3.0.0.0;Culture=中性;PublicKeyToken= 31bf3856ad364e35} -{FullName=System;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b77a5c561934e089} {FullName=System.Core;Version= 4.0.0.0;b77a5c561934e089} {#en0 20#;3.0.0.0;中性;31bf3856ad364e35} en24 1.0.0.0;中性;PublicKeyToken= 31bf3856ad364e35} {FullName=System.Management;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b03f5f7f11d50a3a} {FullName=System.DirectoryServices;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b03f5f7f11d50a3a} -{FullName=System.Xml;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b77a5c561934e089} {FullName=System.Numerics;PublicKeyToken= 4.0.0.0;FullName=System.Numerics中性;en29 20#}{en21#;4.0.0.0;中性;}{承载;0.0.0.0;中性;PublicKeyToken= null} {FullName=Microsoft.PowerShell.Security;Version= 3.0.0.0;Culture=中性;PublicKeyToken= 31bf3856ad364e35} {FullName=System.Transactions;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b77a5c561934e089} {FullName=System.Configuration;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b03f5f7f11d50a3a} {FullName=Microsoft.Powershell.PSReadline;FullName=Microsoft.Powershell.PSReadline 3.0.0.0;PublicKeyToken= 31bf3856ad364e35} {#en0 20#;4.0.0.0;中性;en24#}en25#3.0.0;{FullName=Microsoft.PowerShell.Commands.Utility;Culture=中性;PublicKeyToken= 31bf3856ad364e35} {FullName=System.Configuration.Install;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b03f5f7f11d50a3a} b03f5f7f11d50a3a Version= 3.0.0.0;Culture=中性;PublicKeyToken= 31bf3856ad364e35} -{FullName=Newtonsoft.Json;Version= 8.0.0.0;Culture=中性;PublicKeyToken= 30ad4Fe6b2a6a6aaeed}-{FullName=SharpYaml;Version= 1.6.1.0;PublicKeyToken=中性;1 Ced8108e942bc02}-{;#en23 20# 1.0.0.0;#en21中性;3f5743946376f042} -{#en23;++{FullName=Microsoft.OpenApi.CSharpComment.Reader;Version= 1.0.0.0;Culture=中性;PublicKeyToken= 3f5743946376f042} Version= 1.0.0.0;Culture=中性;PublicKeyToken= null} -{FullName=System.Xml.Linq;Version= 4.0.0.0;Culture=中性;PublicKeyToken= }-{FullName=System.Web;Version= 4.0.0.0;Culture=中性;PublicKeyToken= b03f5f7f11d50a3a} -

问题是什么呢?我调用方法不正确吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 06:45:08

结果,我调用的库创建了一个新的应用程序域,然后尝试加载我传递的程序集。但是,新的应用程序域基路径被设置为机器上PowerShell.exe的位置,因此路径不能正确解析,因为路径相对于脚本的应用域基础路径。

在我的例子中,解决方案是在调用库之前设置基本路径,然后在库将控制权返回给我的脚本后恢复原来的基本路径。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48160549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档