使用.NET编写Power Automate Desktop的自定义模块?
解读
国内企业在RPA(机器人流程自动化)落地时,常把Power Automate Desktop(PAD)作为“平民开发者”工具,但标准动作库往往无法满足内网OA、金蝶/用友、信创Linux环境等本土化场景。面试官抛出此题,核心想验证四点:
- 是否真正理解PAD的“自定义模块”在.NET技术栈里的定位——它既不是传统DLL,也不是简单COM,而是符合PAD C# Scripting规范的NuGet包;
- 能否把.NET 6+跨平台编译、强命名、依赖注入、本地化资源等企业级要求,与PAD的“零安装、XCOPY部署”约束同时满足;
- 是否熟悉国内常见“信创+内网”离线环境:不能上网拉包、必须用国产麒麟OS、CPU可能是龙芯/兆芯,此时如何提前做自包含发布、剪裁运行时;
- 有没有实际踩过坑:PAD 2.27之后强制AppContext切换、WinForms弹窗被Session0隔离、国产WPS进程名随机导致Selector失效等。
回答时若只给出“新建类库→编译→拖进去”,会被直接判为“玩具级”;必须展示出对生命周期、线程模型、版本兼容、离线分发、国产CPU适配的闭环思考。
知识点
- PAD自定义模块规范:
- 必须 targeting net6.0-windows/net472,输出为AnyCPU或x64;
- 入口类标注
[Action],[ActionInput]/[ActionOutput]绑定PAD变量; - 支持
IActionExecutionCallback拿到PAD的CancellationToken,实现“停止按钮”即时响应; - 支持
[ActionIcon]嵌入Base64 PNG,避免外网拉图失败。
- 企业级打包:
- 用
dotnet pack生成.nupkg,在.nuspec里写<files src="runtimes\win-x64\native\**" target="lib\net6.0" />,把国产CPU专用so/dll按 RID 分离; - 强命名密钥用国密SM2证书,满足央国企代码签名合规;
- 写
customModule.settings.json,关闭“自动更新”,防止内网环境反复弹更新失败。
- 用
- 离线部署脚本:
- 用
dotnet publish -c Release -r linux-mips64 --self-contained true -p:PublishTrimmed=true生成单文件,体积<30 MB; - 写PowerShell一键注册:
padcli module add --path .\MyModule.zip --offline,避开市场下载。
- 用
- 常见坑:
- PAD 2.31起默认
AssemblyLoadContext隔离,反射加载国产DLL失败→需AssemblyLoadContext.Default.LoadFromAssemblyPath; - 国产WPS窗口类名随机→用UIA2+
AutomationElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)); - Session0隔离导致弹窗不可见→用
Process.Start("cmd", "/c msg * ...")回显到Console。
- PAD 2.31起默认
答案
步骤一:项目骨架(Visual Studio 2022 v17.8+,SDK-style)
dotnet new classlib -n Pad.Custom.Citic -f net6.0-windows
cd Pad.Custom.Citic
dotnet add package Microsoft.PowerPlatform.PowerAutomateDesktop.Actions.Sdk --version 1.0.10
在CiticActions.cs中:
using Microsoft.PowerPlatform.PowerAutomateDesktop.Actions.SDK;
using Microsoft.PowerPlatform.PowerAutomateDesktop.Actions.SDK.Attributes;
namespace Pad.Custom.Citic
{
[Action(Id = "Citic.KingdeeVoucherImport")]
public class KingdeeVoucherImport : ActionBase
{
[ActionInput(Label = "Excel路径", Description = "内网共享盘路径,如\\\\192.168.0.8\\财务\\凭证.xlsx")]
public string ExcelPath { get; set; }
[ActionOutput(Label = "导入结果")]
public bool Success { get; set; }
public override void Execute(ActionContext context)
{
context.Logger.LogInfo($"开始导入{ExcelPath}");
// 国产龙芯环境需用EPPlus.Core.LoongArch NuGet
using var package = new ExcelPackage(new FileInfo(ExcelPath));
var sheet = package.Workbook.Worksheets[0];
// 省略业务逻辑……
Success = true;
context.Logger.LogInfo("导入完成");
}
}
}
步骤二:强命名+国密签名
dotnet add package System.Security.Cryptography.Cng --version 6.0.0
sn -k citic.snk
dotnet build -c Release -p:SignAssembly=true -p:AssemblyOriginatorKeyFile=citic.snk
signtool sign /sha1 5f5a*** /t http://timestamp.digicert.com bin\Release\net6.0-windows\Pad.Custom.Citic.dll
步骤三:打包并生成离线安装包
<!-- Pad.Custom.Citic.nuspec -->
<package>
<metadata>
<id>Pad.Custom.Citic</id>
<version>1.0.0</version>
<authors>中信信息</authors>
<description>国产CPU适配的金蝶凭证导入模块</description>
<dependencies>
<group targetFramework="net6.0-windows"/>
</dependencies>
</metadata>
<files>
<file src="bin\Release\net6.0-windows\**\*.dll" target="lib\net6.0" />
<file src="customModule.settings.json" target="contentFiles\any\any" />
</files>
</package>
dotnet pack Pad.Custom.Citic.csproj -c Release -o nupkgs
Compress-Archive -Path nupkgs,install.ps1 -DestinationPath Citic.PAD.Module.zip
install.ps1内容:
$padModulePath = "$env:LOCALAPPDATA\Microsoft\Power Automate Desktop\Modules"
Expand-Archive -Path Citic.PAD.Module.zip -DestinationPath $padModulePath -Force
padcli module add --path "$padModulePath\nupkgs\Pad.Custom.Citic.1.0.0.nupkg" --offline
步骤四:国产麒麟V10(龙芯)适配
dotnet publish -c Release -r linux-loongarch64 --self-contained true -p:PublishTrimmed=true -p:TrimMode=partial
scp ./bin/Release/net6.0/linux-loongarch64/publish/* root@kylin:/opt/pad/modules/
ssh root@kylin 'padcli module add --path /opt/pad/modules/Pad.Custom.Citic.dll --offline'
步骤五:PAD流程调用
在PAD设计器→自定义操作→刷新模块→拖入“Citic.KingdeeVoucherImport”,填写\\192.168.0.8\财务\凭证.xlsx,输出变量%Success%,后续用If条件判断即可。
拓展思考
- 版本兼容策略:央企总部已统一PAD 2.31,但分公司仍有2.14,如何在一个.nupkg里同时支持.net472与net6.0-windows?答:用多目标框架
<TargetFrameworks>net472;net6.0-windows</TargetFrameworks>,在代码里#if NET472做兼容,打包时自动拆双lib。 - 敏感数据保护:凭证Excel含个人银行卡号,需国密SM4加密。可在
[ActionInput]上加[Encrypted]特性,利用PAD内置的DataProtectionScope.LocalMachine,但国产麒麟无DPAPI,需替换为Tongsuo开源SM4实现,并在模块初始化时注册CryptoProvider。 - 性能遥测:内网不能连Azure Application Insights,可写
ActionTelemetryClient把Trace推到Kafka(国产中创中间件),再用Flink实时计算RPA耗时,实现“数字员工”SLA看板。 - 热更新:财务制度一年三变,模块需热插拔。可把核心逻辑拆到
AssemblyLoadContext子域,用padcli module upgrade --skip-version-check实现不重启PAD进程即可加载新规则,达到“7×24”不停机。