polina-c
polina-c

Reputation: 7077

Run Web app separately from Service Fabric

I have Service Fabric and web service in it. If I run the Service Fabric locally, from Visual Studio, I can debug my service, and it is very convenient.

However, it takes a lot of time to deploy my code changes to local fabric. I am sure there should be an option to start my service separately from Service Fabric. It seems I need to update my method 'Main', so that it starts the service differently in case of dev environment.

Any ideas what exactly should be changed?

Upvotes: 1

Views: 105

Answers (2)

polina-c
polina-c

Reputation: 7077

Oleg Karasik's response helped to shape the solution. I do not use any Service Fabric specific features yet, so it worked out.

The only code I needed to modify was the class Program:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.ServiceFabric.Services.Runtime;
using System;
using System.Diagnostics;
using System.Threading;

namespace MyNameSpace
{
    internal static class Program
    {
        private static void Main()
        {
            if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("Fabric_ApplicationName")))
            {
                StartInIISExpress();
            }
            else
            {
                StartInServiceFabric();
            }
        }

        private static void StartInIISExpress()
        {
            WebHost.CreateDefaultBuilder()
                    .UseStartup<Startup>()
                    .Build().Run();
        }

        private static void StartInServiceFabric()
        {
            < original code of the method Main >                   
        }
    }
}

Upvotes: 1

Oleg Karasik
Oleg Karasik

Reputation: 969

Does your service use any of Service Fabric features (Remoting? Custom Listeners? etc.).

If yes then your service cannot be run without Service Fabric runtime.

In case you service is an ASP.NET Core service configured via WebHost then you can try the following approach:

  1. Separate the code that configures WebHost into separate static method. Use this method to initialize WebHost inside Service Fabric service.
  2. In the Program.Main you check for Fabric_ApplicationName environment variable (this can be done using Environment.GetEnvironmentVariable method).

    This variable is defined by Service Fabric runtime so if it is defined then you code is running inside Service Fabric.

    If the Fabric_ApplicationName variable is defined then just continue with the Service Fabric service initialization code, otherwise use previously defined static method to initialize and instance of WebHost and run it directly from the Program.Main.

I am not sure whether this is what you were looking for, so I you have any additional questions - please ask.

Hope it helps.

Upvotes: 2

Related Questions