使用 CodePackageDebugParameters 调试服务结构

Service Fabric Debugging with CodePackageDebugParameters

在 VS 中创建新的服务结构应用程序时,默认模板会向脚本添加调试魔法,以便可以调试服务结构中的应用程序 运行。 powershell 脚本将 _WFDebugParams_ 作为应用程序参数传递给集群。我想不出让它在代码中工作。我目前使用的代码是

var debugParameters = new CodePackageDebugParameters(
   "MyApplicationPkg", 
   "Code", null, null, 
   "Main", 
   @"C:\Program Files\Microsoft Visual Studio 15.0\Common7\IDE\Remote Debugger\x64\VsDebugLaunchNotify.exe", 
   "{625F9A8A-16A1-42E6-948F-D5D9AF04F1AB} -p[ProcessId] - tid [ThreadId]",
    null, null, null, null, null, null, null);
nameValueCollection["_WFDebugParams_"] = CodePackageDebugParameters.GetDebugParameters(new[] { debugParameters });

await app.CreateApplicationAsync(new ApplicationDescription(ApplicationName, ApplicationTypeName, ApplicationTypeVersion.ToString(), nameValueCollection)).ConfigureAwait(false);

我的机器上只安装了带有远程调试器工具的 VS2017。我的问题如下:

我遇到的另一个问题是 CodePackageDebugParameters 基本上无法使用。我必须将整个 class 定义复制粘贴到我的项目中,因为构造函数和所有属性都是内部的。

Service Fabric Tools 使用此 WFDebugParams 参数,因此当正在调试的 Service Fabric 应用程序可以附加到 VS 调试器时。它不适合最终用户使用。

综上所述,GUID 是 Visual Studio 调试器设置的命名管道的关联 ID。 Service Fabric Cluster 稍后使用它使用它应该自动附加到的进程 ID 和线程 ID ping VS。

这里是你在 Visual Studio 中按 F5 时发生的事情的更详细解释(请注意,这是对我的本地开发环境进行调查的结果,可能不是 100% 准确的部分VS 或 SF 的文档未涵盖其中的内容):

  1. Service Fabric Extension 扫描即将调试的解决方案并列出要部署到本地群集的所有 SF 服务
  2. 对于每个服务,它 运行s StartListener 方法使用未记录的调试器接口 IVsDebugLaunchNotifyListenerFactory110。因此,如果项目中有 x 个服务,则会启动 x 个通知程序
  3. 这又会创建一个名为 MicrosoftVisualStudioProcessLaunchNotify_{<<GUID>>} 的命名管道,其中为每个函数调用生成 GUID 部分
  4. Service Fabric Extension 部署 SF 应用程序,如问题中所示,在 _WFDebugParams_ 中传递 GUID。
  5. 在服务启动期间,SF 使用 _WFDebugParams_ 到 运行 VsDebugLaunchNotify.exe 传递已启动服务的 GUID、ProcessID 和 ThreadID 调试器应自动附加到。

注意:看来 StartListener 只能在 Visual Studio 扩展中使用。