Andrew Stopford's Weblog

@poobah

News

Articles

Family

Old Blogs

Flex and .NET web services pt1

Seen a few requests for .NET web service and Flex examples, here is a quick example.

First create a web service function (my code is in VB.NET but you get the idea).

<WebMethod()> _
Public Function SayHello(ByVal strMessage As String) As String
 
    Return "Hello " & strMessage
End Function


This amazingly complex bit of code echo's out it's arguement with the string "Hello" attached.

Now we will rig to Flex using the following code.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="
http://www.macromedia.com/2003/mxml" width="600" height="450">
   <mx:WebService id="EchoStuff" wsdl="
http://localhost/EchoService/Echo.asmx?WSDL" showBusyCursor="true" fault="alert(event.fault.faultstring)">
   <mx:operation name="SayHello">
       <mx:request>
        <strMessage>{name.text}</strMessage>
       </mx:request>
   </mx:operation>
</mx:WebService>
<mx:VBox>
<mx:TextInput id="name"></mx:TextInput> 
<mx:Button click="EchoStuff.SayHello.send()"></mx:Button>
<mx:Label text="{EchoStuff.SayHello.result}"></mx:Label>
</mx:VBox>
</mx:Application>

Here we we will use a textbox (Flex calls them textinput) and on the button click event send it's contents to the web service. A label will then display the contents back. A few notes here.

Web services are called using the following.

   <mx:WebService id="EchoStuff" wsdl="http://localhost/EchoService/Echo.asmx?WSDL" showBusyCursor="true" fault="alert(event.fault.faultstring)">
   <mx:operation name="SayHello">
       <mx:request>
        <strMessage>{name.text}</strMessage>
       </mx:request>
   </mx:operation>
</mx:WebService>

Note that the we specify the location to the WSDL for our webservice. We then map mx:operation to the method name of our web service. To send a request to the web service we use mx:request and within that the parameter name and what data we will send (in this case the textinput contents). To trigger the web service we use

EchoStuff.SayHello.send()">

which is tied to the button click event (it must have an event in order to trigger). The result can then be obtained using

EchoStuff.SayHello.result

Note that everything is case senstive so watch how you declare and make use of things within Flex. One of the interesting things I found was that if you get the case wrong when using mx:operation some odd things start happening. For example if you use.

<mx:operation name="Sayhello">

The web service method fires but the mx:request is not passed.

Comments

Aaron said:

Do have any examples where the web service returns XML?

# July 29, 2006 1:04 AM

Partha said:

Hi,

I was trying the above example, but didn't succeeded. The webservice which I have created in VB.Net is working fine(individually). But when I post any value from the flex application, it is getting submitted but returning nothing. No error message displayed. Can I have further help on this?

# September 18, 2006 9:00 AM

Gilbert said:

I have the same exact issue as the above.

# September 28, 2006 5:38 PM

Sajid said:

Hi ...

I want to do same thing in flex 2.0 ??

# October 11, 2006 5:14 AM

Glamour Models said:

More or less nothing seems worth doing. I've just been letting everything wash over me lately. I just don't have anything to say lately. Shrug. I can't be bothered with anything lately.

# February 2, 2007 3:55 PM

adult dating personals said:

College education is a basic need today

# February 4, 2007 8:01 PM

all internal said:

alguem sabe me dizer se e possivel rotacionar uma imagem com angulo de 30?, 45?, ... (em css, javascript, sei la, qualquer

# February 5, 2007 6:14 PM

fatal beauty said:

We are killing the rainforest

# February 6, 2007 7:44 AM

Yamila Diaz said:

Easter, Christmas is the best holiday - Or choose your own favorite and create another persuasive speech topic

# February 9, 2007 1:27 PM

big ass said:

People are less moral than 30 years ago

# February 12, 2007 8:45 AM

dripping *** holes said:

Quer aprender ASP?! Veja este material gratuito

# February 15, 2007 8:07 PM

give me pink said:

Fast food causes obesity

# February 16, 2007 6:57 PM

alternative dating personals said:

animals don't belong in zoos

http://funnyblog.us/post_1164981673.html

Thank

# February 18, 2007 7:42 AM

gothic girl said:

Drivers who run red lights should be punished severly

# February 20, 2007 9:31 PM

Adriana Lima said:

A one-night stand is wrong, is not wrong

# February 21, 2007 6:58 AM

latex girls said:

Needle exchange programs help to prevent

# February 22, 2007 7:20 PM

sperm swap said:

animals don't belong in zoos

# February 28, 2007 8:33 PM

Alessia Merz said:

Vanity is not a valid reason for cosmetic plastic surgery

# March 1, 2007 8:18 PM

Jacob said:

@Abi Titmuss

To get it working in Flex 2.0 use lastResult instead of result.

# April 24, 2007 4:57 AM

Demetris said:

Nice

# May 18, 2007 3:20 PM

Demetri said:

Sorry :(

# May 18, 2007 5:29 PM

Ignatios said:

Cool!

# May 18, 2007 5:43 PM

Rhigas said:

Cool.

# May 18, 2007 6:41 PM

Ivan said:

Nice

# May 19, 2007 2:41 PM

Anaklets said:

interesting

# May 21, 2007 12:00 PM

Gregorios said:

Sorry :(

# May 22, 2007 6:07 PM

Kosta said:

Cool...

# June 8, 2007 2:18 AM

Athanassios said:

Nice!

# June 10, 2007 1:48 PM

Thanasios said:

Nice...

# June 11, 2007 10:54 PM