This is a similar piece to another post of mine from a few years ago Run a Windows Azure cloud service locally without the Azure compute emulator
So you’re working on a Service Fabric application which has an ASP.NET Web api host project. I find the debugging experience painful, for two reasons:
- Time to start debugging the project is a minimum of 45 seconds, every time, because the app gets deployed to a local service fabric cluster which takes forever.
- You need to remember to run Visual Studio as administrator in order for the above local deployment to succeed.
If either of these things bug you, then here’s a possible solution. Once we’re done you’ll be able to set the Web project in your solution as the StartUp project instead of the Service Fabric application, for much faster debugging, and you’ll no longer need to run VS as admin.
First, change the Program.cs in the Web project:
private static void Main() { if (UseServiceFabric()) { StartServiceFabric(); } else { StartWebHost(); } } private static bool UseServiceFabric() { var webHostBuilder = new WebHostBuilder(); var environment = webHostBuilder.GetSetting("environment"); return environment != "Development"; } private static void StartWebHost() { var builder = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>(); var host = builder.Build(); host.Run(); } private static void StartServiceFabric() { try { // The ServiceManifest.XML file defines one or more service type names. // Registering a service maps a service type name to a .NET type. // When Service Fabric creates an instance of this service type, // an instance of the class is created in this host process. ServiceRuntime.RegisterServiceAsync("Web1Type", context => new WebHost(context)).GetAwaiter().GetResult(); ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Web1).Name); // Prevents this host process from terminating so services keeps running. Thread.Sleep(Timeout.Infinite); } catch (Exception e) { ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString()); throw; } }
If the ASPNETCORE_ENVIRONMENT setting is Development, then it won’t use Service Fabric at all and will just use a plain ol’ ASP.NET Core WebHostBuilder to start the web host.
You’ll also need to change your Debug target to be the Web project, instead of IISExpress, via the VS Standard toolbar.
And just like that the app startup time has shrunk from around 40 seconds to around 5 seconds. Or from an unbearable 100 seconds for the application the team I just joined is working on.
Hi Matt!
This sounds super useful. However, I’m struggling with setting this up for Web API. Do you have experience in that?
Yes, my instructions in this post are for Web API.
hi, how do I run console app with in my service fabric application with out deploying it to service fabric cluster
I have no idea, I haven’t worked with Service Fabric for a few years.
Its not as simple as you pointed out. We need to read configuration in the web host etc which is not covered. this would work only with a dumbass service which hard-codes everything instead of loading it from appsettings.json.
I am certain that when I wrote this blog post back in 2017 I was not working with a “dumbass service” which hardcoded everything. It was an ASP.NET Core Web API project which targetted .NET Framework, and of course it loaded config from appsettings.json.
I worked on that solution for about a year but I haven’t touched Service Fabric since.
Maybe I left out the Configuration stuff from this blog post for brevity. I’m sure if you call config.AddJsonFile() it’ll work.