2

We just stood up an on-premise MS Service Fabric cluster. I have some WebAPI's i'd like to host in it. I'm looking for resources on how to take our standard 4.5 WebAPI's and host them in Service Fabric without have to create a Service Fabric project and migrate it; that just seems too complex.

I looked at some of the Service Fabric sample projects, and it seems all the projects are tightly coupled with Service Fabric. My goal is keep these apps unaware of Service Fabric.

Any links of information is greatly appreciated, thanks!

Bill Bensing
  • 193
  • 2
  • 17

3 Answers3

2

We did this way:

  • Created the service fabric projects in the same solution of our WebAPI.
  • Inside the ServiceFabricHost project we created our HttpListenerService to expose the ports using service fabric, the same you would do to a SelfHosted WebApi.
  • Configure the ServiceFabricHost to open the needed endpoints.
  • Add the reference to the WebApi project
  • Use the WebApi Startup to send the IAppBuilder and configure the api with our external(SF) configuration, like ports and URL.

The secret is: when you create a self hosted web api, your generaly create a console application, like the asp.net docs. With service fabric, you replace the console with the ServiceFabricService, that is similar to a ConsoleApplication, but in this case will be a statelessService.

In this case, we used a stateless service for HttpListenerService, if you need a StateFull service, you will need to refactor your Api to use the reliable collections.

The fellow Vaclac, created a nice tutorial for this: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-communication-webapi/

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
0

If it's a standalone web application with a self-hosted web server (e.g., Katana, not IIS), then you can simply run it as a Guest Executable.

If it's not self-hosted and requires a separate web server to run, like IIS, then you can look at running it in a Container.

Vaclav Turecek
  • 9,020
  • 24
  • 29
0

I had the same problem and solved it by using the DependencyResolver.GetService() method on the HttpConfiguration object in the Startup() class. Create a new Service Fabric Stateless/Statefull WebAPI project. In the Startup() class, add the following code:

 public static class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public static void ConfigureApp(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host.
        HttpConfiguration config = new HttpConfiguration();
        // Allow custom routes in controller attributes.
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "Default",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new { controller = "API", action = "HealthCheck", id = RouteParameter.Optional }
        );
        //inject controllers here
        config.DependencyResolver.GetService(typeof({{YourWebAPIRootNamespace}}.Controllers.APIController));

        appBuilder.UseWebApi(config);
    }
}

This allows you to deploy your existing APIs into Service Fabric without having to migrate the entire code base to a new project. Don't forget to update the app.config in the new project with all applicable settings from your web.config.

Full blog post here http://thenameisirrelevant.com/hosting-an-existing-webapi-in-service-fabric

Adrian
  • 222
  • 2
  • 11