Solved

CFC's and Variable Scope/Persistence

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iis reverse proxy virtual directory 8 132
connection string question 2 67
Website being blocked? 3 122
UPLOAD FILE TO Web API USING POST 5 105
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 …
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

22 Experts available now in Live!

Get 1:1 Help Now