Skip to main content
ilssac
Inspiring
January 7, 2010
Question

Aren't web services supposed to be easy?

  • January 7, 2010
  • 1 reply
  • 11659 views

my web service.

<cfcomponent>
    <cfproperty name="id" default="10" type="numeric">
 
  <cfset variables.id = 10>
 
    <cffunction name="setId" access="remote" returntype="void">
        <cfargument name="id" type="numeric" required="yes">
        <cfset variables.id = arguments.id>
    </cffunction>
 
  <cffunction name="getId" access="remote" returntype="numeric">
      <cfreturn variables.id>
  </cffunction>
</cfcomponent>

my test page.

  <p>Call component as web service</p>
  <cfset myWS = createObject("webservice","http://10.104.106.39:8080/ian.cfc?wsdl")>
  <cfoutput>#myWS.getId()#</cfoutput>
  <cfset myWS.setId(888)>
  <cfoutput>#myWS.getId()#</cfoutput>
  <cfdump var="#myWS#">

Both outputs are outputing the same value of 10.  The web service does not seem to be maintaining state from one method call to another.

This topic has been closed for replies.

1 reply

Inspiring
January 7, 2010

My approach would be to use the this scope instead of the variables scope if I wanted to be able to change the value from the calling page.

ilssac
ilssacAuthor
Inspiring
January 7, 2010

With the help of others, I have determined that the problem is that web services do not maintain state.  The the this and variables scopes are not maintinaed from request to request.

Inspiring
January 8, 2010

Sorry to not reply to this earlier Ian, but I spotted it when I was in the pub, and was more inclined to focus on my pint than posts on the forums ;-).  It's now the morning after the night before, hangover in place.

With the help of others, I have determined that the problem is that web services do not maintain state.  The the this and variables scopes are not maintinaed from request to request.

This caught me out a while ago too.  If one thinks about it, it makes sense.  Despite using createObject() to stick an instance of the web service in a variable, it's not as if the web service object itself (which is on a remote computer) is stored in the variable: it's just the stub on the CF end that's in the variable.  CF does not go and grab an instance of the actual web service and store it locally in your variable.  How would it do that, for a start?  And, really, is it desirable?  Generally I'd say "no": you want to be hitting the remote system with each call.

Each call to a web service is its own request, and its own instantiation of [whatever] on the other end of the call.  It's entirely possible that the web service is a facade for some other persisted object, in which case state and other scopes can be maintained between requests; but that's entirely down to how the web service is written, and nothing to do with how it is called.

--

Adam