Confidently Deploy .NET Applications on Linux+Mono with Delphi Prism

.Net on Linux

This article aims to address the main problems that developers currently endure in developing and deploying cross-platform .NET enterprise applications to the Linux platform.

Since its inception .NET has captivated developers across the world with its simplicity of development and its flexibility in deploying enterprise-class applications. Let’s face it; today, Windows applications are reaching new frontiers in the software development spheres of the desktop, the Web and rich UX applications.

And these types of applications have found a new home, the Linux platform. Often, many developers have found deploying .NET apps to Linux challenging. In this short how-to, we will learn how to develop a .NET 4.0 RichTextPad, and deploy it to a Mono 2.8 Linux environment, without changing a single line of code.

Embarcadero Delphi Prism XE provides a cross-platform development solution and a robust programming language for rapidly developing .NET, Mono, ASP.NET and data-driven applications for Windows, Linux and Mac OS X. Delphi Prism combines an easy-to-learn syntax, based on the Delphi language, with features not available in other .NET programming languages — enabling developers to create cutting-edge applications. With Delphi Prism, you can:

  • Create .NET, ASP.NET Web, and Silverlight applications.
  • Build cross-platform Mono applications and deploy them on Windows, Linux and Mac OS X.
  • Develop apps with a choice of IDEs, on both Windows and Mac OS X.
  • Leverage your Delphi language skills with the easy-to-learn Delphi Prism language.
  • Work with the latest .NET 4.0 and Visual Studio 2010 technologies.

Requirements

Setup

  1. Install Delphi Prism XE just like any other Windows application.
  2. Set up the VM — launch VMware Player/VirtualBox, and create a new openSUSE-based VM; boot off the Live CD ISO (Mono-2.8.1.iso) and then choose Install. The recommended configuration is to have swap space of twice your physical memory; the remaining space can be allocated to the / (root) partition.
  3. Once installed, reboot the VM and log in with your credentials.
  4. Ensure that VMWare Tools/VirtualBox Guest Additions are installed in the guest OS.

Building a simple notepad application

  1. Launch Delphi Prism by clicking Start –> All Programs –> Delphi Prism in Visual Studio 2010. Ensure that you run Visual Studio as an administrator.
  2. Select File –> New Project and choose Windows Forms Application (Mono), under the Mono section, as shown in Figure 1.

    DelphiPrism new Mono Project

    Figure 1: DelphiPrism new Mono Project

  3. From the toolbox, drag and drop the following controls onto the form:
    1. MenuStrip
    2. ToolStrip
    3. StatusStrip
    4. StatusStrip
    5. ContextMenuStrip
    6. RichTextBox control
  4. For the MenuStrip and ToolStrip controls, set to insert standard items.
  5. Dock the RichTextBox control.
  6. For the ContextStrip menu, add Cut, Copy and Paste items. The final UI should resemble Figure 2.

    Design view of the Notepad Application

    Figure 2: Design view of the Notepad Application

  7. Add some code to implement simple Cut, Copy and Paste functionality:
    [Cut Action]
    
    method MainForm.cutToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs);
    begin
          richTextBox1.Cut;
    end;
    
    [Copy Action]
    
    method MainForm.copyToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs);
    begin
      richTextBox1.Copy;
    end;
    
    [Paste Action]
    
    method MainForm.pasteToolStripMenuItem1_Click(sender: System.Object; e: System.EventArgs);
    begin
          richTextBox1.Paste;
    end;
    
    end.
  8. Bind the ContextMenuStrip to the RichTextBox, and copy and paste the above code for the respective actions, in the ContextMenuStrip.
  9. Press F5 and run the application. Type some text and right-click to show the context menu, as shown in Figure 3. Verify that the added functions work, and close the application.

    Copy functionality runtime view

    Figure 3: Copy functionality runtime view

Deploying the application to Linux (the Mono appliance)

  1. Make sure you have the Mono appliance running.
  2. Navigate to the Debug folder of your project. The path will be similar to C:\Users\Username\Documents\visual studio 2010\Projects\Simple Notepad\Simple Notepad\bin\Debug.
  3. Copy the Simple_Notepad.exeto the Mono appliance desktop. Now on your Mono desktop, you should see an icon, as shown in Figure 4.

    Simple Notepad deployed to the Linux Desktop

    Figure 4: Simple Notepad deployed to the Linux Desktop

  4. Before we proceed, we should ensure that the application does not have any dependencies that prevent it from running successfully in our Mono environment. To test this, click Computer, and choose MoMA (Mono Migration Analyzer), as shown in Figure 5. Click Next to continue.

    MoMA -- Mono Migration Analyzer Application

    Figure 5: MoMA — Mono Migration Analyzer Application

  5. Now click the (+) button and add Simple_Notepad.exeto the Analyzer, as shown in Figure 6.

    Mono Migration Analyzer Wizard

    Figure 6: Mono Migration Analyzer Wizard

  6. Click Next to continue. It will start scanning the assemblies needed for a successful launch. You can see whether the MoMA tool has found any errors, as shown in Figure 7. It clearly indicates that one can run the .NET app in the Mono environment without any difficulties. Click Next twice to continue, and click Close.

    MoMA successful results Screen

    Figure 7: MoMA successful results Screen

  7. Now, launch a terminal window, and run mono Simple_Notepad.exe. You should see the application running without any problems, as in the composite Figure 8, which shows the application running on both Windows and Linux.

    The same .NET App in action in Windows and Linux

    Figure 8: The same .NET App in action in Windows and Linux

Congratulations! You have successfully ported a .NET application to Linux, using Delphi Prism, without having to change a single line of code.

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherwise noted.
Open Source For You is powered by WordPress, which gladly sits on top of a CentOS-based LEMP stack.

Creative Commons License.