Creating Web Services


In addition to supporting the consumption of Web Services, ColdFusion also allows developers to create their own Web Services for consumption by ColdFusion, .NET, Java, and more. Web Services are created using ColdFusion Components.

ColdFusion Components were reviewed in Chapter 31, "ColdFusion Components."


ColdFusion Components are automatically ready to be used as Web Services, but for security reasons code is not accessible as a Web Service unless specifically enabled. To make a CFC method accessible as a Web Service, simply set access="remote", as seen here:

 <!--- User component ---> <cfcomponent>   <!--- List users method --->   <cffunction name="List"               access="remote"               returntype="query">     <!--- Get users --->     <cfquery name="users"              datasource="exampleapps">     SELECT EmployeeID AS UserID, FirstName, LastName     FROM tblEmployees     ORDER BY LastName, FirstName     </cfquery>     <cfreturn users>    </cffunction> </cfcomponent> 

The List method in this component returns a query (obtained by using a <cfquery> tag), and because access="remote", the method will be accessible as a Web Service.

NOTE

The <cffunction> returntype attribute is optional in ColdFusion Components, but it is required if the method is used as a Web Service.


ColdFusion also automatically generates the required WSDL on demand. The WSDL URL for any ColdFusion Component is the URL to the CFC appended with ?wsdl. For example, the WSDL for the ColdFusion Tip-of-the-Day Web Service at www.forta.com is:

 http://www.forta.com/cf/tips/syndicate.cfc?wsdl 

TIP

ColdFusion Components can be invoked as CFCs or as Web Services, but for performance reasons, CFC invocation is preferred.


Working With SOAP Headers

Within CFCs, the following functions may be used to access SOAP headers (that is, headers within a received request):

  • AddSOAPResponseHeader() adds headers to a SOAP response before it is returned.

  • GetSOAPRequest() provides access to the complete SOAP request.

  • GetSOAPRequestHeader() provides access to received SOAP headers.

  • IsSOAPRequest() can be used to determine whether or not a CFC method was invoked as a Web Services method (as opposed to a direct CFC invocation or a Flash Remoting invocation).



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

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