CFC's and Variable Scope/Persistence

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
LVL 1
joezizzoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anandkpCommented:
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
proceptCommented:
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
proceptCommented:
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
proceptCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anandkpCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.