Solved

CFC's and Variable Scope/Persistence

Posted on 2003-10-30
5
748 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now