How to see if running under service fabric

不羁的心 提交于 2019-12-05 14:43:39

问题


I sometimes run projects locally out of visual studio is there a better way to detect if I'm hosted by SF rather than the exception. I can see possibly the path or entry assembly but there must be a better way.

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);
}

回答1:


Check Service Fabric Environment Variables:

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

Source: from @mkosieradzki GitHub Issue




回答2:


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
}


来源:https://stackoverflow.com/questions/39781817/how-to-see-if-running-under-service-fabric

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