场景和问题代码
在创建封装上述方法的公共类的新实例后,我将在自定义程序集库中调用一个公共方法。
$openApiDocumentGenerator = New-Object Microsoft.OpenApi.CSharpComment.Reader.OpenApiDocumentGenerator
$openApiDocumentGenerator.GenerateSerializedOpenApiDocuments( $VisualStudioXmlDocument, $AssemblyPaths, $OpenApiSpecVersion, $OpenApiFormat, $ConfigurationXmlDocument)通过执行以下操作加载了我的自定义程序集和依赖项,
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方法后出现的异常
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)到目前为止我试过什么?
[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} -问题是什么呢?我调用方法不正确吗?
发布于 2018-06-06 06:45:08
结果,我调用的库创建了一个新的应用程序域,然后尝试加载我传递的程序集。但是,新的应用程序域基路径被设置为机器上PowerShell.exe的位置,因此路径不能正确解析,因为路径相对于脚本的应用域基础路径。
在我的例子中,解决方案是在调用库之前设置基本路径,然后在库将控制权返回给我的脚本后恢复原来的基本路径。
https://stackoverflow.com/questions/48160549
复制相似问题