Session Variables and Remote CFC Calls
Why does ColdFusion not recognize my session va开发者_开发技巧riable when I remotely call a CFC?
Example:
I have a CFC that I call using:
http://www.mywebsite.com/CFC/myfunc.cfc?method=dosomething;arg1=foo;arg2=foo2
If I put the following...
<cfdump var="#session#" abort>
...on the very first line of myfunc.cfc, I see a properly displayed cfdump of all of my session variables. However, if I do something simple like:
<cfset myvar = session.datasrc>
I get a 500 error. Element DATASRC is undefined in SESSION.
session.datasrc appears in the cfdump and if I don't access it remotely (like with a <cfinvoke>
) it works fine. Am I missing a some property, or a setting in CFIDE? Something somewhere isn't allowing for remote calls to CFC's to access session variables. Thanks.
Calls to a "remote" cfc do maintain session if called through a browser.
<cffunction access="remote" returntype="any" output="No" hint="this hint" name="test">
<cfargument required="false" name="username" type="string" default=""/>
<cfset session.username="#arguments.username#">
<cfreturn session>
</cffunction>
Call the cfc with http://localhost:8500/CFCs/your.cfc?method=test and you will see the session id will remain constant. Call it as http://localhost:8500/CFCs/your.cfc?method=test&username=bob and you will set the session variable, strip off the argument from the url and the session variable is persisted.
If you are calling the remote cfc without using a browser, you will need to "manually" pass in the sessionid as discussed here
Remote calls to a cfc don't have any sense of session. It's always a one shot deal.
If you access the cfc via the url as you have above, you'll gets session because it's like accessing it as a page. The web server will pass through all the elements, such as session cookies, through to the server making the session scope available to the cfc.
If you access the cfc remotely none of this will be available to the component because of the way it was requested.
You should never rely on transient scope variables inside of service methods. Either inject them using something like coldspring to provide dependancy on a "configuration" service or pass values in as arguments.
To be blunt with you, the session scope is absolutely not the right place to be storing datasource names. Given that you're on ColdFusion 9, you might be interested in looking at the application wide datasource setting under Application.cfc
The session isn't the proper scope for variables like a datasource unless you have different datasources for different users. The application scope is the proper location for application-wide variables. I would first try using the application scope and see if you are still having issues. Also check things like variable name spelling, and try to hit that function locally from within the application via a CFINVOKE call (set the function to public if you need to) to see if it works fine locally. Then you know it's probably something with the way you are calling it remotely.
精彩评论