Employing a Winning Porting Strategy

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:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists