会话变量和远程 CFC 调用
当我远程调用 CFC 时,为什么 ColdFusion 无法识别我的会话变量?
示例:
我有一个我调用的 CFC,使用:
http://www.mywebsite.com/CFC/myfunc.cfc?method=dosomething;arg1=foo;arg2=foo2
如果我将以下内容...
<cfdump var="#session#" abort>
...放在 myfunc.cfc 的第一行,我会看到正确显示的所有会话变量的 cfdump。但是,如果我执行一些简单的操作,例如:
<cfset myvar = session.datasrc>
我会收到 500 错误。元素 DATASRC 在 SESSION 中未定义。
session.datasrc 出现在 cfdump 中,如果我不远程访问它(例如使用
),它可以正常工作。我是否缺少 CFIDE 中的某些属性或设置?某些地方不允许远程调用 CFC 来访问会话变量。谢谢。
Why does ColdFusion not recognize my session variable 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果通过浏览器调用,对“远程”cfc 的调用确实会维持会话。
使用 http://localhost:8500/CFCs/your.cfc?method= 调用 cfc测试,您将看到会话 ID 将保持不变。将其命名为 http://localhost:8500/CFCs/your.cfc ?method=test&username=bob 并且您将设置会话变量,从 url 中删除参数,并且会话变量将被保留。
如果您在不使用浏览器的情况下调用远程 cfc,则需要“手动”传入 sessionid,如下所述 此处
Calls to a "remote" cfc do maintain session if called through a browser.
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
对 cfc 的远程调用没有任何会话意义。这总是一锤子买卖。
如果您通过上面的 url 访问 cfc,您将获得会话,因为这就像将其作为页面访问一样。 Web 服务器将把所有元素(例如会话 cookie)传递到服务器,使会话范围可供 cfc 使用。
如果您远程访问 cfc,则由于请求的方式,组件将无法使用这些内容。
您永远不应该依赖服务方法内的瞬态作用域变量。要么使用 Coldspring 之类的东西注入它们以提供对“配置”服务的依赖,要么将值作为参数传递。
坦率地说,会话范围绝对不是存储数据源名称的正确位置。鉴于您使用的是 ColdFusion 9,您可能有兴趣查看 Application.cfc 下的应用程序范围数据源设置
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
会话不是数据源等变量的正确范围,除非您为不同的用户提供不同的数据源。应用程序范围是应用程序范围变量的正确位置。我会首先尝试使用应用程序范围,看看您是否仍然遇到问题。还要检查变量名拼写等内容,并尝试通过 CFINVOKE 调用从应用程序内部本地调用该函数(如果需要,请将函数设置为公共)以查看它在本地是否正常工作。然后您就知道这可能与您远程调用它的方式有关。
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.