Orchard Core and Service Fabric - Part 1 - Installing Service Fabric and Creating an ASP.NET Core website

Installing Service Fabric

Before we can host anything on Service Fabric, we first need to make sure we installed it. Service Fabric not only enables us to host applications in the cloud, but also on-premise and on our local machines.

If you haven't done so already, here's how you install Service Fabric:

  1. Launch Microsoft Web Platform Installer.
  2. Search for Service Fabric and install it.

Microsoft Web Platform Installer and Service Fabric

Once installed, you should see Service Fabric's Local Cluster Manager tray icon:

Service Fabric Local Cluster Tray Icon

Finally, make sure that the Service Fabric cluster is set-up using 1 node instead of 5 nodes (which you don't need for local development and increases deployment time and requires more memory) and that the cluster is started. To do all this, simply right-click on the Service Fabric Cluster Manager tray icon:

Service Fabric Local Cluster Task Tray Icon Contet Menu

To make sure your local Service Fabric cluster is up and running, right-click on the tray icon and select Manage Local Cluster. This will launch your default web browser and display the Service Fabric Explorer dashboard:

Service Fabric Explorer - Dashboard

There are currently no applications, but that is about to change.

Creating a Service Fabric Project

Now let's have a look what it takes to create a new Service Fabric Project using Visual Studio 2017.

Open Visual Studio, and choose to create a new Service Fabric Application project. You will find this project template in the Visual C# -> Cloud category.

Creating a new Service Fabric Application using Visual Studio 2017

Make sure to select the .NET Framework 4.7 as the target framework. As of this writing, Service Fabric does not target .NET Core, but Microsoft is planning to provide support .NET Standard. What this means today is that when you build an Orchard Core application for Service Fabric, it will target .NET Framework 4.7. However, since Service Fabric runs on both Windows and Linux, your application too will run on those operating systems (since they run on Service Fabric).

Choose "BooksUnlimited" as the name of the project and solution, and hit the OK button.

Before Visual Studio will create the solution, the next screen will ask you to select which type of service you would like to create. Since we're going to create an Orchard Core application first, we'll go with the Stateless ASP.NET Core template and call the service BooksUnlimited.Web.

Creating a new Service Fabric Service using the Stateless ASP.NET Core template

When you click the OK button, Visual Studio will create the Service Fabric project, and then prompt you to select which project template to use for creating the ASP.NET Core Web Application. Make sure to got with the Empty template, as we'll be adding middleware ourselves from scratch.

New ASP.NET Core Web Application - BooksUnlimited.Web

When all is said and done, Visual Studio will have created a solution with 2 projects:

  1. The BooksUnlimited Service Fabric Application project. This project references all services to be deployed as part of the application.
  2. The BooksUnlimited.Web Service Fabric Stateless Service project

Books Unlimited solution containing two projects

At this point, you can run the application using CTRL+F5 or via Debug -> Start Without Debugging. When you do, Visual Studio will deploy the Service Fabric application (BooksUnlimited) to your local cluster.

Fixing the launch URL

Once deployment is succesful, Service Fabric will try to launch the configured Application URL, which is a random URL that will not necessarily match the URL used when generating the ASP.NET Core project. Therefore, I recommend you sync the two settings to be the same.

To do so, we need to know what URL will be used by the BooksUnlimited.Web service to listen for requests. One way to find out is to open the ServiceManifest.xml file in the PackageRoot folder, and taking a note of the port number being used for the "ServiceEndpoint" endpoint. In my case, this is 8303, which means that the desired launch URL should be http://localhost:8303. Now select the BooksUnlimited project from Solution Explorer, hit F4 to open its properties window, and update the Application URL to the desired URL.

Try it out and launch the application using CTRL+F5. If succesful, you should see the "Hello World" message from the empty ASP.NET Core project:

Hello World output from an empty ASP.NET Core project template

And the Service Fabric Explorer Dashboard should now show the ASP.NET Core application as a service:

Service Fabric Explorer with one application and one service

A brief description of the numbered elements:

  1. This is the Service Fabric Application Type, which is by default based on the solution name, suffixed with the word Type. We can have many instances of a given application type.
  2. This is one instance of the BooksUnlimitedType application type.
  3. This is the one and only SF service we have so far. It is a Stateless service that hosts our ASP.NET Core application.

Pro tip

When you have been developing with Service Fabric for a while, you'll quickly become very bored (and very frustrated) by the fact that even though deployment doesn't take all that long (somewhere between 10 seconds to a minute, depending on the size of the project), still those seconds add up when trying to do rapid application development. At some point you might even start to wonder if this Service Fabric thing is even worth it if you have to give up the development speed you get when building for regular IIS. Fortunately, there is a solution to this. There is an option called application refresh that you should enable while developing your application. To enable this, select your Service Fabric project (BooksUnlimited in our case), and change the Application Debug Mode to Refresh Application (Preview). You can thank me later. :)

Next, we'll see how easy it is to turn our ASP.NET Core application into a full-blown Orchard Core application in Part 2.

See you there!

Leave a comment