Creating and Using Web Service CFCs

     

As you saw in the previous section, the differences in format between a custom tag and a single-function component are minor. The difference in potential, however, is significant, because a CFC has the potential to be accessed remotely in three ways: a URL parameter (including a URL parameter generated automatically when a form is submitted), Flash Remoting, or a Web service invocation.

For a function within a CFC to respond as a Web service, it must be formatted with four parameters: name , access (set to "remote"), returnType , and output (set to "no" or "false"). For example, this is the start tag for the getSiteName function:

 <cffunction name="getSiteName" access="remote"     returnType="string" output="false"> 

If the function doesn't return anything, set returnType to "void" .

The output attribute must be set to "no" or "false" because ColdFusion converts all Web service output to XML.

WARNING

When a CFC is invoked as a Web service, all arguments will be considered required, even if the required attribute of the argument is set to "false" .


There are a number of approaches to passing parameters to CFCs. Here is a CFC with a getSiteName function, with one (useless) argument added. (This CFC was largely generated by Dreamweaver, with only the words "The Green Department" added by hand.)

The getSiteName function was auto-generated in Dreamweaver, as described in "Creating a Web service CFC," page 314 , in Chapter 13.


 <cfcomponent displayName="Get Site Name">     <cffunction name="getSiteName" access="remote" returnType="string" output="false">         <!--- getSiteName body --->         <cfargument name="uselessArgument" required="no">         <cfreturn "The Green Department">     </cffunction> </cfcomponent> 

When invoking this CFC, you can provide the required argument in three ways. (And it is required for Web service access.) The following examples assume that the name of the CFC file is getSiteName.cfc , and that it is in the Web service directory under the Web root ( wwwroot ).

  • With the cfinvokeargument tag:

     <cfinvoke  webservice="http://www.someDomain.com/webservice/getSiteNameArg.cfc?wsdl"  method="getSiteName"  returnvariable="aString">  <cfinvokeargument name="uselessArgument" value = "useless"> </cfinvoke> 

  • As a named attribute-value pair:

     <cfinvoke  webservice="http://www.someDomain.com/webservice/getSiteNameArg.cfc?wsdl"  method="getSiteName"  uselessArgument = "useless"  returnvariable="aString"> </cfinvoke> 

  • As a structure, in the argumentCollection attribute:

     <cfset argumentStruct = StructNew()> <cfset argumentStruct.uselessArgument = "useless"> <cfinvoke  webservice="http://www.someDomain.com/webservice/getSiteName.cfc?wsdl"  method="getSiteName"  argumentcollection="#argumentStruct#"  returnvariable="aString"> </cfinvoke> 

Each of the foregoing methods can be extended to provide any number of arguments.

The last approach can be used to hand over an entire scope to the CFC. For instance, to submit a form to a CFC, you can use

 argumentcollection="#FORM#" 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

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