C# PS Cmdlet - 无法加载文件或程序集或其依赖项之一。系统找不到该文件

问题描述 投票:0回答:1

我正在用 C# 编写一个 PowerShell cmdlet,一切正常,直到我引用 nuget 中的库,然后抛出有关无法加载文件或程序集的错误。

就好像代码没有内置到 DLL 中 - 如何确保所有引用的 nuget 库都嵌入到我的 DLL 中,或者至少在外部注册,以允许使用 cmdlet?

在这种情况下,它特别抱怨

Nito.AsyncEx
(或者如果我删除它,它将转到下一个库,例如
Flurl
)基本上是我通过nuget为项目引用的任何库:

Import-Module .\PolygonIo.PowerShell.dll
Get-Aggregates -StocksTicker TSLA

Get-Aggregates : Could not load file or assembly 'Nito.AsyncEx.Tasks, Version=5.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file
specified.
At line:1 char:1
+ Get-Aggregates -StocksTicker TSLA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Aggregates], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,PolygonIo.PowerShell.GetAggregates

我的项目文件是:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Nito.AsyncEx" Version="5.1.0" />
    <PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\PolygonIo.WebApi\PolygonIo.WebApi.csproj" />
  </ItemGroup>

</Project>

cmdlet 中的代码是:

public class GetAggregates : Cmdlet
{
    /* snip parameters and ProcessRecordAsync() */

    protected override void ProcessRecord()
    {
        base.ProcessRecord();

        var task = ProcessRecordAsync();
        var result = task.WaitAndUnwrapException();
    }
}

生成的文件是(我注意到,我通常会像在其他构建的应用程序中一样在这里引用所有库 DLL,但它们丢失了):

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        21/12/2020     23:49          26112 PolygonIo.Api.dll
-a----        21/12/2020     23:49          12300 PolygonIo.Api.pdb
-a----        21/12/2020     23:49           9838 PolygonIo.PowerShell.deps.json
-a----        21/12/2020     23:49           9216 PolygonIo.PowerShell.dll
-a----        21/12/2020     23:49           9156 PolygonIo.PowerShell.pdb

并且

PolygonIo.PowerShell.deps.json
是:

