Employing a Winning Porting Strategy

n the recent “Race to Linux 2.0” competition, a .NET programming event hosted by DevX, .NET developers were invited to get ASP.NET applications up and running on Linux. The three-part competition allowed entrants to download preselected .NET applications and either port them or rewrite them to run on Linux, using any other technologies of their choosing.

I chose to port the Blog Starter Kit to the Java Enterprise Edition (Java EE) platform using Mainsoft for Java EE, Developer Edition (Grasshopper 2.0). The Blog Starter Kit is an open source ASP.NET application developed by Shanku Niyogi for users who want to adopt and customize an easy-to-use blog application for their site. Grasshopper is a Visual Studio 2005 extension that enables you to cross-compile an ASP.NET 2.0 web application project to a Java EE application and run it on Linux. The combination of Grasshopper and VMware Server for the MySQL Virtual Appliance made porting the Blog Starter Kit very easy. To begin, take a look at the application chosen for this solution.

The Blog Starter Kit
The Blog Starter Kit is an open source, ASP.NET 2.0 application. It works out of the box with a small amount of configuration, and it offers interesting features such as a rich text editor for formatting posts, the ability to expose the blog as an RSS feed, and an email notification for posted comments. The application works with IIS on Windows 2003 or IIS on Windows XP. Using Grasshopper, you can port the Blog Starter Kit to run on Java under Linux and migrate the database in less than a day.

Grasshopper bridges the gap between .NET and Java EE by extending the Visual Studio IDE with a robust code-porting toolkit. When you build an ASP.NET application, the C# compiler compiles the program code into an assembly containing Microsoft Intermediate Language (MSIL). To execute the MSIL the .NET Common Language Runtime (CLR) compiles the assembly into a platform-specific instruction set.

Grasshopper converts that same MSIL code into Java bytecode. Grasshopper rehosts the Mono .NET Framework libraries on Java to deliver key .NET functionality on the Java EE platform. (Note that Mono is an open source, cross-platform implementation of the .NET Framework?Mainsoft is a contributor.) Grasshopper deploys your applications to a Tomcat application server, along with a Java implementation of the .NET namespaces. With very few changes to an original .NET application, you can “hop” into Java.

To get started with the porting project, first you’ll step through the process of preparing to port the Blog Starter Kit:

  1. Install the Grasshopper 2.0 extension into Visual Studio 2005 to open the sample project.
  2. Download the Blog Starter Kit sample project. Unzip it to a directory of your choosing, and then open Visual Studio 2005.
  3. Select File –> Open –> Web Site, and browse to the project folder that you just unzipped (BlogWebApplication folder).
  4. Once you’ve loaded the Web application project, look for a file called Welcome.html, which outlines the functionality of the Blog Starter Kit. Follow the instructions for creating an administrative user.

Before you begin the port, it’s a good idea to run the web site to make sure it functions as expected. Press the F5 key or click the Play button to run the web site from Visual Studio’s built-in web server.

Connecting to a SQL Server in a hybrid Windows/Linux network is one approach to porting the application. For example, you can change the C# code to access MySQL using the OleDb ADO.NET provider and the MySQL JDBC driver.

Editor’s Note: This article was contributed through Mainsoft. We have selected this article for publication because we believe it to have sufficient, objective technical merit, and it was written by an independent developer.

Expressly Virtual MySQL
The Blog Starter Kit uses a SQL Server 2005 Express database file for saving posts, categories, feedback, and other data. MySQL is a popular database to use with Linux; and using its latest version is recommended. Instead of installing MySQL 5 on your local computer, implement the VMware Server and run the MySQL virtual machine included with the source code download.

The Blog Starter Kit database is pre-installed on the server image, which emulates the separate database/web server infrastructure that is common in many hosted environments. To get started, download VMware Server and run the installer. Be sure to request a free license for the server.

If you’re using Windows XP, when you run the installer a warning message will appear that informs you of potential problems running VMware Server on a nonserver operating system. You can ignore this message and continue with the install. Another warning will appear about running the management interface manually from IIS, but you can ignore it as well because running this interface doesn’t adversely affect using the VMware Server.

