在创建封装所述方法的公共类的新实例后,我在自定义程序集库中调用公共方法。
$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
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)
确保PowerShell的CLRVersion与我的自定义程序集和依赖项匹配,即.Net Framework 4.6.2
PS D:\> $psversiontable
Name Value
---- -----
PSVersion 5.1.15063.786
PSEdition Desktop
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
确保我的自定义程序集(由下面的++表示)与其所有依赖项一起加载(由下面的表示)。为了获得已加载程序集的列表,我在生成上述异常的代码行之前使用了[System.AppDomain]::CurrentDomain.GetAssemblies()
。
{FullName=mscorlib; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=Microsoft.PowerShell.ConsoleHost; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
--{FullName=System; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=System.Core; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=System.Management.Automation; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
{FullName=Microsoft.Management.Infrastructure; Version= 1.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
{FullName=System.Management; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
{FullName=System.DirectoryServices; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
--{FullName=System.Xml; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=System.Numerics; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=System.Data; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=Anonymously Hosted DynamicMethods Assembly; Version= 0.0.0.0; Culture= neutral; PublicKeyToken= null}
{FullName=Microsoft.PowerShell.Security; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
{FullName=System.Transactions; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
{FullName=System.Configuration; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
{FullName=Microsoft.Powershell.PSReadline; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
{FullName=Microsoft.CSharp; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
{FullName=Microsoft.PowerShell.Commands.Management; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
{FullName=System.Configuration.Install; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
{FullName=Microsoft.PowerShell.Commands.Utility; Version= 3.0.0.0; Culture= neutral; PublicKeyToken= 31bf3856ad364e35}
--{FullName=Newtonsoft.Json; Version= 8.0.0.0; Culture= neutral; PublicKeyToken= 30ad4fe6b2a6aeed}
--{FullName=SharpYaml; Version= 1.6.1.0; Culture= neutral; PublicKeyToken= 1ced8108e942bc02}
--{FullName=Microsoft.OpenApi.Readers; Version= 1.0.0.0; Culture= neutral; PublicKeyToken= 3f5743946376f042}
--{FullName=Microsoft.OpenApi; Version= 1.0.0.0; Culture= neutral; PublicKeyToken= 3f5743946376f042}
++{FullName=Microsoft.OpenApi.CSharpComment.Reader; Version= 1.0.0.0; Culture= neutral; PublicKeyToken= null}
--{FullName=System.Xml.Linq; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b77a5c561934e089}
--{FullName=System.Web; Version= 4.0.0.0; Culture= neutral; PublicKeyToken= b03f5f7f11d50a3a}
问题是什么?我是否错误地调用了该方法?
答案 0 :(得分:1)
事实证明,我调用的库创建了一个新的app域,然后尝试了我传递的加载程序集。但是,新的应用程序域基本路径设置为计算机上PowerShell.exe的位置,因此路径无法正确解析,因为路径是相对于脚本的应用程序域基本路径。
我的解决方案是在调用库之前设置基本路径,然后在库将控制权返回给我的脚本之后恢复原始基本路径。