devxlogo

Free Your C# Apps from .NET Platforms

Free Your C# Apps from .NET Platforms

ainsoft’s Visual MainWin is a revolutionary product that allows you to develop applications in C# and deploy and run them on J2EE application servers (for a detailed review, click here). Using this flexiblity, in conjunction with the simplicity of Web services development provided by Visual Studio.NET, you can finally create applications and Web services that run on platforms other than IIS and .NET.

Getting Started
If you don’t have a copy of the application, you can register for a 30-day evaluation copy from Mainsoft. To understand and follow the rest of this article, you will need a copy of Visual Studio.NET 2003 and a copy of Visual MainWin installed and running. Note that when you are installing Visual MainWin, please select the ‘Bundled Tomcat 5.0’ application server. If you are more comfortable working in BEA Weblogic 8.1 or IBM Websphere 5, you can also use these with MainWin?simply select them when you install. Some of the URL references in the article will be different if you are using one of these products (for example: Weblogic runs on port 7001, so references in this article to port 8080 should be changed to 7001).

First, start your application server. The new project dialog box in Visual Studio doesn’t automatically start it up, and it will throw an error if the application server isn’t running. Do this by going to your Start Menu and selecting ‘Start Tomcat’ in the Visual MainWin program group. Once Tomcat has started you can launch Visual Studio.NET and you are ready to proceed.

In Visual Studio.NET, select the ‘New Project’ wizard, and you will immediately notice some changes to the dialog.

Figure 1. This is the New Project Dialog.

Figure 1 shows some of these changes. Two new project type folders have been added, giving projects in C# and VB for J2EE. Also, the location for Web applications is now localhost:8080 instead of the familiar localhost. This is because Tomcat runs on this port by default. If you had selected Weblogic 8.1 when installing Visual MainWin this would read ‘localhost:7001.’

Select ASP.NET Web service, as in Figure 1, but change the name to DevxMainWin and click OK. The Visual Studio wizard creates the Web service for you.

If you have used Visual Studio to create Web services before, this will look immediately familiar. It’s a ‘Hello World’ Web service with a single Web method, called HelloWorld(). The method will be commented out, so comment it back in so that your code will resemble that in Figure 2.

Figure 2. Visual Studio.NET creates a default Web Service.

Compile and run this application to see the first major difference with Visual MainWin. Instead of the familiar test harness, you get a simple screen that says:

Visual MainWin For J2EE: Test page is not yet supported. Add the string ?wsdl to the current URL to get the service description.

So, you can build and run the Web service, but you can’t really tell if it does anything.

Editor’s Note: Laurence Moroney is currently the Director of Product Evangelism at Mainsoft; however, he wrote this article for DevX before joining Mainsoft.

Building and Consuming a Simple Web Service
To add a Web method that returns a value, add the following code to the Web service from the previous section:

[WebMethod]public string echoHelloWorld(string strMyName){  StringBuilder strReturn = new StringBuilder("Hello World to you, ");  strReturn.Append(strMyName);  return strReturn.ToString();}

