How to see if running under service fabric

冷暖自知 提交于 2019-12-04 00:29:05

Check Service Fabric Environment Variables:

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

Source: from @mkosieradzki GitHub Issue

user1496062

This is what i have come up with but something without an exception would be better (and note some projects use Core)

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

eg.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!