The following example will help you connect your Flex application to C# code via FluorineFx.While this is easy, lack of proper example makes the job tough for beginners like me. Since after googling and searching the web I was finally able to do it, I thought it would be good to post an example.
The following example will assist you with communicating with C# code using Remoting. The Flex application will create an object of the remote class and execute a function of the remote class.
Since Flex uses AMF and C# uses SOAP for remoting we need a gateway like FLuorinFx (or WebORB).
Requirements :
You will need the following softwares.
1. Adobe Flex 3.0 (or above)
2. MS Visual Studio 2005 (or above)
3.FluorineFx (available at http://www.fluorinefx.com/download.html)
Create an empty solution in VS 2005. Add a FluorineFx class library and a FluorineFx website to the solution. Implement the following class in your class library.
namespace MyDemoLib
{
[RemotingService]
public class Math
{
public Math()
{
}
public int add(int x,int y)
{
return x + y;
}
}
}
Build the project and run it. The class library is automatically built and added in the references section of the website. If you create a different project for the two then add the dll of the class library manually.
Now comes the Flex part.
Create a simple project in flex.
I named it "DemoProjectFluorine". Since we are taking a simple example we will design the main application page to accept the user input. If you have decided to make a more complex project the you would probably make a custom mxml component and then use it in the main application page.
The code is as below.
In the above code we create a RemoteObject and use this object to call the remote method.
One important point is to include a configuration file in the flex project that helps to resolve the uri to the appropriate website ( which we created and ran before). Also include this file in the Flex compiler settings.
You can do this by a right click on the flex project -> properties ->Flex compiler and include the path to this configuration file in the additional compiler arguments.
"-services "services-config.xml" "
The configuration file is as below.
Please note the endpoint URI for the above is at port 1425, please change it to the port on which your ASP.NET website runs.
All is done now you are ready to go.
Just for fun add a breakpoint to the method in the website and see the magic. When you enter the two numbers and click "Add" button, the Visual Studio Debugger pops up confirming the connection and a call being placed. I felt great when this happened, I had just called a method in a different application domain ( and in a different language ) from an entirely different application.
Hope this simple example helps you. Any comments are welcome.
Fun !!
Monday, August 24, 2009
Subscribe to:
Comments (Atom)