{
  "runtimeTarget": {
    "name": ".NETStandard,Version=v2.0/",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETStandard,Version=v2.0": {},
    ".NETStandard,Version=v2.0/": {
      "PolygonIo.PowerShell/1.0.0": {
        "dependencies": {
          "NETStandard.Library": "2.0.3",
          "Nito.AsyncEx": "5.1.0",
          "PolygonIo.Api": "1.0.0",
          "PowerShellStandard.Library": "5.1.0"
        },
        "runtime": {
          "PolygonIo.PowerShell.dll": {}
        }
      },
      "Flurl/3.0.1": {
        "runtime": {
          "lib/netstandard2.0/Flurl.dll": {
            "assemblyVersion": "3.0.1.0",
            "fileVersion": "3.0.1.0"
          }
        }
      },
      "Microsoft.NETCore.Platforms/1.1.0": {},
      "NETStandard.Library/2.0.3": {
        "dependencies": {
          "Microsoft.NETCore.Platforms": "1.1.0"
        }
      },
      "Newtonsoft.Json/12.0.3": {
        "runtime": {
          "lib/netstandard2.0/Newtonsoft.Json.dll": {
            "assemblyVersion": "12.0.0.0",
            "fileVersion": "12.0.3.23909"
          }
        }
      },
      "Nito.AsyncEx/5.1.0": {
        "dependencies": {
          "Nito.AsyncEx.Context": "5.1.0",
          "Nito.AsyncEx.Coordination": "5.1.0",
          "Nito.AsyncEx.Interop.WaitHandles": "5.1.0",
          "Nito.AsyncEx.Oop": "5.1.0",
          "Nito.AsyncEx.Tasks": "5.1.0",
          "Nito.Cancellation": "1.1.0"
        }
      },
      "Nito.AsyncEx.Context/5.1.0": {
        "dependencies": {
          "Nito.AsyncEx.Tasks": "5.1.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.AsyncEx.Context.dll": {
            "assemblyVersion": "5.1.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "Nito.AsyncEx.Coordination/5.1.0": {
        "dependencies": {
          "Nito.AsyncEx.Tasks": "5.1.0",
          "Nito.Collections.Deque": "1.1.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.AsyncEx.Coordination.dll": {
            "assemblyVersion": "5.1.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "Nito.AsyncEx.Interop.WaitHandles/5.1.0": {
        "dependencies": {
          "Nito.AsyncEx.Tasks": "5.1.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.AsyncEx.Interop.WaitHandles.dll": {
            "assemblyVersion": "5.1.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "Nito.AsyncEx.Oop/5.1.0": {
        "dependencies": {
          "Nito.AsyncEx.Coordination": "5.1.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.AsyncEx.Oop.dll": {
            "assemblyVersion": "5.1.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "Nito.AsyncEx.Tasks/5.1.0": {
        "dependencies": {
          "Nito.Disposables": "2.2.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.AsyncEx.Tasks.dll": {
            "assemblyVersion": "5.1.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "Nito.Cancellation/1.1.0": {
        "dependencies": {
          "Nito.Disposables": "2.2.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.Cancellation.dll": {
            "assemblyVersion": "1.1.0.0",
            "fileVersion": "1.1.0.0"
          }
        }
      },
      "Nito.Collections.Deque/1.1.0": {
        "runtime": {
          "lib/netstandard2.0/Nito.Collections.Deque.dll": {
            "assemblyVersion": "1.1.0.0",
            "fileVersion": "1.1.0.0"
          }
        }
      },
      "Nito.Disposables/2.2.0": {
        "dependencies": {
          "System.Collections.Immutable": "1.4.0"
        },
        "runtime": {
          "lib/netstandard2.0/Nito.Disposables.dll": {
            "assemblyVersion": "2.2.0.0",
            "fileVersion": "2.2.0.0"
          }
        }
      },
      "PowerShellStandard.Library/5.1.0": {
        "runtime": {
          "lib/netstandard2.0/System.Management.Automation.dll": {
            "assemblyVersion": "3.0.0.0",
            "fileVersion": "5.1.0.0"
          }
        }
      },
      "System.Collections.Immutable/1.4.0": {
        "runtime": {
          "lib/netstandard2.0/System.Collections.Immutable.dll": {
            "assemblyVersion": "1.2.2.0",
            "fileVersion": "4.6.25519.3"
          }
        }
      },
      "PolygonIo.Api/1.0.0": {
        "dependencies": {
          "Flurl": "3.0.1",
          "Newtonsoft.Json": "12.0.3"
        },
        "runtime": {
          "PolygonIo.Api.dll": {}
        }
      }
    }
  },
  "libraries": {
    "PolygonIo.PowerShell/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    },
    "Flurl/3.0.1": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-i7CuPSikVroBaWG8sPvO707Ex9C6BP5+r4JufKNU1FGMmiFgLJvNo1ttUg6ZiXIzUNknvIb1VUTIO9iEDucibg==",
      "path": "flurl/3.0.1",
      "hashPath": "flurl.3.0.1.nupkg.sha512"
    },
    "Microsoft.NETCore.Platforms/1.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==",
      "path": "microsoft.netcore.platforms/1.1.0",
      "hashPath": "microsoft.netcore.platforms.1.1.0.nupkg.sha512"
    },
    "NETStandard.Library/2.0.3": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==",
      "path": "netstandard.library/2.0.3",
      "hashPath": "netstandard.library.2.0.3.nupkg.sha512"
    },
    "Newtonsoft.Json/12.0.3": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-6mgjfnRB4jKMlzHSl+VD+oUc1IebOZabkbyWj2RiTgWwYPPuaK1H97G1sHqGwPlS5npiF5Q0OrxN1wni2n5QWg==",
      "path": "newtonsoft.json/12.0.3",
      "hashPath": "newtonsoft.json.12.0.3.nupkg.sha512"
    },
    "Nito.AsyncEx/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-KOahZdW0fRLnoym5lGtyWpFUZ94I5s0wLgYF5RHhK8RUhXpCiEX+185qjNvkse3H9WJV2/pFfPlKZHv8Eej7Hw==",
      "path": "nito.asyncex/5.1.0",
      "hashPath": "nito.asyncex.5.1.0.nupkg.sha512"
    },
    "Nito.AsyncEx.Context/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-EE7M37c5E/kvulzEkpUR6v1AnK34b2wysOLJHSjl78p/3hL7grte0XCPRqCfLZDwq98AD9GHMTCRfZy7TEeHhw==",
      "path": "nito.asyncex.context/5.1.0",
      "hashPath": "nito.asyncex.context.5.1.0.nupkg.sha512"
    },
    "Nito.AsyncEx.Coordination/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-Nv+oA+cSxidjOImiKcz2FJgMIDxiK0A6xormKmsUklUBjTNqQpjtdJsACMgTQG56PkTHdbMi5QijPTTUsmcCeg==",
      "path": "nito.asyncex.coordination/5.1.0",
      "hashPath": "nito.asyncex.coordination.5.1.0.nupkg.sha512"
    },
    "Nito.AsyncEx.Interop.WaitHandles/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-wFm3lrXXNPBtZHjLI21xhcADoh5CzO5KKNO38ybLO/CcL9zMUWWfsNiAFbw8JGp/wHoxhfdEUlThBnY3XaLR/w==",
      "path": "nito.asyncex.interop.waithandles/5.1.0",
      "hashPath": "nito.asyncex.interop.waithandles.5.1.0.nupkg.sha512"
    },
    "Nito.AsyncEx.Oop/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-J5DVgQrmE9qMNhK2eEMsuEw7V7cw7MIPrv3jqqQWolzDXkOxJFFYKUK+4dnC6UAEmum3xRVD2oBAoXg0vdYDDQ==",
      "path": "nito.asyncex.oop/5.1.0",
      "hashPath": "nito.asyncex.oop.5.1.0.nupkg.sha512"
    },
    "Nito.AsyncEx.Tasks/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-tU3Ib4zs8ivM+uS8n7F7ReWZlA3mODyLqwPE+v+WJI94hZ8xLXl+a9npfj/IcmeXo9a6fGKLWkswKQHOeTWqwA==",
      "path": "nito.asyncex.tasks/5.1.0",
      "hashPath": "nito.asyncex.tasks.5.1.0.nupkg.sha512"
    },
    "Nito.Cancellation/1.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-BSezc6jsEEAoa8UtVjQ6Qr/D5xX+FozlDKFHAvDeTv24I7ZZmmfbFxEmdjaSLnrboz1WMRjUKCQwZw7Gf4+WcA==",
      "path": "nito.cancellation/1.1.0",
      "hashPath": "nito.cancellation.1.1.0.nupkg.sha512"
    },
    "Nito.Collections.Deque/1.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-RXHe531Oaw2IathDr0Q2kbid0iuudBxtgZsfBZ2eUPuFI8I1P7HMiuUeaIefqYykcDYFTDQsFAPAljduIjihLA==",
      "path": "nito.collections.deque/1.1.0",
      "hashPath": "nito.collections.deque.1.1.0.nupkg.sha512"
    },
    "Nito.Disposables/2.2.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-QcL+uBwUCEoK8GKp/WzjdCiG8/3G1WLlVNJgLJUNG7bIIVAcEV+Mro4s53VT4Nd8xMSplv0gy+Priw44vRvLaA==",
      "path": "nito.disposables/2.2.0",
      "hashPath": "nito.disposables.2.2.0.nupkg.sha512"
    },
    "PowerShellStandard.Library/5.1.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-iYaRvQsM1fow9h3uEmio+2m2VXfulgI16AYHaTZ8Sf7erGe27Qc8w/h6QL5UPuwv1aXR40QfzMEwcCeiYJp2cw==",
      "path": "powershellstandard.library/5.1.0",
      "hashPath": "powershellstandard.library.5.1.0.nupkg.sha512"
    },
    "System.Collections.Immutable/1.4.0": {
      "type": "package",
      "serviceable": true,
      "sha512": "sha512-71hw5RUJRu5+q/geUY69gpXD8Upd12cH+F3MwpXV2zle7Bqqkrmc1JblOTuvUcgmdnUtQvBlV5e1d6RH+H2lvA==",
      "path": "system.collections.immutable/1.4.0",
      "hashPath": "system.collections.immutable.1.4.0.nupkg.sha512"
    },
    "PolygonIo.Api/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    }
  }
}
c# visual-studio powershell msbuild powershell-cmdlet
1个回答
0
投票

不,除非您正在构建独立的应用程序,否则依赖项不会构建到单个文件中。当您构建 C# 项目时,通常会将 NuGet 依赖项复制到您的

bin
文件夹中。然而,这不是你道路的一部分。您可以尝试直接从
bin
文件夹加载和调用 C# cmdlet,但还有更好的方法!

我的建议是创建一个模块清单(

psd1
)文件并导入它而不是直接导入库。您可以从 New-ModuleManifest 开始生成文件。您要特别注意的是
RootModule
表示您的原始库,
RequiredAssemblies
表示您的 NuGet 依赖项。请记住,路径是相对于清单文件的!

© www.soinside.com 2019 - 2024. All rights reserved.