Click here to Skip to main content
Click here to Skip to main content

Create your first Linux application with the Visual Studio .NET IDE in 10 minutes!

By , 27 Jun 2005
 

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Overview

Visual MainWin® for J2EE™ (a.k.a. Grasshopper) is an innovative product from Mainsoft that allows you to use the productivity of Visual Studio .NET to tap into the power of J2EE. Thanks to the cross-platform capabilities of J2EE, you can even write your applications in C# and ASP.NET and deploy them to J2EE servers running on Linux.

This article describes how you can use Grasshopper to build a simple ASP.NET application that consumes a stock quote Web service, and presents it using a Web form that runs on Tomcat under Linux.

The application

 

 

Figure 1. Your ASP.NET application running on Linux!

You can see the application in Figure 1.

This application consists of a single Web form containing a data grid. The form consumes the public Web service from XMethods.Net, which provides a 20 minute-delayed stock quote. A data grid cannot bind to a Web service; it can only bind to a DataSet object. Also, the Web service can only provide information for one stock ticker at a time. Therefore, the application makes a number of calls to the Web service, gets the retrieved data and stores it in an XmlDocument, and then generates a DataSet from the contents of this document that the DataGrid can bind to.

As such, this example demonstrates the following principles:

  • Web service consumption via a proxy class

  • XmlDocument management

  • Data binding

  • ADO.NET data sets

  • ASP.NET grid control

This is a pretty broad slice across some of the technologies offered by the .NET framework. Read on to see how these can be used to build Linux applications.

Building the application

Step 1.

 

 

Figure 2. ASP.NET Web form designer containing the grid

The first thing you need to do is to create a new Visual MainWin ASP.NET Web Application with a WebForm that contains a Data Grid. Using Visual Studio .NET, once you have installed Visual MainWin for J2EE, the File > New Project dialog contains two new project types – Visual MainWin C# and Visual MainWin VB. Be sure to create your ASP.NET Web Application from within one of these two projects types, using your language of choice. In this article we use the C# version.

The layout of the Web form is shown in Figure 2. It contains a grid that has been styled and a single button.

You can use the properties editor within the IDE to change the physical attributes of the grid and make it more cosmetically appealing. These settings will work on your Linux implementation, too! Other than placing, designing, and sizing the controls as you see fit, you don’t need to do anything further in this step.

Step 2.

 

 

Figure 3. Adding the stock quote web service from XMethods

Add a Web reference to the delayed stock quote service published by XMethods.net. You do this in Visual Studio .NET by selecting Web References in the Solution Explorer, selecting Add Web Reference, and then entering the following URL to the WSDL for the Web service:

http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl

You should give it the name ‘QuoteService’ to be consistent with the code snippets that you’ll see throughout this article. Your screen should look like the one shown in Figure 3.

In case you were wondering whether or not the automatically generated proxy for the Web service will work under Linux – the answer is ‘yes’, since the full SOAP Web Services Framework is implemented on J2EE and used when compiling your code for J2EE.

Step 3.

Before writing the code for the handlers, there's some initial setup that you need to do. First, add this line at the top of the QuoteList.aspx.cs code:

using System.Xml;

Next add a DataGrid and a Button control giving them the names ‘dgQuoteList’ and ‘bRefresh’ respectively.

