Flex supports Flash Remoting as well as web services and the best bit is that calling out to .NET objects is a breeze. In order to use Flash Remoting to call out to .NET objects you will need Flash Remoting installed server side. This article shows how to use Macromedia's soloution but you could also use Flash ORB.
With Flash Remoting installed I will use the default folder that it sets up, setting up your own is easy though. So first things first we create our .NET assembly.
Imports FlashGateway.IO
Public Class RemotingTests
'returns what ever value is passed
Public Function EchoString(ByVal strValue As String)
Return strValue
End Function
End Class
This code echos back what ever value is passed. Note the inclusion of the Flashgateway.IO namespace, this is sourced from the flashgateway.dll assembly that you must referance. With this compiled copy the assembly into the bin folder of the default samples folder that the Flash remoting setup sets up. The next step is to add the Flash Remoting gateway to Flex. Open up your applications flex-config.xml file and add your gateway as follows.
<amf-gateway>http://localhost/flashremoting/gateway.aspx</amf-gateway>
This is the default Flash Remoting gateway that Flash Remoting sets up.
With this in place we can now write our Flex code.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" width="600" height="450">
<mx:Script>
var MyResult;
</mx:Script>
<mx:RemoteObject id="test" encoding="AMF" source="FlashRemotingCode.RemotingTests"
fault="alert(event.fault.faultstring, 'Error')" result="MyResult=event.result">
<mx:method name="EchoString"/>
</mx:RemoteObject>
<mx:Label text="{MyResult}">
</mx:Label>
<mx:Button click="test.EchoString('Flex and Flash Remoting rocks')">
</mx:Button>
</mx:Application>
Here I have created a simple Flex app that calls out to our .NET object via Flash Remoting passing a hard coded value. Note that the source attribute points to the .NET object fully referanced, that is any namespace names and then the class name (we will assume the default namespace for our .NET object is FlashRemotingCode).