Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CFC's and Variable Scope/Persistence

Posted on 2003-10-30
5
Medium Priority
?
760 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 750 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

886 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