【问题标题】:How to see if running under service fabric如何查看是否在服务结构下运行
【发布时间】:2021-05-29 07:59:40
【问题描述】:

我有时会在 Visual Studio 之外本地运行项目,有没有更好的方法来检测我是否由 SF 托管而不是异常。我可以看到路径或入口程序集,但必须有更好的方法。

try
{
    ServiceRuntime.RegisterServiceAsync("FisConfigUIType",
        context = > new WebHost < Startup > (context, loggerFactory, "ServiceEndpoint", Startup.serviceName)).GetAwaiter().GetResult();
    Thread.Sleep(Timeout.Infinite);
}
catch (FabricException sfEx)
{
    RunLocal(args, loggerFactory);
}

【问题讨论】:

  • 如果您与我们分享您为什么需要知道,我可能会帮助我们提出更好的解决方案。
  • 我们在 VS 中使用 F5 本地运行解决方案进行调试,而不是部署到需要很长时间的集群。
  • 不知新的2.2 SDK能否解决你的问题。它引入了一个很棒的功能:使用 1-Node 集群进行本地调试。您可以查看发行说明以获取更多信息blogs.msdn.microsoft.com/azureservicefabric/2016/09/13/…
  • 它有帮助,但即使是一个小型核心应用程序的打包也远远超过 100 Meg,所以 id 仍然像某种 ISSFHosted 没有处理的异常
  • 好问题,opened a GitHub issue 希望他们能改进这一点。

标签: c# azure-service-fabric


【解决方案1】:

查看Service Fabric Environment Variables:

var sfAppName = Environment.GetEnvironmentVariable("Fabric_ApplicationName");
var isSf = sfAppName != null;

来源:来自@mkosieradzki GitHub Issue

【讨论】:

  • 这应该被赞成,它还具有在入口库中没有依赖关系的好处。
  • 正确答案!
【解决方案2】:

这是我想出的,但毫无例外会更好(注意一些项目使用核心)

static bool IsSFHosted()
{
    try
    {
        FabricRuntime.GetNodeContext();
        return true;
    }
    catch (FabricException sfEx) when (sfEx.HResult == -2147017661 || sfEx.HResult == -2147017536 || sfEx.InnerException?.HResult == -2147017536)
    {
        return false;
    }
}

例如。

var isSFHosted = IsSFHosted();
var servicesPreRegister = builder.GetPreRegisterServicesForStore(node: node, security: false);

if (isSFHosted)
{
    ServiceRuntime.RegisterServiceAsync("DeliveriesWriteType",
        context => new WebAPI(context, loggerFactory, servicesPreRegister)).GetAwaiter().GetResult();
}
else
{
    loggerFactory.AddConsole();
    // run with local web listener with out SF
}

【讨论】:

  • 您能否解释一下这究竟是如何工作的?
  • If ( IsSFHosted == false) { starviatweblistener( ..) } // 显然不适用于有状态服务
  • GetNodeContext 不会在本地调试集群和本地集群上引发异常。不过,我不明白这是如何工作的。我必须更改代码才能使其正常工作。 var node = FabricRuntime.GetNodeContext(); return node.IPAddressOrFQDN != "localhost";
  • 在上面添加到控制台应用程序以进行尝试。我得到一个 BadImageFormatException,我什至无法用 try-catch 块捕获它: System.BadImageFormatException: 'Could not load file or assembly 'System.Fabric, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one其依赖项。试图加载格式不正确的程序。'
  • @cassandrad 对我们来说主要用途是我们在未托管在 SF 时重用服务代码,但最好的用途是您可以通过部署到本地集群进行本地调试。
猜你喜欢
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 2013-04-07
  • 2021-08-14
  • 2012-04-09
  • 1970-01-01
相关资源
最近更新 更多