I would like to connect to a .NET code with my Flex application.
WebORB is an excellent option for connecting Flex application. With WebORB interface you can exchange data using AMF, WDMF, RTMP protocol. In this example I will give a short explanation what to do when you install WebORB and how to connect your Flex Application with your .NET server application.
WebORB is a development and runtime environment for connecting Flex, Flash, Silverlight and Ajax applications with a server technologies such as PHP, .NET, Java, Coldfusion and Ruby at Rails.
To connect your .NET application and your Flex client application you need to download WebORB for .NET server application and install it on your web server. Here you can find the latest version: www.themidnightcoders.com/products/weborb-for-net/overview.html
Follow the instructions during installation. Also you can find informations about the instalation of WebORB on weborb homepage. If you are installing WebORB on your local server, fter the installation procedure you should have the access to WebORB app on this URL: http://localhost/weborb30/.
Now, you should create your code for your server application. In your Visual Studio you should create a new project. Use a template that is now integrated in your Visual Studio - 'Class Library with WebORB'.
Now, you can create a simple class like I created in attached archive.
using System;
using System.Collections.Generic;
using System.Text;
namespace FleFlexWebOrb.ZgFlex
{
public class WebOrbConnect
{
public Response sayHello(string name)
{
Response response = new Response(name);
return response;
}
}
public class Response
{
public string Name;
public string Greeting;
public Response(string name)
{
this.Name = name;
}
}
}
Compile/Rebuild the solution and you will get an DLL file in debug directory. Copy this file into WebORB\bin directory and you will have a new service available. You can test the service with WebORB console (http://localhost/weborb30/weborbconsole.html)
All done? Excellent. Let's create a Flex application that will connect to this WebORB service. Create a new Flex project and add a SWC library that is located in weborb\weborbassets\wdm directory.
Also you need to modify a compiler path by adding:
-services c:\Inetpub\wwwroot\weborb30\web-inf\flex\services-config.xml
Now you should create a simple application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.*;
import mx.collections.*;
import mx.controls.*;
private function faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.faultString, "Error");
}
private function getHelloGreetingHandler(event:ResultEvent):void
{
Alert.show(event.result.Greeting);
}
]]>
</mx:Script>
<mx:RemoteObject id="WebOrbConnect"
destination="GenericDestination"
source="FlexWebOrb.ZgFlex.WebOrbConnect"
showBusyCursor="true"
fault="faultHandler(event)" >
<mx:method name="sayHello" result="getHelloGreetingHandler(event)"/>
</mx:RemoteObject>
<mx:Panel x="24" y="26" width="355" height="254" layout="absolute" title="Connecting to WebOrb">
<mx:TextInput x="89" y="28" width="236" id="txtName"/>
<mx:Label x="10" y="30" text="Your name:"/>
<mx:Button x="244" y="67" label="Connect" width="81" click="WebOrbConnect.sayHello(txtName.text)"/>
</mx:Panel>
</mx:Application>
This dummy application sends user name and returns a welcome note. With this Flex code you are using RemoteObject to call FlexWebOrb.ZgFlex.WebOrbConnect .NET class and it's sayHello method that gets parameter Name and it sends a Response type object that contains a name and a Greeting string.
And that's it. In the next recipe I will show you how to send an object instance from Flex app to .NET server and how to get an object from .NET to Flex application. Have fun with WebORB.