Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

27 Jun 2005 1  
This article describes using the Visual Studio .NET� IDE to write a stock quote ASP.NET Web application that runs on Linux�.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles 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