Download the MySQL virtual machine to a folder that is accessible to your VMware installation. Run VMware Server, and select local as the host environment. In VMware Server Console Home, select Open Existing Virtual Machine, and then click Browse (see Figure 1). Navigate to the folder containing the virtual machine download, unzip the MySQL Virtual Appliance, and select the VirtualAppliancesMySQL.vmx file. The server will load the virtual machine into the management console and display a details page.

If you’re using a server like Windows 2003 as your development machine, VMware should be configured automatically to map virtual machines to your network. If you’re using Windows XP Pro, follow these instructions:

  1. On the Details page, select Edit virtual machine settings.
  2. Select Ethernet from the Device list (Hardware tab).
  3. Select the NAT radio button (see Figure 2).
  4. Click OK, and then start the virtual machine using the green Start button.

Next, fire up the MySQL appliance by clicking the Start button. VMware boots up Linux as a hardware-abstracted, virtual server utilizing the same network resources as your host (local) machine. (See Figure 3.)


Figure 1. Load the VMware Server: Use this dialog to open an existing virtual machine.
 
Figure 2. NAT Connection Setup: Set up the virtual machine for sharing the host’s IP address.
 
Figure 3. Virtual Database: The MySQL virtual appliance runs on the VMware Server.

In case the server doesn’t obtain an IP address like the one shown in Figure 3, follow the previous Windows XP instructions. The second-to-last line of the boot screen refers to the administration web site for the MySQL appliance. You’ll need to log in to this site shortly; use “admin” for both the username and the password.

Database Dialects
It was necessary to make some code changes to make the application compatible with the MySQL database. If you take a look at the code in the BlogEngine class, you’ll see where changes were made to the data access methods. In this case, you can see a change that was required to support MySQL—handling DateTime—as an example. In SQL Express, the DateTime field looks like this:

5/1/2007 12:00:00 AM

This format is supported when using the .NET DateTime datatype to populate the corresponding database type. In MySQL, however, a DateTime field is formatted like this:

2007-04-01 12:00:00

If a .NET DateTime value is used to populate a MySQL DateTime column, MySQL registers an exception. To solve this communication breakdown, you can add a new property called DatePostedMySQL to the Post class, which returns a formatted string that complies with MySQL’s DateTime. Take a look at how the existing DatePosted property is utilized:

public string DatePostedMySQL{   get   {    string _datePostedMySQL;      DatePosted = DateTime.Now;      StringBuilder sb = new StringBuilder();      sb.Append(DatePosted.Year);      sb.Append(@"-");      sb.Append(DatePosted.Month);      sb.Append(@"-");      sb.Append(DatePosted.Day);      sb.Append(" ");      sb.Append(DatePosted.Hour);      sb.Append(":");      sb.Append(DatePosted.Minute);      sb.Append(":");      sb.Append(DatePosted.Second);      string _datePostedMySQL = sb.ToString();      _datePostedMySQL.Replace("{", "");      _datePostedMySQL.Replace("}", "");      return _datePostedMySQL;      }}

Generate and Configure the Java EE Project
Remove the original Java EE project from the solution. Right-click the Visual Studio project, and select Generate Java EE Project from the menu. The Generate Java EE Project wizard appears. The next screen gives you the option of saving the original ASP.NET solution file. Select this option, and continue to the next screen. Click OK and Grasshopper will jump into action, creating a new Java EE project.

The ported Java application will use the MySQL JDBC driver to access your MySQL server. If it’s not already preconfigured in your Tomcat server, add the JDBC driver as a reference to your project. If you’re wondering how to add a Java assembly to a Visual Studio project, Grasshopper takes care of it by using the tools it has integrated with your IDE. Right-click the References folder in your Java EE project, and select Java Reference from the list.

Select Browse, and go to the Java References folder in the Visual Studio project example. Select the mysql-connector-java-5.0.5-bin.jar file and click Open. Grasshopper processes the reference and adds it to the list. Change the “Blog” connection string in the project’s web.config to specify this driver, the MySQL server IP address, database, and the login details:

Build the Java EE Project
The next step is to compile the project into Java bytecode. Before the build starts, Tomcat should be running because Grasshopper will deploy all the necessary files for the Blog Starter Kit to the Tomcat web application folder. You can start Tomcat using this shortcut: Start menu –> All Programs –> Mainsoft for Java EE –> Start Tomcat.

