.NET Web Service (Returning a Simple Type)
Creating a Web service in Visual Studio .NET is relatively straightforward. You want to create a new ASP.NET Web Service project, as shown in Figure 25.2. In our examples, we will be building all .NET solutions with the C# language. Note that all examples in this chapter utilize Visual Studio .NET (VS.NET). For a free IDE, download ASP.NET Web Matrix from http://msdn.microsoft.com/academic/techdown/techprod/aspnet/aspnetmatrix/default.aspx
Figure 25.2. Building an ASP.NET Web Service with Visual C#.

Figure 25.3. Creation of WebServices1 project under wwwroot directory.

Figure 25.4. Creation of WebServices1 virtual directory in IIS.

Listing 25.1. HelloWorld.asmx.cs
To compile the code in Visual Studio .NET, go to Build and select the proper build scenario. To build out all objects in the solution, select Build Solution or press Ctrl+Shift+B. Compile errors will be displayed in the output window.The message "Build: 1 succeeded, 0 failed, 0 skipped" means you're ready to go and test your Web service. To do this, go to Debug and select Start. Internet Explorer will launch and display the WebService1 class definition. The documentation provided through the .NET interface gives you the ability to test the HelloWorld method. Click it, and you'll see the test page. Click Invoke, and a browser window is displayed showing the XML generated by the operation. See Figure 25.5 for the windows and the expected results.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;
namespace WebServices1
{
public class WebService1 : System.Web.Services.WebService
{
//WebService1 class constructor
public WebService1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component 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
// Hello World example web service
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
Figure 25.5. WebService1 class definition and test page with result.
[View full size image]

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
ColdFusion/.NET Web Service Integration Example
Now let's call the Web service through ColdFusion. We'll be using the <cfinvoke> tag for our examples. Notice the code in Listing 25.2 that is required to connect to the .NET Web service. Browsing to the HelloWorld.cfm file displays the results of the call to the .NET Web service. A simple type of string with a value of "Hello World" is sent to the calling <cfinvoke> tag, and the returnvariable is displayed using <cfdump>. The resulting page is displayed in Figure 25.6.
Listing 25.2. Display the results from WebService1
<cfsilent>
<!--- Call the .NET web service with cfinvoke --->
<cfinvoke
webservice="http://localhost/WebServices1/WebService1.asmx?wsdl"
method="HelloWorld"
returnvariable="result" />
</cfsilent>
<!--- Display the value returned from the web service --->
<cfdump var="#result#" />
Figure 25.6. HelloWorld.cfm results.
