ASP.NET on OpenShift Part 3: Running your ASP.NET Code on OpenShift

Part 3 – Running Your ASP.NET Code on OpenShift

In this part, we’re going to get your ASP.NET application (as created in Part 1 and Part 2) up and running on OpenShift. If you are new to OpenShift, here is an explanation of how it uses Containers and Kubernetes to build a platform for your to run your application.

Have an account for OpenShift Online (Next Gen) Developer Preview yet? If you don’t want to install anything on your machine for getting your project running on OpenShift, this is the first thing that you’re going to need.

OpenShift Online provides a quick way of trying out OpenShift without needing to install anything first. It’ll let you quickly see your project in action and test that it’s working on the platform.

If you’d like to try running it locally, take a look at installing Minishift.

However, for the purposes of this tutorial, I’ll continue with OpenShift Online Developer Preview, as it will have you up and running without needing to install anything. The Web Console in both Minishift and Online Dev Preview should work the same way, so you can easily follow along in Minishift if you prefer.

1: Create a Developer Preview account (or use Minishift)

If you’re going to proceed with OpenShift Online, go ahead and get your account first. To use it, you’ll need a GitHub login. You’ll be wanting to use GitHub anyway to host your example project for Dev Preview.

Before putting the project in the repo, let’s make a few last changes to it to make sure that it’s ready for OpenShift. These changes may mean that your project won’t build locally; that’s fine for now. In a later blog, I’ll go into how to set some variables that should allow you to control how your project builds, and some control over not only building in VisualStudio vs. OpenShift, but also setting up demo or debug builds.

2: Add some changes to your app

To get your ASP.NET project ready for OpenShift, all you need is to add 4 more lines of code in Program.cs (or you can try my repo out if you want to skip this step):

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; // 1st line added
using System.IO;

namespace MeowWorld
{
public class Program
{
public static void Main(string[] args)
{
var config = new ConfigurationBuilder().AddEnvironmentVariables("").Build();
// 2nd line added

var url = config["ASPNETCORE_URLS"] ?? "http://*:8080";
// 3rd line added

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup()
.UseUrls(url) // 4th line added
.Build();

host.Run();
}
}
}

The changes made are configuring ASP to listen to port 8080, which is going to be helpful for the next part.

3: Get it ready for a GitHub repo

I’m assuming that you have some experience with creating a project for GitHub.  If you don’t know how to import your project into GitHub, or need a refresher, here is a nice little doc put together by the GitHub people.  Make sure that your base directory in your Git repo is the one that contains your project.json file. This is crucial for getting it to build in OpenShift; otherwise, you’ll receive an error saying that this file is missing.

4: Create your project in OpenShift

Now that everything should be all set to get started, navigate to the Web Console and create your New Project.

Everything that’s necessary for this tutorial can be done using the command line or in the Web Console. For simplicity, I’ll show doing it in Web Console here.

Fill out the form with a Name for your project. Keep it to something that will work well as a URL. You can set the Display Name to anything that you like.

5: Import the s2i

Next, you’ll want to Add to Project. This is the fun part where you will be importing a JSON file that has the ASP.NET source-to-image (s2i). There may be several available for ASP.NET, but I recommend using this one to start:

https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams.json

You can download this from the redhat-developer GitHub and upload it from your machine, or simply copy & paste the code from the above raw file into the import form that’s pictured below.

6: Add your application to the project

Once finished, you should see a “Creating resources in project…” message. Click on the Add to Project button below it. Now, it’s time to upload your code that you’ve worked so hard on!

.NET should now be an option in the Languages menu. Click on the dropdown and select 1.0 if you made the project as I did in Visual Studio Community 2015. If you’re using Visual Studio 2017 or Visual Studio Code, then you probably want 1.1.

Next, enter a Name for this app, which will be used to label pieces created in your project. It can be the same name as your project. Now, get the URL for your Git Repository and enter that. Then Create your application. We’ll get to some of the advanced options soon.

7 (Optional): Create a webhook in your GitHub repo

This part is optional because webhooks will cause your build to automatically update, which you may not want. Use it with caution. If you’re using Minishift instead of OpenShift Online, take a look at Grant’s tutorial on getting started with Ultrahook.