Right-click the Java EE project, and select Build. Grasshopper compiles the .NET code into Java bytecode using a Java version of the Mono Framework assemblies.

Switch the startup project in Visual Studio to the Java EE project, make sure that the MySQL virtual machine is running, and press F5 to start the application in Debug_Java mode. As a result you’ll see the Blog Starter Kit home page running in Tomcat.

If you want to set a breakpoint and step through some of the code, Grasshopper makes this process transparent by mapping the C# source code to the bytecode currently executing in the Java EE runtime. You can still use Visual Studio to set breakpoints and view variable data, and you can also debug the Blog Starter Kit once it’s deployed to the Tomcat server by attaching the Visual Studio debugger to the Tomcat virtual machine.

Do you remember previously setting up the administrative user and role using Visual Studio’s built-in Configuration Manager? Grasshopper covers these settings by implementing its own default Membership provider, and it creates a Derby database that does the same job. Apache Derby is an embedded, 100% Pure Java database server, which is now included in the new Java EE project.

Select the Java EE project in Visual Studio, and click the Configuration Manager icon to go to Grasshopper’s Web Site Administration Tool (WSAT). Add an administrative role and a user for that role, which enables you to log in to the admin section of the Blog Starter Kit at http://localhost:8090/BlogWebApplication/admin/default.aspx, and you can add posts and categories. Keep in mind that the administration tool is a Grasshopper application coded entirely in Java, but it can run under Linux, should you need to access it again (see Figure 4).


Figure 4. Add the User: The WSAT is a Grasshopper application coded entirely in Java, but it runs under Linux if you need to access it again.

Your Java EE application needs to know which MembershipProvider to use; add this tag to the system.web section of your project’s web.config:

 

You also should enable remote access to the Grasshopper WSAT to be able to manage your users from a browser running remotely. An application setting key, which by default is disabled for security reasons, has been defined to enable remote access for the WSAT. Add this code into your web.config file to enable it:

(...)    

Another change that is required for the WSAT is in global.asax. The Blog Starter Kit redirects all requests to the post.aspx page. Since the Grasshopper WSAT application is packaged within the application itself, and not as a separate application, you’ll need to ensure that requests to the WSAT won’t be redirected to the post.aspx page. Therefore, add another condition in global.asax before proceeding to the redirect, just to be sure that the request is not to the WSAT application:

if (!File.Exists(physicalPath)   && physicalPath.EndsWith(    ".aspx", StringComparison.InvariantCultureIgnoreCase)   && !physicalPath.Contains("aspnetconfig"))

Deploy the Application in Linux
The next step is to create the Java deployment package. Stop the running application by clicking the Stop button, or by closing the browser. Switch the project to Release_Java, and then go to the Java EE project’s properties page. Select Java Build from the left-hand menu, and change the Deployment method to Full deployment package (WAR). Then build the project once this property has been set.

Now for the fun part: running the starter kit on a Linux server to get you to the finish line. Follow the same steps outlined for the MySQL virtual appliance to download the Tomcat appliance, and run it on the VMware Server.

Start the Tomcat virtual appliance, and go to the Tomcat administration site as specified in the Linux command prompt. Log in to the Tomcat administration section as admin?and enter admin as the password (if needed, check the Virtual Appliances download page for the up-to-date login details)?and scroll to the bottom of the page. In the Import section, browse to the WAR file that Grasshopper generated in the project’s bin directory and select it (see Figure 5).

Tomcat will install the Blog Starter Kit and provide a link to the site to set up the Blog Starter Kit running on the Tomcat virtual appliance (see Figure 6).


Figure 5. Deploy the WAR File: Browse to and select the WAR file that Grasshopper generated in the project’s bin directory.
 
Figure 6. Running It: The ported Blog Starter Kit displays this home page when running in Linux.

There you have it: you’ve ported completely an ASP.NET 2.0 application that began in .NET running on Windows and SQL Express and ended up being served up by a Linux web server and MySQL. Hopefully, this procedure demonstrated a porting strategy that will better enable you to enter the world of cross-platform .NET development.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS