Solved

Hello World Flex/Webservice app - web service ResultEvent never triggered

Posted on 2009-04-14
19
1,482 Views
Last Modified: 2012-05-06
Hi, I've spent days trying to get Flex to display a simple string that says "Hello World" that is delivered from a WCF web service that is running on our server.  I have no problem getting this to work on an Asp.net web page where it takes me about 1 minute to write.

But in Flex, it seems to work differently.  It appears to be necessary to create an event listener and then wait to see if if it's triggered before the result can be displayed.  So I do that in the code below (utilizing classes Flex creates after specifying the wsdl).  

Problem is, the event apparently is never triggered so nothing ever happens.

Is there anything wrong with the Flex code?  Should I stop using WCF and use a different type of service?  I don't totally understand how the ResultEvent works or why it is necessary since I never had to do that in ASP.net.  

Is there something in the service that needs to be written to accommodate Flex requirements?

I'd really appreciate any help, anything just to get this really supposedly simple "Hello World" program to work!



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

<![CDATA[

	import com.myCompany.HelloResultEvent;

    import com.myCompany.Service_Upload;

    import mx.controls.Alert;

    import flash.system.Security;

    public function sayHello():void {

        var service:Service_Upload = new Service_Upload();

        Alert.show("test");  // This message box appears

        service.addhelloEventListener(

            function (event:HelloResultEvent):void {

                Alert.show(event.result);  // This message box never appears

            });

        service.hello();

    }

]]>

    </mx:Script>

    <mx:Button x="296" y="54" label="Click to receive greeting" click="sayHello()"/>

</mx:Application>

Open in new window

0
Comment
Question by:dparkes
  • 9
  • 5
  • 5
19 Comments
 
LVL 19

Expert Comment

by:Jones911
ID: 24140869
What are you trying to do retrieve some data from .NET?  Or send something to .NET?
0
 
LVL 5

Expert Comment

by:Fuzzy_Logic_
ID: 24141043
Your service will be returning a FaultEvent, which is why nothing appears to happen.

Could you add a FaultEvent?

Add a listener and get the error message.



0
 
LVL 1

Author Comment

by:dparkes
ID: 24141477
Jones, I am trying to retrieve data.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 24141494
If you run it in debug mode can u see it erroring anywhere?

Can you post the com.myCompany.Service_Upload; code?
0
 
LVL 1

Author Comment

by:dparkes
ID: 24141625
I'm not getting any errors, although I'm still trying to figure out how to add a FaultEvent, as FuzzyLogic suggests.  But I have a feeling the FaultEvent won't trigger either.

Below is the code for the web service.

Thanks.
Service_Upload.cs
 

using System;

using System.ServiceModel;

using System.ServiceModel.Activation;
 

namespace WcfServiceLibrary_Upload

{

    public class Service_Upload : IService_Upload

    {

        public string Hello()

        {

            return string.Format("Hello World!");

        }

    }

}
 
 
 
 

IService_Upload.cs
 

using System;

using System.Collections.Generic;

using System.ServiceModel;

using System.Text;
 

namespace WcfServiceLibrary_Upload

{

    [ServiceContract]

    public interface IService_Upload

    {

        [OperationContract]

        string Hello();

    }

}

Open in new window

0
 
LVL 19

Expert Comment

by:Jones911
ID: 24141650
I don't understand how can u import c# into Flex?

var service:Service_Upload = new Service_Upload();  on this lkine are you trying to make c# calls?
0
 
LVL 1

Author Comment

by:dparkes
ID: 24141657
Hones, here is the code for Service_Upload.as that was automatically generated:

/**

 * Service_UploadService.as

 * This file was auto-generated from WSDL by the Apache Axis2 generator modified by Adobe

 * Any change made to this file will be overwritten when the code is re-generated.

 */

 /**

  * Usage example: to use this service from within your Flex application you have two choices:

  * Use it via Actionscript only

  * Use it via MXML tags

  * Actionscript sample code:

  * Step 1: create an instance of the service; pass it the LCDS destination string if any

  * var myService:Service_Upload= new Service_Upload();

  * Step 2: for the desired operation add a result handler (a function that you have already defined previously)  

  * myService.addHelloEventListener(myResultHandlingFunction);

  * Step 3: Call the operation as a method on the service. Pass the right values as arguments:

  * myService.Hello();

  *

  * MXML sample code:

  * First you need to map the package where the files were generated to a namespace, usually on the <mx:Application> tag, 

  * like this: xmlns:srv="com.cushingco.*"

  * Define the service and within its tags set the request wrapper for the desired operation

  * <srv:Service_Upload id="myService">

  *   <srv:Hello_request_var>

  *		<srv:Hello_request />

  *   </srv:Hello_request_var>

  * </srv:Service_Upload>

  * Then call the operation for which you have set the request wrapper value above, like this:

  * <mx:Button id="myButton" label="Call operation" click="myService.Hello_send()" />

  */

package com.cushingco

{

	import mx.rpc.AsyncToken;

	import flash.events.EventDispatcher;

	import mx.rpc.events.ResultEvent;

	import mx.rpc.events.FaultEvent;

	import flash.utils.ByteArray;

	import mx.rpc.soap.types.*;
 

    /**

     * Dispatches when a call to the operation Hello completes with success

     * and returns some data

     * @eventType HelloResultEvent

     */

    [Event(name="Hello_result", type="com.cushingco.HelloResultEvent")]

    

	/**

	 * Dispatches when the operation that has been called fails. The fault event is common for all operations

	 * of the WSDL

	 * @eventType mx.rpc.events.FaultEvent

	 */

    [Event(name="fault", type="mx.rpc.events.FaultEvent")]
 

	public class Service_Upload extends EventDispatcher implements IService_Upload

	{

    	private var _baseService:BaseService_Upload;

        

        /**

         * Constructor for the facade; sets the destination and create a baseService instance

         * @param The LCDS destination (if any) associated with the imported WSDL

         */  

        public function Service_Upload(destination:String=null,rootURL:String=null)

        {

        	_baseService = new BaseService_Upload(destination,rootURL);

        }

        

		//stub functions for the Hello operation

          
 

        /**

         * @see IService_Upload#Hello()

         */

        public function hello():AsyncToken

        {

         	var _internal_token:AsyncToken = _baseService.hello();

            _internal_token.addEventListener("result",_Hello_populate_results);

            _internal_token.addEventListener("fault",throwFault); 

            return _internal_token;

		}

        /**

		 * @see IService_Upload#Hello_send()

		 */    

        public function hello_send():AsyncToken

        {

        	return hello();

        }

              

	  		/**

		 * Internal variable to store the operation's lastResult

		 * @private

		 */

        private var _hello_lastResult:String;

		[Bindable]

		/**

		 * @see IService_Upload#Hello_lastResult

		 */	  

		public function get hello_lastResult():String

		{

			return _hello_lastResult;

		}

		/**

		 * @private

		 */

		public function set hello_lastResult(lastResult:String):void

		{

			_hello_lastResult = lastResult;

		}

		

		/**

		 * @see IService_Upload#addHello()

		 */

		public function addhelloEventListener(listener:Function):void

		{

			addEventListener(HelloResultEvent.Hello_RESULT,listener);

		}

			

		/**

		 * @private

		 */

        private function _Hello_populate_results(event:ResultEvent):void

		{

			var e:HelloResultEvent = new HelloResultEvent();

		            e.result = event.result as String;

		                       e.headers = event.headers;

		             hello_lastResult = e.result;

		             dispatchEvent(e);

	        		}

		

		//service-wide functions

		/**

		 * @see IService_Upload#getWebService()

		 */

		public function getWebService():BaseService_Upload

		{

			return _baseService;

		}

		

		/**

		 * Set the event listener for the fault event which can be triggered by each of the operations defined by the facade

		 */

		public function addService_UploadFaultEventListener(listener:Function):void

		{

			addEventListener("fault",listener);

		}

		

		/**

		 * Internal function to re-dispatch the fault event passed on by the base service implementation

		 * @private

		 */

		 

		 private function throwFault(event:FaultEvent):void

		 {

		 	dispatchEvent(event);

		 }

    }

}

Open in new window

0
 
LVL 5

Expert Comment

by:Fuzzy_Logic_
ID: 24141844
Here is how you would get the FaultEvent:
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

