Solved

CFC's and Variable Scope/Persistence

Posted on 2003-10-30
5
756 Views
Last Modified: 2013-12-24
I'm looking to create a web service that can be made available to anyone.  I want to start by creating the component object, running an initialization method to set the user's unique keys, and then make a number of methods available that will use those keys for their operations.

so, the component code:

<cfcomponent output = "false">
      
      <cfproperty name="AssociatesID" type="string">
      <cfproperty name="DeveloperKey" type="string">
      
      
      <!--- function to initialize the AssociateID of the component --->
      <cffunction name="AmazonSearchInitialize" access="remote" returntype="string" output="no">
      <!--- Define the AssociatesID argument --->
      <cfargument name="AssociatesID" type="string" required="yes">
      <cfargument name="DeveloperKey" type="string" required="yes">
      <!--- set the AssociatesID argument --->
      <cfset this.AssociatesID = arguments.AssociatesID>
      <cfset this.DeveloperKey = arguments.DeveloperKey>
      <cfreturn>
      </cffunction>
      
      <!--- function to perform an Amazon.com keyword search --->
      <cffunction name="AmazonKeywordSearch" access="remote" returntype="string" output="no">
      <cfargument name="mode" type="String" required="yes">
      <cfargument name="display_page" type="String" required="no" default="1">
      <cfargument name="search" type="String" required="yes">
      <cfset var AmazonURL = "http://xml.amazon.com/onca/xml3?t=webservices-20&dev-t=" & this.DeveloperKey & "&KeywordSearch=" & arguments.search & "&mode=" & arguments.mode & "&type=lite&page=" & arguments.display_page & "&f=xml">
      <cfhttp url = "#AmazonURL#" method = "get" timeout="60">
      <cfreturn cfhttp.fileContent>
      </cffunction>
      
      <!--- function to perform an Amazon.com ASIN search --->
      <cffunction name="AmazonProductDetails" access="remote" returntype="string" output="no">
      <cfargument name="asin" type="String" required="yes">
      <cfset var AmazonURL = "http://xml.amazon.com/onca/xml3?t=webservices-20&dev-t=" & this.DeveloperKey & "&AsinSearch=" & arguments.asin & "&type=heavy&f=xml">
      <cfhttp url = "#AmazonURL#" method = "get" timeout="60">
      <cfreturn cfhttp.fileContent>
      </cffunction>
</cfcomponent>


and the calling page:

<cfobject webservice="http://www.joezizzo.com/cfc/whatever.cfc?wsdl" name="AmazonResourceFeed">

<cfinvoke webservice="#AmazonResourceFeed#" method="AmazonSearchInitialize" AssociatesID="#associates_id#" DeveloperKey="#developer_key#">

<cfinvoke webservice="#AmazonResourceFeed#" method="AmazonKeywordSearch" mode = "#url.mode#" display_page = "#url.page#" search = "#urlencodedformat(url.search)#" returnVariable="AmazonKeywordSearchResults">


Now, it is my understanding that variables with THIS scope are available to all cfc methods, as well as the calling page, for the life of the object.  This does not seem to be the case.  I invoke AmazonSearchInitialize(), and then attempt to run AmazonKeywordSearch, and the THIS scope is not defined.  I've also tried every other scope (including unnamed).
I could set these two init variables at the top of the component with hard coding, but that would defeat the purpose of making this remotely available to many users, wouldn't it?
I could also require every function call (such as AmazonKeywordSearch) to take developer and associate keys as arguments, but that is unnecessarily redundant.

Any ideas as to how a function can be run to set initializing variables that will persist for every method invocation?

Thanks
0
Comment
Question by:joezizzo
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:anandkp
ID: 9655600
try
<cffunction ACCESS="public" ..... I had used this to make my variable in that particular function accessable by other functions in CFC

let me know if it helps !

K'Rgds
Anand
0
 
LVL 4

Expert Comment

by:procept
ID: 9656976
Hi,

sorry to say, but, that won't work out...

the this scope is handled differently in components and in web services. In a web service, if you set or change a this-var in one function, another function won't be aware of it.

If you set access to "public", the this scope will be visible again, but, then it's not a webservice any more. :-((

I don't know if it's a bug or intended, but, I'm going to ask some web service experts and post it here if I find out anything.

Chris
0
 
LVL 4

Expert Comment

by:procept
ID: 9657244
Hi,

just got some help that might help you. ;-))

"The webservice calls know nothing about each other. There's no persistence between the calls ...."

"If func1 needs to be called before func2 you may have to call func1 from within func2."

In your situation that would mean to change the second method:

<cffunction name="AmazonKeywordSearch" access="remote" returntype="string" output="no">
   <!--- arguments here --->
   <cfinvoke method="AmazonSearchInitialize">
    <!--- rest of code here --->
</cffunction>

HTH,

Chris

0
 
LVL 4

Accepted Solution

by:
procept earned 250 total points
ID: 9658646
Hi,

here's another comment I got:

> Seems like what you are looking for is an implicit constructor that is
> invoked prior to the actual method call being made by the web service
> consumer. There is no way to do this currently.

HTH,

Chris

0
 
LVL 17

Expert Comment

by:anandkp
ID: 9662077
u can have the main fucntion as a "remote" & the other internal functions as "public" so thigns can be worked with

like i said - i had done the same - when i got stuck with my functions - cos i was using them with flash - so it had to be kept as remote - but other fucntions used internally were with access as public.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
retrieving files from old server once DNS has changed 10 72
Use System DSN 6 91
UPLOAD FILE TO Web API USING POST 5 132
Web server settings related to keepalive 1 130
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question