Developing a Web Service with Borland C#Builder

By: Kevin Lin

Abstract: This step-by-step article demonstrates how to develop a simple Web Service with Borland C#Builder. This Web Service exposes methods for a simple encoding and decoding text string.

Step 1: Download and install the following software

a. Microsoft .NET Framework Version 1.1 Redistributable Package (file name: donetfx.exe, file size: 23MB)
http://msdn.microsoft.com/netframework/downloads/framework1_1/

b. Microsoft .NET Framework SDK Version 1.1 (file name: setup.exe, file size: 108MB)
http://msdn.microsoft.com/netframework/downloads/framework1_1/

c. Borland C#Builder Version 1.0 (file name: csb10_per_noncommercial.exe, file size: 30MB)
http://www.borland.com/products/downloads/download_csharpbuilder.html

d. ASP.NET Cassini Sample Web Server Version 1.0 (file name: Cassini.exe, file size: 332KB)
http://www.asp.net/Projects/Cassini/Download

TechNote:
1. Cassini Web Server is a very small Web Server and good for development purpose. However, I encounter the csc and port errors.
    The following suggestions are reading from some forums and solved my problem.
  a. I added or changed the PATH in the Windows environmental controls, as follows: "C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322"
     (replace the version or location with yours).
  b. From the command line, ran "C:/Program Files/Microsoft.NET/SDK/v1.1/Bin>sdkvars.bat"
  c. From the command line, ran "C:/Cassini>build.bat"
If you do not see any errors when run build.bat that Cassini Web server is working properly. Otherwise, you will need to troubleshoot Cassini Web server or use the Internet Information Server.

2. To be able to use this article to develop your Web Service component, you need to have the following products already installed:
  * Windows 2000 (any version)
  * Microsoft Internet Explorer 6 with SP1
For more complete hardware and software requirements, you can read the Borland C#Builder 1.0 installation notes.


Step 2: Create an ASP.NET Web Service Application in Borland C#Builder

Follow these steps to create a new ASP.NET Web Service application named SimpleWebService in Borland C#Builder:

1. Start Borland C#Builder.

2. On the File menu, point to New, and then click Other...

3. Click ASP .NET Web Service Application under C#ASP Projects.

4. In the Name box, type SimpleWebService and leave the default location in the Location box.

5. Change server to Cassini Web Server in the Server box and then click OK.

6. Point to "C:/Cassini/CassiniWebServer.exe" or other locations if it asks for your Web Server.

7. Double-click the file name WebServices1.asmx.cs in Files Explorer to open the source code.

8. Replace the existing code in the WebServices1.asmx.cs file with the following code:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace SimpleWebService
{
	// replace it with your own namespace
	[WebService(Namespace="http://localhost/WebServices1/")]

	public class WebService1: System.Web.Services.WebService
	{
		public WebService1()
		{
			InitializeComponent();
		}

		#region Web Form Designer generated code

		//Required by the Web Services Designer
		private IContainer components = null;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#endregion

		// Simple Web Service Methods
		// The following methods are providing simple encoding/decoding functions.

		[WebMethod]
		public string encode(string str)
		{
		return System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(str));
		}

		[WebMethod]
		public string decode(string str)
		{
		return System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(str));
		}
	}
}

9. The source code for this article can be downloaded from the BDN Code Central at http://cc.borland.com/codecentral/ccWeb.exe/listing?id=22309

Simple_Web_Service_Project_Screen

Step 3: Testing your Web Service

a. Let's test your simple Web Service by Click F9 in your Borland C#Builder to run the SimpleWebService project.

b. When you success build and run the project, you will see a page like this:

Run_Simple_Web_Service_Project_Screen

c. Click encode link to test your Web Service.

Test_Simple_Web_Service_Project_Screen

d. To invoke encode method, simply type in str parameter value for the method and click on the Invoke button. Once you invoke the encode method, you will see a resulting page in the XML output for the Web Service that will look like the button part of the screen shot.

e. Vice versa you can click the decode link and convert your text string back to the original text.

TechNote:
a. You can change your C#Builder configurations on the Project menu, point to Options. Under ASP.NET, you can find your Web Server, virtual directory or other settings.
b. The testing environment used here is Windows XP professional version with service pack one.
c. To deploy this Web Service component to Internet Information Server is as simple as copy the compiled code SimpleWebService.dll under bin folder to production virtual site. Either your .Net or Java client application will be able to access this Web Service component over the Web.

Summary

This article walked you through the entire process of installing, developing and testing a Web Service with Borland C#Builder. You saw how to develop a simple Web service component with Borland C#Builder. This Web Service exposes methods for a simple encoding and decoding text string. It can be used for hide values in configuration files or data in database. Although this article uses Borland C#Builder with Cassini Web Server, people familiar with Internet Information Server (IIS) can use it as well.

About the author

Kevin Lin is an Application Systems Engineer at Borland Software Corporation. He is a Sun Certified Professional for Java Technology. Kevin specializes in Java-based Web application and he can be reached at webkevin@yahoo.com


Server Response from: ETNASC02