Search code examples
restful-architectureazure-service-fabric

Can reliable service be directly RESTful also (not a separate owin based controller) in service fabric?


Visual studio 2015 Update 2 provides a template for authoring owin based controller in the service fabric. The created structure shows you a stateless reliable service and the owin based controller as 2 different C# classes. This works. In this case, reliable service class simply registers a http listener and all the calls will be routed to controller class. In a way, stateless reliable service is out of the picture as soon as it is created and is only useful during startup of the service.

I was expecting to see reliable service and the owin based controller, one and the same. The current structure seems like a patch work.

If I were to change the stateless reliable service to stateful reliable service, I can not really do anything with the stateful service because my requests are routed to the controller. If I were to interact in my controller with state manager, then I have to get the reference to the stateful service and then do my stuff. Feels awkward.

Is there a cleaner way of doing this?


Solution

  • The service is your application container. The service base classes (StatefulService and StatelessService) are your entry points to your application. You can party on directly in those classes and have all your code live in there, or you can use that entry point to bootstrap some other application framework, like ASP.NET MVC, or Reliable Actors, or whatever. Service Fabric provides all your platform dependencies through the service base classes (e.g., ServiceContext, IReliableStateManager for stateful services, etc.), and again you can either use them directly in those classes if that's where your code is, or you can pass those dependencies along to some other application framework.

    For ASP.NET, we use the service to bootstrap an OWIN-based web server (Katana, Kestrel, WebListener), to which we then provide application middleware (MVC, StaticFiles, etc.). You can then pass all the platform dependencies on to the middleware using your favorite dependency injection framework (Unity, Autofac, Ninject, or ASP.NET Core's built-in dependency injection framework).

    Here is an example of a stateful service that does this with Unity and Katana: https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/tree/master/Services/WordCount/WordCount.Service