16

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);
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
user1496062
  • 1,309
  • 1
  • 7
  • 22
  • If you share with us why do u need to know, I might help us come up with a better solution. – Rotem Varon Oct 01 '16 at 18:30
  • We run the solutions locally in VS with F5 for debug instead of deploying to a cluster which take to long . – user1496062 Oct 04 '16 at 01:04
  • I wonder if the new 2.2 SDK will solve your problem. It introduced a great features: Local debugging with 1-Node cluster. You can check the release notes for more info https://blogs.msdn.microsoft.com/azureservicefabric/2016/09/13/release-of-service-fabric-sdk-2-2-207-and-runtime-5-2-207/ – Rotem Varon Oct 04 '16 at 03:18
  • It helps but packaging even a small core app is well over 100 Meg so id still like some sort of ISSFHosted without a handled exception – user1496062 Oct 05 '16 at 04:09
  • Great question, [opened a GitHub issue](https://github.com/Azure/service-fabric-issues/issues/656) hoping they will improve this. – spottedmahn Nov 17 '17 at 18:35

2 Answers2

22

Check Service Fabric Environment Variables:

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

Source: from @mkosieradzki GitHub Issue

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
5

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
}
spottedmahn
  • 14,823
  • 13
  • 108
  • 178
user1496062
  • 1,309
  • 1
  • 7
  • 22
  • Could you please explain how exactly this intended to work? – cassandrad Oct 11 '16 at 09:00
  • If ( IsSFHosted == false) { starviatweblistener( ..) } // obviously does not work for stateful services – user1496062 Oct 20 '16 at 06:24
  • GetNodeContext does not throw an exception on local debug cluster and on on-premises cluster. Still, I don't understand how this intended to work. I had to change the code to get it working. var node = FabricRuntime.GetNodeContext(); return node.IPAddressOrFQDN != "localhost"; – cassandrad Oct 20 '16 at 08:08
  • Added above to a console application to try it. I get a BadImageFormatException which I can't even catch with a try-catch block: System.BadImageFormatException: 'Could not load file or assembly 'System.Fabric, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. An attempt was made to load a program with an incorrect format.' – Nizar Qamar Apr 13 '17 at 09:53
  • you still need to compile for 64bit for the SF code – user1496062 Apr 28 '17 at 12:34
  • 1
    @cassandrad the main use for us is we re-use the service code when not hosted in SF but the best use is you can debug locally with deploying to the local cluster. – user1496062 May 08 '17 at 08:33