Then, in the declarations section for the page (just under public class QuoteList : System.Web.UI.Page() add the following declarations:

protected string[] strTickers;
protected string[] strValues;

For demonstration purposes, the application is hard-coded to use six stock tickers. This is set up in the Page_Load() function, as follows:

private void Page_Load(object sender, System.EventArgs e) 
{
// Put user code to initialize the page here 
  strTickers = new String[6] {"IBM", "MSFT", "SUNW", "BEAS", "T", "KO"}; 
  strValues = new String[6]; 
  if(!Page.IsPostBack) 
  { 
    RefreshGrid(); 
  } 
}

There are three main functions that need to be written to make this application work. These functions all use the strTickers and strValues arrays.

RefreshGrid() Calls the Web service using each value in strTickers as the input to the Web service, and stores the return value in the corresponding entry in the strValues array.

private void RefreshGrid() 
{ 
  // This is a reference to the XMethods.NET Quote Web Service. 
  // Use: http://services.xmethods.net/soap/urn:xmethods-delayed- 
  // quotes.wsdl 
  // to create a Web reference. 
  QuoteService.netxmethodsservicesstockquoteStockQuoteService quote = new 
    QuoteService.netxmethodsservicesstockquoteStockQuoteService(); 
  // Go through the array of Ticker strings to call the Web 
  // service to get the appropriate values. This is done synchronously 
  // for simplicity. 
  for(int lp=0;lp<6;lp++) 
  { 
    strValues[lp] = quote.getQuote(strTickers[lp]).ToString(); 
  } 
  // Build an XML document based on the values in the 
  //strings.
  // We do this because the grid is a data grid and needs to bind to a 
  //DataSet.
  // One way of doing this is to use an XML document and ReadXML the 
  //data on the DataSet object.
    BuildXML(strValues); 
}

BuildXML() Takes the two arrays and builds an XmlDocument from them. This XmlDocument forms the basis of the data set that the Grid binds to.

private void BuildXML(string[] strValues)
{
  // Initialize a new blank XMLDocument
  XmlDocument xmlData = new XmlDocument();     
  // Create a root element
  XmlElement elRoot = xmlData.CreateElement("root");
  // For each company
  for(int lp=0;lp<6;lp++)
  {
    // Create an element called 'quote'
    XmlElement elVal = xmlData.CreateElement("quote");
    // Create an attribute called 'ticker'
    XmlAttribute attTicker = xmlData.CreateAttribute("ticker");
    // Attribute val = ticker val
    attTicker.Value = strTickers[lp];
    // Add the attribute to the element
    elVal.Attributes.Append(attTicker);
    // Set the value of the element to the current price
    elVal.InnerText = strValues[lp];
    // Append the element to the root node
    elRoot.AppendChild(elVal);
  }
  // Append the root node to the XML Document
  xmlData.AppendChild(elRoot);
  // Render the data.
  RenderData(xmlData);
}

RenderData() Creates a DataSet object from the XML in the XmlDocument and binds the grid to this DataSet object, causing the data to be rendered on the screen.

private void RenderData(XmlDocument xmlData)
{
  // The grid is a data grid needed to bind to a DataSet
  DataSet myDataSet = new DataSet();
  // Use XmlNodeReader to read the XmlDocument
  XmlNodeReader xR = new XmlNodeReader(xmlData);
  // The DataSet reads the XmlNodeReader
  myDataSet.ReadXml(xR);
  // And is then bound to the grid and rendered
  dgQuoteList.DataSource = myDataSet;
  dgQuoteList.DataBind();
}

Finally you need to add a click handler for the button. The easiest way to do this is to double click the button in the IDE designer, and the IDE will then create the handler and take you to the code for the handler. Edit this code so that it looks like this:

private void bRefresh_OnClick(object sender, System.EventArgs e)
{
    RefreshGrid();
}

You can see each of these functions in the download. After you have successfully coded and built the ASP.NET Web form, you can execute it. You will see results similar to those shown in Figure 1 (above).

All of the .NET classes that are used in the above code have J2EE implementations, and therefore work without modification when you compile your J2EE project with Grasshopper.

Your J2EE project workspace

 

 

Figure 4. Visual Studio .NET IDE with your J2EE-based C# project.

The workspace used for this project is a little different from what you would be using if you were building to run purely on .NET. For example, there are references to J2EE as well as the .NET namespaces that Grasshopper supports (See Figure 4).

Notice also that your initial solution configurations are now Debug_Java and Release_Java. You can see how nicely integrated all the Java™ references and requirements are with the Visual Studio .NET environment – you don’t have to step out of your comfort zone!


Debugging your project on Tomcat

 

 

Figure 5. Debugging ASP.NET on J2EE.

You can even use the Visual Studio .NET debugger to debug your application while it runs on Tomcat. To try it out, put a breakpoint on a line in Page_Load(). Then, make sure that Debug_Java is selected as your run profile, and start execution. After the application compiles and executes, you can debug it the same way you would if it were running on ASP.NET under IIS Internet Information Services (IIS). See Figure 5 for details.

Debugging works exactly the same as it does for regular .NET applications, even though you are no longer running them on IIS. You can inspect variables, step through code, run commands in the immediate window, and even inspect the call stack. When you are satisfied with your program’s execution, you are ready for the last stage: deployment to Linux.

Putting the icing on the cake: Running your ASP.NET application on Linux.

At this point, we assume you already have a Tomcat instance running on a Linux box.  For information on getting and deploying Tomcat, if you don’t already have it, refer to Mainsoft’s developer zone, which is full of resources for the budding Linux developer.

Here are the easy steps that you will go through to prepare your application for deployment and execution on Linux:

Step 1: Generate the WAR File

 

 

Figure 6. Creating the WAR file in Visual Studio .NET.

In the Visual Studio .NET IDE, on the Build menu, there is a new option called Deployment Packager. Select this option to run the WAR file wizard, shown in Figure 6.

Check the options as shown in the figure, and then click OK. Stand by while Grasshopper generates the WAR file for you.

Step 2. Deploy the WAR file to Linux

Use the Tomcat management console on your Linux box (http://:8080/manager/html) to upload and deploy your WAR file. Check the Tomcat documentation or refer to Mainsoft’s Linux Toolbox for more specific details. You don’t even need to restart Tomcat – it will upload the WAR file, place it in the appropriate directory, and service it for execution in the browser.

Step 3. Execute!

 

 

Figure 7. Your Application in Action on Linux!

If all goes well, Tomcat explodes your WAR into the appropriate directories and makes it available for execution. One thing that you should be aware of is that J2EE application servers use the concept of a context root, which is a little like a virtual directory under IIS. Your application's context root is the name of the WAR file, so if your WAR file is named linuxweb.war, your context root is now linuxweb. The URL for your application is now something like:

http://localhost:8080/linuxweb/QuoteService.asmx

See Figure 7 for an example of it running on Linux Mandrake 10.1 and viewed within the Konqueror browser.

Conclusion

In this article, you saw how simple it is to take a C# application, port it from .NET to J2EE, and deploy it within a J2EE container such as Tomcat. In addition, you took your J2EE application and, thanks to the portability of the specification, you were able to deploy and run it on Linux! So, with these few, simple steps, you were able to build your first Linux application using C# and Visual Studio .NET. That’s the power of Grasshopper, and that’s just for starters. There is so much more that you can do,, and it is well worth checking out the power that Visual MainWin for J2EE will give you. Download a copy and try it out yourself today!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Laurence Moroney is the Director of Product Evangelism for Mainsoft. He joined Mainsoft from Reuters, where he was a Senior Architect in their CTO Office, specializing in Enterprise Architecture and Web Services Interoperability. Laurence has also worked extensively in the financial services and security fields. He is the author of several computer books, including ‘Expert Web Services Security in the .NET Platform’ and ‘ASP.NET 1.1 with VB.NET’ as well as several dozen technology articles.
 
You can find his blog at: philotic.com/blog

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWhy to compile to Java Bytecode?...memberPetee3-Sep-05 19:33 
...why not just use Mono's CLI to run the bytecode ready for .net cli? Where is the problem?
 
Thanks
Pete
GeneralRe: Why to compile to Java Bytecode?...memberLaurence Moroney6-Sep-05 5:09 
No problem at all -- Mono is always an option, and a good one. It's a good, and a very fair question.
 
However, many companies will not run mission critical applications on anything other than a professional (as opposed to an open source) runtime. Add to the fact that operations management and monitoring processes and software will likely only work with J2EE or .NET; or add the fact that existing investments in J2EE or .NET need to be capitalized upon instead of introducing a new runtime and you have a recipe for a requirement to be able to run .NET applications on J2EE.
 
Once upon a time it was thought impossible, so, when it was necessary to get the 2 to work side by side, interop technologies such as Web Services would be used.
 
Now, with VMWJ2EE, you can have a unified application stack, with .NET and J2EE applications running side by side on a single platform, with a single (and therefore cost-effective) set of ancialliary tools (such as platform or application management)
 
Laurence
Generalconverting a asp.netmembermarciootta11-Jul-05 2:07 
HI,
Is it possible to convert a project written in c# and asp.net to linux platform in a simple manner? I'm looking forward a product that do this because of high cost of Windows 2003 C.A.L.
 
Thanks

GeneralRe: converting a asp.netmemberLaurence Moroney12-Jul-05 8:44 
Absolutely. The Visual MainWin for J2EE product is designed for that solution.
 
Take a look at this website: http://dev.mainsoft.com for more details, as well as lots of resources and how-tos. Of course you can also feel free to email me with any questions.
GeneralRe: converting a asp.netmembertcboring18-Jul-05 10:21 
Why not just deploy your application on Mono?
 
Mono is the open-source version of the .NET Framework, located at http://www.go-mono.com. I've written a few applications now in C# which run on both Windows and Linux, and I didn't have to buy any additional software.
Generala question!sussEEmad9-Jul-05 18:01 
dear,
 
is it possible to convert windows applications(Form-based) to some object code which is available to run in linux compatible OS?
and before that is it possible to install .NET platform on Linux(s)?Sniff | :^)
 
Thanks

GeneralRe: a question!memberLaurence Moroney12-Jul-05 8:46 
The Visual MainWin for J2EE product doesn't handle WinForms. However, in many cases it isn't a great deal of effort to transform your WinForms into Web Forms, which can then be compiled into Java Byte Code using Visual MainWin for J2EE, and can then run on LinuxOS.
 
With regards to .NET platform on Linux, you could take a look at Mono (www.gomono.com)
GeneralRe: a question! [modified]membertcboring18-Jul-05 10:25 
The URL is actually: http://www.mono-project.com/Main_Page.
 
Back to the question at hand...Mono is a great alternative when you want to compile .NET projects to run under LINUX. I have four .NET systems I've built for my company which use Windows Forms for the User Interface. In each case, porting the applications from Windows to LINUX was trivial because Mono is such a great product. The best part about Mono is...IT'S FREE! Which means you don't have to pay any money for any kind of a license, unlike "Grasshopper".
 
theBoringCoder

modified 2-May-12 18:12pm.

GeneralRe: a question!memberLaurence Moroney18-Jul-05 12:14 
Mono is a great alternative -- you are quite right.
 
What is important to understand is that Grasshopper allows you to deploy to Linux *via* J2EE, so, in addition to being able to use the same application on multiple OS platforms (Unix, Solaris, WinTel etc.), you also have the advantages of being able to use commercial J2EE platforms such as Weblogic or WebSphere.
 
Not only that, when you start thinking scalability, clustering, management, reporting etc. that are necessary for commercial applications, then J2EE is a very compelling choice, and Grasshopper is the best and only way to take your C# / VB.NET applications and move them to a multi-platform environment without recoding them from the ground up.
 
Laurence
GeneralRe: a question! [modified]membertcboring20-Jul-05 8:42 
Laurence,
 
Thank you for replying very directly and with specific information to back up your stance. While I may not agree that J2EE is generally compelling when looking for scalability, clustering, management, and reporting, I do concede to your other points.
 
In my situation, I've had customers running my applications built in .NET that said, "hey, we want to run this under Linux now." My response was and is to port the application to run under Mono on Linux. It's true that there is some work that has to be done, but that work is minimal in terms of resource-hours spent.
 
thanks,
 
Tom
 
theBoringCoder

modified 2-May-12 18:11pm.

GeneralRe: a question!memberLaurence Moroney20-Jul-05 10:35 
Tom:
 
You are very welcome -- and I agree that each solution requires a different analysis. Mono is great, but it isn't for everyone, in much the same way that J2EE is great, but it isn't for everyone.
 
It's not fair on either to do an apples to apples comparison between the two. What I would recommend is for anyone who wants to port to think about the reasons behind the desire to port, and to project beyond *this* port to where you want to go in the future. If you're happy with running it on Linux with Mono and going no further, then great, best of luck to you.
 
If you want to start thinking Enterprise applications, in particular Grid and Utility computing, or if you need to use management platforms such as Tivoli, or if you need to integrate with existing middleware (i.e. EJB or WS), and of course if you want to look beyond Linux onto other platforms, then J2EE is the only realistic choice, and then it becomes a matter of *how* you get there -- recoding or porting?
 
Laurence

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 27 Jun 2005
Article Copyright 2005 by Laurence Moroney
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid