gpt4 book ai didi

c# - Powershell 包丢失/无法在发布时工作

转载 作者:行者123 更新时间:2023-12-05 02:35:01 25 4
gpt4 key购买 nike

我有一个控制台应用程序项目,我在其中使用 powershell SDK,它在调试时工作得很好,但在发布时,除了 powershell 之外的所有东西都可以工作。

我发现唯一有效的设置是将项目发布为依赖于框架和可移植的项目。或者至少它可以在我的电脑上运行,在其他计算机上它说缺少 dotnet 运行时,即使通过提供的链接安装也是如此。

使用独立的 .net6-windows 和 win-x86 不起作用。不太确定哪里出了问题?我试过清理项目、解决方案、重新启动 visual studio 和我的电脑。在调试中一切都按预期工作,但当我发布时,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>

Profile settings

enter image description here

编辑:

我将我的 powershell 处理程序更改为使用 .EndInvoke(),现在我终于得到了正确的错误!我发现了一个提到错误 https://github.com/PowerShell/PowerShell/issues/7909 的问题但是安装 Microsoft.Management.Infrastructure 并没有解决问题,我安装了 2.0.0 版。如果我尝试安装版本 1,我会收到一条错误消息,指出 System.Management.Automation 包需要更高版本 (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();

最佳答案

最终解决我问题的是这个答案:Dotnet publish not publishing DLL to publish directory

我将目标运行时更改为 win10-x86 并且成功了!

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

关于c# - Powershell 包丢失/无法在发布时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70643435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com