.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#.


Once the creation process has been initialized, .NET creates all Web service projects under the wwwroot directory on the drive where you have IIS installed (See Figures 25.3 and 25.4).

Figure 25.3. Creation of WebServices1 project under wwwroot directory.


Figure 25.4. Creation of WebServices1 virtual directory in IIS.


Our first Web service example will return a simple data type of string. Table 25.1 shows the simple types natively supported between ColdFusion and .NET Web services through the common WSDL data type definitions.

Table 25.1. CF, WSDL, and .NET Data Types

COLDFUSION DATA TYPE

WSDL DATA TYPE

.NET DATA TYPE

numeric

SOAP-ENC:double

Double

boolean

SOAP-ENC:boolean

Boolean

string

SOAP-ENC:string

String

array

SOAP-ENC:Array

primType[ ]

binary

xsd:base64Binary

Array of Byte objects

date

xsd:dateTime

DateTime

guid

SOAP-ENC:string

String

uuid

SOAP-ENC:string

String


Notice that VS.NET creates an initial Web service skeleton, let's take a look at the code. We'll go to the [WebMethod] area of the code to create a method that will return a string containing none other than the famous Hello World. Notice how .NET imports all required libraries from the .NET FCL to expose the method as a Web service. See Listing 25.1 for the code generated along with the Hello World example.

Save the WebService1.asmx.cs file, and be sure to set it as the start page by right-clicking on it in the Solution Explorer window and choosing Set as Start Page.

Listing 25.1. HelloWorld.asmx.cs
 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";    }   } } 

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.

Figure 25.5. WebService1 class definition and test page with result.


Notice that the default test scenario used here is exercised by an HTTP POST. This can prove problematic for applications that utilize an HTTP GET protocol to call the Web service. This issue is easily remedied by editing the web.config file associated with the .NET solution, so that it supports both protocols. This file is an XML-based text file that can contain standard XML document elements. It is located underneath the project tree when the project is created by default. Within the root element <configuration>, the <system.web> element supports configuring of Web services with the following:

 <?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.




Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net