SUSE Conversations


Create Your First SUSE Linux Application in 10 Minutes Using the Visual Studio .NET IDE



By: coolguys

July 31, 2006 12:00 am

Reads:248

Comments:0

Rating:0

This article describes using the Visual Studio .NET IDE to write an ASP.NET stock quote Web application that runs on Linux!

Overview

Visual MainWin for J2EE (also called 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 SUSE 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

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. You can see the application in Figure 1.

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

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

The first thing you need to do is to create a new Visual MainWin ASP.NET Web application with a Web Form 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.

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

Step 2

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://www.webservicex.net/stockquote.asmx?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.

Figure 3. Adding the stock quote Web service from XMethods.

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", "ET", "BEAS", "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://www.webservicex.net/stockquote.asmx?wsdl
// to create a Web reference. 
QuoteService.StockQuote sq = new QuoteService.StockQuote();


// 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++) 
{ 
  string stockXml = sq.GetQuote(strTickers[lp]);
  XmlDocument stock = new XmlDocument();
  stock.LoadXml(stockXml);
  strValues[lp] =  stock.SelectSingleNode("//Last").InnerText;

} 


// 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

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 , so you don’t have to step out of your comfort zone!

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

Debugging your project on Tomcat

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 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 programs execution, you are ready for the last stage: deployment to Linux.

Figure 5. Debugging ASP.NET on J2EE.

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.

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

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.

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

Step 2. Deploy the WAR file to Linux

Use the Tomcat Management Console on your Linux box (http://<linuxboxaddress>:8080/manager/html) to upload and deploy your WAR file. Check the Tomcat documentation or the Grasshopper Web site 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!

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 SUSE Linux Mandrake version 10 and viewed within the FireFox browser.

Figure 7. Your application in action on Linux!

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!

Learn how to:

  • Use C# or VB.NET in your Grasshopper applications to write and debug JMS messaging applications using Visual Studio .NET
  • Build a Linux Communication Server and WinForm Clients Using Visual MainWin for J2EE
  • Port Microsoft’s Commerce Starter Kit to J2EE

at http://dev.mainsoft.com/Default.aspx?tabid=166.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Categories: Uncategorized

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

Comment

RSS