簡體   English   中英

Powershell package 缺失/無法發布

[英]Powershell package missing/not working on release

我有一個控制台應用程序項目,我正在使用 powershell SDK,它在調試時工作得非常好,但在發布時,除了 powershell 之外的所有東西都可以工作。

我發現它起作用的唯一設置是在將項目發布為依賴於框架且可移植時。 或者至少它可以在我的電腦上運行,在其他計算機上它說缺少 dotnet 運行時,即使通過提供的鏈接安裝也是如此。

使用自包含的 .net6-windows 和 win-x86 不起作用。 不太確定可能出了什么問題? 我已經嘗試清理項目、解決方案、重新啟動 Visual Studio 和我的 PC。 在調試中一切都按預期工作,但是當我發布時,powershell 就不起作用。

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

  <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net6.0-windows</TargetFramework>
      <UseWindowsForms>true</UseWindowsForms>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <UserSecretsId>dotnet-SocketService-04A4D71F-C305-4A5B-BD0B-529C28B25DAD</UserSecretsId>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.1" />
    <PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
    <PackageReference Include="System.Management" Version="6.0.0" />
    <PackageReference Include="System.Management.Automation" Version="7.2.1" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Speedtest\" />
  </ItemGroup>

  <ItemGroup>
    <None Update="Speedtest\speedtest.exe">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>

配置文件設置

在此處輸入圖像描述

編輯:

我將我的 powershell 處理程序更改為 use.EndInvoke(),現在我終於得到了一個正確的錯誤! 我發現一個問題提到了錯誤https://github.com/PowerShell/PowerShell/issues/7909但安裝 Microsoft.Management.Infrastructure 並沒有解決問題,我安裝了 2.0.0 版。 如果我嘗試安裝版本 1,我會收到一條錯誤消息,指出 System.Management.Automation package 需要更高版本(2.0.0)

public static string CMD(string script)
{
    string errorMsg = "";
    string output;

  
    ps.AddScript(script);

    //Make sure return values are outputted to the stream captured by C#
    ps.AddCommand("Out-String");

    PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
    ps.Streams.Error.DataAdded += (object sender, DataAddedEventArgs e) =>
    { errorMsg = ((PSDataCollection<ErrorRecord>)sender)[e.Index].ToString(); };

    IAsyncResult result = ps.BeginInvoke<PSObject, PSObject>(null, outputCollection);
    ps.EndInvoke(result);

    //while (!result.IsCompleted)
    //    Thread.Sleep(300);

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject outputItem in outputCollection)
    {
        stringBuilder.AppendLine(outputItem.BaseObject.ToString());
    }
    output = stringBuilder.ToString();

    //Clears commands added to runspace
    ps.Commands.Clear();

    if (!string.IsNullOrEmpty(errorMsg))
        return string.Empty;

    return output.Trim();
}

這是錯誤消息

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Den angivne fil blev ikke fundet.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
   at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)
   at SocketService.PowerShellHandler.CMD(String script) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Handlers\PowershellHandler.cs:line 35
   at SocketService.Classes.SystemInfo..ctor() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Classes\SystemInfo.cs:line 21
   at SocketService.CommandHandler.Initialize() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Handlers\CommandHandler.cs:line 31
   at SocketService.SocketService..ctor() in C:\Users\Blue\source\repos\SocketClientProject\Clientside\SocketService.cs:line 27
   at SocketService.WindowsBackgroundService.StartAsync(CancellationToken cancellationToken) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\WindowsBackgroundService.cs:line 17
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Program.<Main>$(String[] args) in C:\Users\Blue\source\repos\SocketClientProject\Clientside\Program.cs:line 14
   at Program.<Main>(String[] args)

[process exited with code 3762504530]

第 14 行是我調用 PowerShell.Create();

如果不顯示實際代碼並提供關於除了“它不起作用”之外正在發生的事情的更具描述性的細節,很難准確地說出這里發生了什么,但我會盡力而為。

幸運的是,根據Microsoft 的文檔,您肯定可以在自包含的 .Net 應用程序中運行 Powershell SDK。

A self-contained .NET application can use Microsoft.PowerShell.SDK to run arbitrary PowerShell functionality without depending on any external PowerShell installations or libraries.

這讓我相信您可能不會遇到 SDK 本身的問題,而是編譯器的問題。

單文件部署

我在您的屏幕截圖中注意到您正在嘗試執行單文件部署。 您可能會在這里遇到一些問題。 一是確保您沒有使用不兼容的 API 如果您在應用程序中調用其中任何一個,這可能是一個因素:

  • 匯編代碼庫
  • Assembly.EscapedCodeBase
  • 程序集.GetFile
  • 程序集.GetFiles
  • 裝配位置
  • AssemblyName.CodeBase
  • AssemblyName.EscapedCodeBase
  • Module.FullyQualifiedName
  • 模塊名稱

因為這些都不兼容單文件部署。

修剪

您可能遇到的另一個問題稱為修剪 這是編譯器在編譯時從項目中“修剪”未使用的程序集的地方,並且往往在發布運行時發生。 雖然我相信默認情況下這是關閉的,但您可以將以下內容添加到 your.csproj 文件以確保禁用修剪:

<PropertyGroup>
    <PublishTrimmed>false</PublishTrimmed>
</PropertyGroup>

優化

JIT 編譯器傾向於嘗試優化發布版本的代碼 當我們在調試模式下運行應用程序時,應用程序已針對調試代碼進行了優化。 它試圖使所有內容幾乎與您編寫的內容完全相同,但可能存在一些細微差別。 當應用程序在發布模式下運行時,運行的實際代碼在維護相同邏輯時可能會大不相同。 這通常是為了嘗試使最終組裝盡可能小而快。 您可以通過以下步驟禁用此優化:

  1. 右鍵單擊項目
  2. 點擊“屬性”
  3. Go 到“構建”
  4. 在“常規”部分下停用“優化代碼”

優化代碼

除了上面提到的可能原因之外,如果沒有看到代碼、錯誤消息或更多詳細信息,我想不出更多我能提供的東西。

最終解決我的問題的是這個答案: Dotnet publish not publishing DLL to publish directory

我將目標運行時更改為 win10-x86 並且有效!

<TargetFramework>net6.0-windows</TargetFramework>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM