Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Create or Convert Your Service Fabric Microservices To .Net Core xproj Structure

DZone's Guide to

Create or Convert Your Service Fabric Microservices To .Net Core xproj Structure

Today we’ll discuss how we can create a new .Net Core Service Fabric Microservice targeting the full stack (.net46).

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

In a previous article, we walked through the process of hosting our Asp.Net Core Web Microservice within Service Fabric and also self-hosting outside Service Fabric via Kestrel for development and debugging. Today we’ll discuss how we can create a new .Net Core Service Fabric Microservice targeting the full stack (.net46), given the VS 2015 template only supports web projects currently.

Note that similar principles would apply to converting existing Microservice projects to .Net Core xproj structure. 

To begin, in Visual Studio 2015 add a new Service Fabric Project, in my example a Stateful Service named AcmeService:

Image title

Once complete you should have a solution resembling the below:

Image title

What we do next is remove the AcmeService project from the solution altogether and rename the folder to AcmeService.tmp. We will re-create the project as a .Net Core Console Application. Select Add New Project and select Console Application (.Net Core), making sure the location is the same as the original and enter AcmeService as the project name:
Image title
From the AcmeService.tmp folder copy:

PackageRoot folder
Properties folder
AcmeService.cs (copy over target file)
Program.cs
ServiceEventSource.cs


to the AcmeService folder, your solution resembling:

Image title
Copy the contents of the below into your project.json file:

{
    "title": "AcmeService",
    "description": "AcmeService",
    "copyright": "Copyright © Acme 2016",
    "version": "1.0.0-*",

    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true,
        "compile": {
            "exclude": [
            "PackageRoot"
            ]
        } 
    },

    "dependencies": {
        "Microsoft.ServiceFabric": "5.1.150",
        "Microsoft.ServiceFabric.Services": "2.1.150"
    },

    "frameworks": {
        "net46": { }
    },

    "runtimes": {
        "win7-x64": { }
    }

}


Lastly, we have to add back our .Net Core Console Application project by right clicking on the Service Fabric project and selecting Add Existing Service Fabric Service. You might get a warning about updating but just click OK. You can also delete the AcmeService.tmp folder as it’s no longer needed.

To compile you can use Visual Studio or at a command prompt you can issue normal dotnet.exe commands, for example:

dotnet.exe build

In the next series of articles we’ll look at some more advanced topics such as sharing appsettings.json files between Web and other Microservice projects, as well as logging to Application Insights.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
asp.net core ,microservices ,service fabric ,microsfot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}