If you want to proceed with having your application automatically rebuild & deploy as you make changes, then copy the webhook trigger URL from the bottom of the “Completed” page, shown below.

Go back to your GitHub repository, go to the Settings tab, and look for Webhooks on the left. Click Add webhook.

Paste your copied webhook URL into the Payload URL form below. From the dropdown, set the Content type as application/json. You’ll also need to Disable SSL verification for now. When you’re done, your page should look similar to the image below. Click Add webhook.

You can also check on the health of your webhook here by returning to the webhooks setting and clicking the Edit button below it. You should see Recent Deliveries; click on one to see what it’s doing. It’ll show Response 200 if it’s working as expected. If not, you’ll see what error it’s throwing.

8: View an overview of your project in OpenShift

Head back to the OpenShift Dev Preview Web Console. It should now be looking something like this:

Congratulations! Your ASP.NET web app is automatically building and deploying. We’re almost there!

If you added the webhook, your app will also automatically rebuild and deploy any time that you commit changes to your GitHub repository. This also means that you need to be careful with what and when you commit as long as the webhook is working, because any changes will be triggering your project to build and deploy again — meaning that your current deployment will go down until it’s done rebuilding and redeploying. This could take a while.

You can continue to view the status of your app in this overview.

If for some reason your app is not building and deploying automatically, you can click on Builds > Builds and then your app name and find the link to manually Start Build in the upper right. You can also check on the status of individual past builds here by clicking the build number in the list.

You can similarly do a manual deployment by clicking Applications > Deployments and then selecting the app name and clicking the Deploy button in the upper right. Feel free to poke around in this area while you wait on your app to get up and running, and see what information you can view about your build and deployment.

9: Manually create a route for your app

While waiting, go ahead and manually set up a route. Routes work by exposing an OpenShift service through an URL, giving a handy link to your page. One may be automatically created, but it will go to a default ASP.NET path, such as /api/values. This isn’t currently being used in our app and will just show a generic “value 1” and “value 2” if viewed. We’ll need to make an additional one to route to a path we use in the application.

Navigate to Applications > Routes.

Pick a Name for your route — again, something that will be suitable for a URL, as this will be appended to your app’s web address. Leave Hostname blank, and for Path, enter /cat. Select your app’s name (the one that you picked when you added the build and GitHub repo) from Service if it’s not there by default, and the Target Port should be 8080. Click Create when done.

Now, you can view your routes and should see the new one that you created there, as well as the default one.

10: Check that your app is working using the terminal

When your app is done building and deploying, your overview should look something like this:

As you may remember from building your app, you can now check to see if one of your cats is there by navigating to /cat/bill. The route that you created will go to /cat by default, so just append /bill (or any other cat name), and there you are.

Although everything is working with your app, you may see a message like this for some results:

 

This is fine! This message is ambiguous, but doesn’t indicate a problem. Your app should still be working behind the scenes. You can check to make sure by poking around in the terminal.

From the Overview page, you can click on “1 pod” and be taken to the Pods page. Click on the name of the pod, and then select the Terminal tab.

In the terminal, type curl http://localhost:8080/cat/bill. You should get an answering Meow! if it’s working.

Done!

You may not be able to see them by going to your app’s URL yet, but your cats are there.

You may need some patience with showing this app to anyone in Dev Preview, as it may take a while before the routes work. If you want faster results just for your own local experimentation, running Minishift on your machine is another option.

For now, your cat horde app is up and running! It should work in OpenShift just as it did when you were experimenting with it in VisualStudio.

Next blog

Next time, I’ll be teaching you about the M & V in MVC by showing how to do web pages. (We’ve already covered the C (Controllers) in the first two tutorials: Part 1 and Part 2.

Thanks for following along and creating an app on OpenShift. Do you have any future topics that you would like to see covered in this blog? Leave a comment below!

Categories
.NET, OpenShift Container Platform, OpenShift Dedicated, OpenShift Online, OpenShift Origin, Products
Tags
, ,
  • Jake Watkins

    Visual Studio 2017 does not provide a project.json file any longer.
    Is there a work around for this?