<![CDATA[

    import com.myCompany.HelloResultEvent;

    import com.myCompany.Service_Upload;

    import mx.controls.Alert;

 

    import mx.rpc.events.FaultEvent; // import fault event as raised by Service_Upload.as

                                 // [Event(name="fault", type="mx.rpc.events.FaultEvent")]
 
 

    import flash.system.Security;

    public function sayHello():void {

        var service:Service_Upload = new Service_Upload();

        Alert.show("test");  // This message box appears
 

        service.addhelloEventListener(                    

            function (event:HelloResultEvent):void {

                Alert.show(event.result);  // This message box never appears
 

        service.addhelloEventListener(                   // add FaultEvent listener

            function (event:FaultEvent):void {

                Alert.show(event.fault);  // display error message.

            });

        service.hello();

    }

]]>

    </mx:Script>

    <mx:Button x="296" y="54" label="Click to receive greeting" click="sayHello()"/>

</mx:Application>

Open in new window

0
 
LVL 1

Author Comment

by:dparkes
ID: 24141894
FuzzyLogic,

You are right, the FaultEvent did provide a result: Security Error Accessing URL.

I wonder if this is related to a previous problem with Flex confusing our internal domain name and the external domain name, although all references that I know of now point to the internal name....  So I'm not sure.
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 

<mx:Script>

<![CDATA[

    import com.myCompany.HelloResultEvent;

    import com.myCompany.Hello_request;

    import com.myCompany.Service_Upload;

    import mx.controls.Alert;

    import mx.rpc.events.FaultEvent;

    import flash.system.Security;

    public function sayHello():void {

        var service:Service_Upload = new Service_Upload();

        service.addhelloEventListener(

            function (event:HelloResultEvent):void {

                Alert.show(event.result);  // This message box never appears

            });

        service.addService_UploadFaultEventListener(

            function (event:FaultEvent):void {

                Alert.show(event.fault.faultString, "Error");   

            });

        service.hello();

    }

]]>

    </mx:Script>

    

	<mx:Button x="289" y="55" label="Button" click="sayHello();"/>

	

</mx:Application>

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:dparkes
ID: 24141931
FL:

Now I am getting: HTTP Request Error.

(I actually had to rename the debug URL again so it matches the internal name)
0
 
LVL 1

Author Comment

by:dparkes
ID: 24141963
Jones,

The c# code is what is on the ASP.NET server, not in the Flex code.  I guess you did not ask for that, so that's why I also added the Service_Upload.as file.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 24142061
Yep after the 2nd code block I figured that.

Whats the HTTP error?
0
 
LVL 1

Author Comment

by:dparkes
ID: 24142475
How do I get details for this "HTTP Request Error"?  Do you think this could be a WCF/Flex3 compatibility issue... should I just give up and use some other kind of service?

0
 
LVL 19

Assisted Solution

by:Jones911
Jones911 earned 200 total points
ID: 24142526
Well it depends what your failure with.  I use Coldfusion and Flex so that's my preference.  If you have access to CF use that but I would stick with what you know best.

Take a look at this: http://www.themidnightcoders.com/products/weborb-for-net/overview.html
0
 
LVL 5

Expert Comment

by:Fuzzy_Logic_
ID: 24142772
you have a mismatch in package locations?

Service_Upload is in com.cushingco yet you are importing com.myCompany packages
0
 
LVL 5

Expert Comment

by:Fuzzy_Logic_
ID: 24142907
Somewhere in the code you must be telling flex to get the service from somewhere, where is that somewhere?
0
 
LVL 1

Author Comment

by:dparkes
ID: 24142997
Nah, I was just trying to avoid putting my company name in the posts by doing a search/replace in the text, I just missed that one.
0
 
LVL 5

Accepted Solution

by:
Fuzzy_Logic_ earned 300 total points
ID: 24143149
To get more details on the fault change this:


       service.addService_UploadFaultEventListener(
            function (event:FaultEvent):void {
                Alert.show(event.fault.faultString, "Error");  
            });

to:

       service.addService_UploadFaultEventListener(
            function (event:FaultEvent):void {
                Alert.show(event.fault.faultDetail, "Error");   // <- change here
            });



0
 
LVL 1

Author Comment

by:dparkes
ID: 24143379
Thanks for both your help on this.  

Because I can, I just tried using the more traditional asmx based web service, instead of the WCF web service.  

Got Hello World running in 10 minutes, using the asmx web service.

FuzzyLogic, yes I got a better report from the previous project:

Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://ftp-server.local.myCompany.com/Service_Upload.svc/basic"]. URL: http://ftp-server.local.myCompany.com/Service_Upload.svc/basic

Still not sure what that means, but it's all academic for now since I got HelloWorld working with Asmx, and will continue doing it the old fashioned way until maybe Flex 4 comes out with better WCF integration.

Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial will show you how to add an attribute to an XML (http://en.wikipedia.org/wiki/XML) stream returned from a Windows Communication Foundation (http://en.wikipedia.org/wiki/Windows_Communication_Foundation) (WCF) Web Service.  Some knowled…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now