ASyncToken and IResponder

So here is a weird little problem I ran into while using the generated webservice code in Flex 3. Basically if using the ASyncToken returned by the proxy class with an IResponder, the token never triggered any of the IResponder we never got a response. We tried to use the standard code like:


var service:MyWebService = new MyWebService();
var token:ASyncToken = service.myTestFunction();
token.addResponder(this);

However the responder functions (result and fault) are never called. The easy work around is using addEventListener, but it doesn’t explain why the ASyncToken never fires. I know in Flex 3 they extended the ASyncToken to user an array of responders instead of just one, unfortunately I have not been able to trace the dispatch code to the level required to see why the following sample fails. Am I just completely misunderstanding the framework??

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.rpc.IResponder" layout="vertical">

<mx:Script>

<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.rpc.AsyncToken;
import mx.controls.Alert;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;

import mx.core.mx_internal;
use namespace mx_internal;

public function result(data:Object):void
{
Alert.show("result");
}
public function fault(info:Object):void
{
Alert.show("fault");
}

public function test():void
{
var token:AsyncToken = new AsyncToken(null);
token.addResponder(this);
Alert.show("dispatching"); token.dispatchEvent(ResultEvent.createEvent(result,token,null));

ResultEvent.createEvent(result,token,null).callTokenResponders();
}

public function testWithEventListener():void
{
var token:AsyncToken = new AsyncToken(null);
Alert.show("this works");

token.addEventListener(ResultEvent.RESULT, result);

token.dispatchEvent(ResultEvent.createEvent(result,token,null));

}

]]>
</mx:Script>
<mx:Button label=”TestMe” click=”test();” />

</mx:Application>

Update 4/23:

I added some code to the above sample (highlighted in Red) to show how to get the token to actually call the responders. The weird thing is a) the method is mx_internal b) the generated webservice does not reference it. Seems like they expect you to use addEventListener instead of the token responders.

5 Responses to “ASyncToken and IResponder”

  1. Nate Says:

    I am having this exact same problem. I was excited to find the post but the fact two of us are having this problem doesn’t really help! :0

    How did you get it to work with addEventListener?

  2. Nate Says:

    Actually, that was easy. Just in case someone else needs this:

    token.addEventListener(ResultEvent.RESULT, responder.result);
    token.addEventListener(FaultEvent.FAULT, responder.fault);

  3. mbir Says:

    Sorry, I should have listed the code for addEventListener. And yes, that was exactly how we worked around the issue.

    Still hoping someone out there can explain why it doesn’t work. This happens consistently with the WSDL generated code, however if I use a standard HTTPService then it works fine with the IResponder…..

  4. mbir Says:

    Found the issue, turns out the Base”WebService” class that extends AbstractWebService is missing calls to callTokenResponders. There are two methods that must be modified: processResult and faultResult. Before each call to token.dispatchEvent(event) there should be a call to event.callTokenResponders(). (3 places total)

    I figured this out by comparing how the code in the HTTPService works versus the generated WSDL code.

    Gotta admit I’m at a loss as to why the generator needs to create so much code for the wrapper, most of the code looks like it could easily be abstracted to a parent class.

    I’ve updated the code above to demonstrate how tokens are apparently supposed to be used.

    For anyone interested I created the following Adobe issue:
    https://bugs.adobe.com/jira/browse/FB-12491
    so please vote if you want to use Responders with your imported webservices.

  5. Kenneth Garee Says:

    Marc,

    Thank you for this post. You’re fix worked like a charm. I was banging my head on this for way, way too long.

    Cheers,
    Ken

Leave a Reply