Running the application gives you a similar result to what you saw earlier. Append the string ‘?WSDL’ to the url as specified, and see the WSDL page for this Web service. [i.e. http://localhost:8080/DevxMainWin1/Service1.asmx?WSDL]

Figure 3. This is the Web form design.

To consume this Web service, you will now build a Web application. To do this, make sure that the application isn’t running and then right-click the project in the Solution Explorer and select ‘Add Web Form.’ The ‘Add New Item’ dialog box will appear with ‘Web Form’ selected as default. Change the default name to ‘ConsumeDevx.aspx’ and hit OK to create the Web Form.

Create a Web reference to the Web service by right-clicking DevxMainWin on the Solution Explorer and selecting ‘Add Web Reference.’ In the ‘Add Web Reference’ dialog that pops up, enter the WSDL url (from above), and select ‘Add reference.’

Add labels and text boxes to the Web form as in Figure 3.

Double click the ‘Go’ button to add an event handler for clicking the button. Add the code from Listing 2 to this. This is assuming that the ‘top’ text box in Figure 3 is called TextBox1 (the default), and the ‘bottom’ one is called TextBox2.

private void Button1_Click(object sender, System.EventArgs e){	localhost.Service1 myWS = new localhost.Service1();	string strMyName = TextBox1.Text;	string strReturn = myWS.echoHelloWorld(strMyName);	TextBox2.Text = strReturn;}
Figure 4. Here’s a deployed ASPX, running on Tomcat, that consumes the Web Service.

Before you run the application, make sure that the aspx is the ‘default’ page for the application by right-clicking it and selecting ‘Set as Start Page.’

When you run the application, it is compiled and deployed onto Tomcat for you by the Visual MainWin plug ins to Visual Studio. The results are shown in Figure 4.

Now is that cool or what? You have just built a C# Web forms application and a C# Web services application, tied them together with Visual Studio Web services proxies, and compiled and deployed them?all on an application server without tweaking a single app server configuration file! Accessing Data with ADO.NET
Your Web service can also access data using ADO.NET or JDBC directly. In the case of the former, you have full access to the features of ADO.NET to allow ease of development and debugging. The problem is that with the current release of Visual MainWin only SQL Server and Oracle are supported.

To use ADO.NET with SQL Server, use the Server explorer to find the table or other view that contains data that you want to access. Drag this view onto the Web service design area, and Visual Studio.NET will create a sqlConnection object and a sqlDataAdapter object. The former contains the specifics for connecting to the desired database and the latter contains the specifics for the records in which you are interested. For example, locate the Northwind database and drag the ‘Customers’ table onto the workspace.

Your interaction with the data is through an object type called a DataSet. To get one of these, select the sqlDataAdapter object and, in the top menu, select Data?>Generate Dataset. Call the dataset dsCustomers and make sure that you check ‘Add this component to the designer.’

Once you have this dataset successfully created, you can pull data from the database and return it to the user in XML using this code:

[WebMethod]public XmlDocument getAuthors(){	sqlDataAdapter1.Fill(dsCustomers1.Customers);	System.IO.StringWriter sw = new System.IO.StringWriter();	dsCustomers1.Namespace="";	dsCustomers1.WriteXml(sw);	System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();	xDoc.LoadXml(sw.ToString());	return xDoc;}

This code compiles and runs on the J2EE application server, cross compiling the ADO.NET data access into the appropriate JDBC code.

What About Other Databases?
One of the problems with a cross compiler is that the compiler itself has to understand every technology that it cross-compiles. This can cause it to lose the ‘pluggability’ you find in aspects of the technology such as JDBC or ADO.NET. For instance, to support different databases, a cross compiler is necessary for each instance of a JDBC driver. This allows you to cut code using the ADO.NET or ODBC version of the driver. The compiler then cross-compiles this to the equivalent JDBC-generated bytecode. As you can see, the number of databases that a cross compiler can support is be limited so the vendors have chosen to support SQL Server and Oracle natively within Visual Studio.NET. This is why you can only use the ADO.NET shortcuts if you are using these databases.

All this need not concern you, as there is a very neat feature if you want to access any other database (and by extension any other java-based functionality)?you simply import the JAR file containing the JDBC drivers for your target database, and reference the drivers in C#.

For example, say you want to access a MySQL database on your application server. Add a java reference to the JDBC driver’s JAR file in your project by selecting the ‘references’ node in the solution explorer and then selecting ‘Add Java Reference.’ Browse to the JAR containing the JDBC drivers and add them. Visual Studio will take a few minutes to process the JAR.

Once you have done this with the drivers for MySQL, you can then create code to connect to a MySQL database. The code below is java, developed using all the features of the Microsoft IDE!

java.sql.Statement stmt = null;java.sql.ResultSet rs = null;String query="Select * from useraccounts";java.lang.Class.forName("com.mysql.jdbc.Driver").newInstance();string strSource = jdbc:mysql://localhost/mydb?user=me&password=this";java.sql.Connection conn = DriverManager.getConnection(strSource); stmt = conn.createStatement();rs = stmt.executeQuery(query);java.sql.ResultSetMetaData rsmd = rs.getMetaData();

You can add this code to a WebMethod and compile and run it inline with your C# code.

Freedom for Middleware Developers
Microsoft has appealed to the low- to mid-level developers by giving them the best tools, thus enabling them to do their jobs without worrying about underlying complexity. Opinion is sharply divided as to the right technology for the more advanced tiers of an application, notably for the data access and business logic tiers. Applications like Visual MainWin render this division irrelevant for middleware and user interface developers. Using the simplicity of Web services development provided by Visual Studio.NET, you can finally create applications and Web services that run on platforms other than IIS and .NET.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist