Enable ASP.NET Core Api on a Service Fabric Reliable Service
Often times I need to create a quick Service Fabric reliable service demo and I find myself looking up and repeating the same steps to initialize the app. Usually I need my service to also have a http listener so I can easily access it. Since I don’t need the full blown asp.net core mvc template I start with a simple reliable service and just add the Kestrel
listener. Here is the quick start:
-
Create a new Service Fabric app
Using Visual Studio create a new Service Fabric application. When asked to configure your first project, select a reliable service (stateless or statefull, it doesn’t matter)
-
Add the following NuGet packages to your service:
Microsoft.AspNetCore.Hosting
,Microsoft.AspNetCore.Mvc.Core
,Microsoft.AspNetCore.Server.Kestrel
,Microsoft.ServiceFabric.AspNetCore.Kestrel
-
Add the
Kestrel
listenerFollowing the official documentation add the
KestrelCommunicationListener
listener to your service. There are subtle differences between statefull and stateless services so make sure to follow the documentation. For a stateless service theCreateServiceInstanceListeners
from yourStatefulService
class could look like this:... protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() { return new ServiceInstanceListener[] { new ServiceInstanceListener(serviceContext => new KestrelCommunicationListener(serviceContext, (url, listener) => new WebHostBuilder() .UseKestrel() .ConfigureServices( services => services .AddSingleton<StatelessServiceContext>(serviceContext)) .UseContentRoot(Directory.GetCurrentDirectory()) .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None) .UseStartup<Startup>() .UseUrls(url) .Build())) }; } ...
-
Add the
Startup.cs
class to configure your asp.net core hostWhile there are many configurations you may touch in your Startup.cs class, for a very basic api you only need the
Mvc
middleware. YourStartup.cs
could look like this:internal class Startup { public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); } }
-
Add a test controller
Now all you need to do is to add a dummy controller:
[ApiController] public class DummyController : ControllerBase { [HttpGet("dummy")] public async Task<ActionResult> Get() { return Ok("ok"); } }
-
Test it
Using Service Fabric reverse proxy a GET would suffice:
curl localhost:19081/Application/StatelessApi/dummy
, where
Application
= name of the application andStatelessApi
= name of the service -
(for statefull service only) Check the partitioning type
By default the partitioning scheme for a stefull service is
UniformInt64Partition
. If you don’t want to deal with multiple partitions you could easily change the partition scheme toSingleton
. In the application manifest it would look like this:<Service Name="StatefulApi" ServicePackageActivationMode="ExclusiveProcess"> <StatefulService ServiceTypeName="StatefulApiType" TargetReplicaSetSize="[StatefulApi_TargetReplicaSetSize]" MinReplicaSetSize="[StatefulApi_MinReplicaSetSize]"> <SingletonPartition /> </StatefulService> </Service>
GitHub repo with a full working example: Service Fabric - Enable ASP.NET Core Api Quick Start