TypeError: Error #1034: Type Coercion failed: cannot convert Object@1456c7b9 to mx.messaging.messages.IMessage

actionscript-3apache-flexcastingflash

Im trying to connect a Flash client to BlazeDS. There has been some success with this from others using the vanilla BlazeDS setup. However I'm using the new Spring BlazeDS Integration from springsource and running aground.
The flash client actually seems to be working in that I can see the correct data in the body of the returned object, but for some reason unknown it fails casting as an IMessage. It fails in PollingChannel.as on this line with the subject line error

            var messageList:Array = msg.body as Array;
            for each (var message:IMessage in messageList)  <--

On application load I register a whole bunch of classes like so

registerClassAlias( "flex.messaging.messages.RemotingMessage", RemotingMessage );
registerClassAlias("mx.messaging.messages.IMessage", IMessage);
etc..

my code is basically

        var channelSet:mx.messaging.ChannelSet = new mx.messaging.ChannelSet();
        var channel:mx.messaging.channels.AMFChannel = new AMFChannel("my-amf", "http://localhost:8400/SpringA/messagebroker/amf");
        channelSet.addChannel(channel);

        var consumer:mx.messaging.Consumer = new Consumer();
        consumer.channelSet = channelSet;
        consumer.destination = "simple-feed";
        consumer.subscribe();
        consumer.addEventListener(MessageEvent.MESSAGE, test);


    private function test(event:IMessage)
    {
        trace("msg..");
        // breakpoint never makes it here
    }

I have a flex client which works 100% with same destination/channel.

Best Answer

The error in the title means that you, for some reason, got an object that is not implementing or extending the IMessage interface, therefore the loop can not cast it in this part:

for each (var message:IMessage in messageList){

Either you should somehow make sure that you don't add anything that is not extending or implementing IMessage, or check if the variable IS actually ext./imp. it. Also - if you want to do that, you will have to change the for each like this:

for each (var obj in messageList){
    if (obj is IMessage){
        var message:IMessage = obj as IMessage;
        // DO STUFF HERE
    }
}
Related Topic