You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fedora Linux下导入F#编写的PowerShell模块遇程序集加载错误

F#编写PowerShell模块导入问题排查

问题概述

尝试创建并导入F#编写的PowerShell模块,但导入编译后的DLL时遇到依赖缺失和程序集重复加载的问题。

F#模块代码(Library.fs)

// Library.fs
namespace HiThereFsharp

open System
open System.Management.Automation

[<Cmdlet(VerbsCommon.Get, "Greeting")>]
type HelloWorld() =
    inherit PSCmdlet()

    let mutable name = ""

    [<Parameter(ValueFromPipeline = true)>]
    member this.Name
        with get () = name
        and set (value) = name <- value

    override this.ProcessRecord() =
        let whoDat =
            if String.IsNullOrEmpty(name) then
                "World"
            else
                name
        
        let greeting = "Hello " + whoDat
        this.WriteObject(greeting)

构建与导入步骤及错误

1. 初始构建与依赖缺失错误

使用dotnet build成功构建,输出DLL路径:

/home/deca/RiderProjects/HiThereFsharp/HiThereFsharp/bin/Debug/net8.0/HiThereFsharp.dll

执行导入命令:

Import-Module /home/deca/RiderProjects/HiThereFsharp/HiThereFsharp/bin/Debug/net8.0/HiThereFsharp.dll

报错:

Import-Module: Could not load file or assembly 'FSharp.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

2. 自包含发布后的重复加载错误

为解决FSharp.Core.dll缺失问题,执行自包含发布:

dotnet publish -c Debug -o ./out -r linux-x64 --self-contained true

发布后out目录包含FSharp.Core.dll,再次导入:

Import-Module ./out/HiThereFsharp.dll

报错:

Import-Module: Could not load file or assembly 'HiThereFsharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Assembly with same name is already loaded

尝试移除模块无效:

Remove-Module HiThereFsharp

结果:

Remove-Module: No modules were removed. Verify that the specification of modules to remove is correct and those modules exist in the runspace.

强制导入仍报相同错误:

Import-Module -Force ./out/HiThereFsharp.dll

环境信息

  • PowerShell版本:7.5.0
  • F#版本:8.0
  • PowerShellStandard.Library:5.1.1

C#版本对比(可正常运行)

C#代码

namespace HiThere;
using System;
using System.Management.Automation;

[Cmdlet(VerbsCommon.Get, "Greeting")]
public class HelloWorld : PSCmdlet
{
    [Parameter(ValueFromPipeline = true)]
    public string Name { get; set;}
    protected override void ProcessRecord()
    {
    var whoDat = string.IsNullOrEmpty(Name) ? "World" : Name;
    var greeting = "Hello " + whoDat;
    WriteObject(greeting);
    }
}

构建与运行结果

PS ~/Documents/Programming/Csharp/HiThere> dotnet build
MSBuild version 17.8.5+b5265ef37 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  HiThere -> /home/deca/Documents/Programming/Csharp/HiThere/bin/Debug/net8.0/HiThere.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.95
PS ~/Documents/Programming/Csharp/HiThere> Import-Module /home/deca/Documents/Programming/Csharp/HiThere/bin/Debug/net8.0/HiThere.dll           
PS ~/Documents/Programming/Csharp/HiThere> Get-Greeting -Name ""
Hello World

解决思路建议

  1. 处理FSharp.Core依赖问题
    • 无需使用自包含发布,在.fsproj文件中添加<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>,执行dotnet build后FSharp.Core.dll会自动复制到输出目录,直接导入该目录下的DLL即可。
  2. 解决重复加载问题
    • 重启PowerShell会话:已加载的程序集无法在当前会话中卸载,Remove-Module仅移除模块定义,无法清除已加载的程序集。
    • 确保每次导入同一目录下的DLL,避免混合导入不同路径/版本的同名程序集。
  3. 项目配置检查
    • 对比C#项目的.csproj配置,确认F#项目是否缺少依赖复制相关的默认配置。
    • 验证PowerShellStandard.Library版本与PowerShell 7.5.0的兼容性,当前5.1.1版本适配正常,可排除版本冲突问题。

内容的提问来源于stack exchange,提问作者decabytes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:29:50