The Apprenda Platform has a long history of providing developers with choice when deploying their applications in a variety of stacks, architectures and technologies. For this blog post, I want to show how easy it is deploy an OWIN hosted Web API application using Windows Services. The completed source for this demo is also available on GitHub.
Let’s get started!
1. Open Visual Studio and create a new project of type Windows Service
2. Let’s rename our service to myApi by right clicking on Service1.cs and renaming it.
3. We need to add OWIN to our reference. We can do that using the nuget package manager. Search and add the following dependency: AspNet.WebApi.OwinSelfHost
4. Let’s create our WebAPI controller. Add a new class to your project and name it cs.
The class should look as follows:
5. Now we need to create a Startup class to load up the WebAPI and map the route to the controller. Create a new class called cs and make it look as follows:
6. Apprenda allows developers to map ports in two different ways for Windows Services, static or dynamic. In this case, since we will be deploying multiple instances of the service, we will use dynamic port mappings and token replacements as a way to configure each instance with the correct information.
7. Open the config and create an appSettings section. The content should look as follows:
What are we configuring?
The Apprenda Platform provides certain mechanisms to dynamically reconfigure an application at deploy time and provide a variety of tokens and parameters. In this case, we are taking advantage of the following: A. Local/Live switching –> This allows Apprenda to comment configuration in/out depending on whether an application is deployed or not and based on the stage (Sandbox vs Published). B. Token replacement –> The Apprenda Platform has a variety of tokens that can be switched at deploy time based on environment information. In this case, we are using tokens to dynamically retrieve the port assigned to each service instance.
8. Open the code view for the service by double clicking the service in the project explorer and then switching to code view.
9. Declare an IDisposable private object called _webapp.
10. Add a reference to the project for System.Configuration (we will use this to read the app.config)
11. Modify the OnStart code as follow (fix any reference issues due to missing Using statements):
12. Now we will add an Apprenda DeploymentManifest to configure the service for deployment. This will allow us to set the dynamicPort mapping, minimum instance count and service account required to run this service. Right click on your solution, add an XML file and name it DeploymentManifest. (The credentials have been left blank on purpose)
13. At this point, we have everything we need to deploy this application! The next step is to create an archive for this application by packaging the service binaries and the DeploymentManifest. The structure of the archive is as follows:
B. winservices –> Apprenda.Sample.OWIN –> service binaries (bin directory output)
14. That’s it! Log in to your Apprenda Platform instance, upload the archive and deploy!
The endpoint can be reached at /api/demo
Erik is a Senior Solutions Engineer at Apprenda. He is responsible for the successful implementation and migration to large-scale PaaS projects, for secure customer adoption, and for the expansion of the platform. You can follow him on Twitter at @elustgarten.