Accessibility

Table of Contents

Invoking .NET objects using the Flex RemoteObject API

Developing a .NET class

In this section, you'll learn how to compile a class as a .NET class library (assembly). The assembly is then deployed into a virtual directory, so the class is exposed as a remoting service to Flex clients.

The following code shows a simple service class with one method, the getTickerInfo method. The getTickerInfo method accepts a string argument ticker symbol, creates an object representing the ticker, populates it with data, and returns back to the caller. There are two versions of the class, one is written in C# and the other is in VB.NET.

Example in C#:

using System;

namespace GettingStarted.Examples
{
  public class StockQuoteService
  {
    public TickerInfo getTickerInfo( String symbol )
    {
      // Create a random number generator to make the example more dynamic
      Random rand = new Random();
      
      // Create an instance of the TickerInfo class
      TickerInfo ticker = new TickerInfo();
      
      // Populate object fields with data. The example uses 
      // public fields, but the public properties can be used 
      // as well.
      ticker.symbolName = symbol;
      ticker.lastPrice = rand.NextDouble() * 100;
      ticker.currentBid = ticker.lastPrice - 1;
      ticker.currentAsk = ticker.lastPrice + 1;
      ticker.lastPriceTimeStamp = DateTime.Now;
      
      // return the object back to the client
      return ticker;
    }
  }
         
  public class TickerInfo
  {
    public string symbolName;
    public double lastPrice;
    public DateTime lastPriceTimeStamp;
    public double currentBid;
    public double currentAsk;
  }
}

Example in VB.NET:

Imports System
 
Namespace GettingStarted.Examples
  Public Class StockQuoteService
    Public Function getTickerInfo(ByVal symbol As String) As TickerInfo
      ' Create a random number generator to make the example more dynamic
      Dim rand As Random =  New Random() 
 
      ' Create an instance of the TickerInfo class
      Dim ticker As TickerInfo =  New TickerInfo() 
 
      ' Populate object fields with data. The example uses 
      ' public fields, but the public properties can be used 
      ' as well.
      ticker.symbolName = symbol
      ticker.lastPrice = rand.NextDouble() * 100
      ticker.currentBid = ticker.lastPrice - 1
      ticker.currentAsk = ticker.lastPrice + 1
      ticker.lastPriceTimeStamp = DateTime.Now
 
      ' return the object back to the client
      Return ticker
    End Function
  End Class
 
  Public Class TickerInfo
    Public symbolName As String
    Public lastPrice As Double
    Public lastPriceTimeStamp As DateTime
    Public currentBid As Double
    Public currentAsk As Double
  End Class
End Namespace
          

Next, you'll create a Visual Studio project and compile the code shown above. You can create a brand new project or use the one available in the sample files archive in the Requirements section at the beginning of this article. If you decide to create a brand new Visual Studio project, make sure to select a target type of "Class library." Choose one of the options below:

To creating a new Visual Studio project:

  1. Create a Visual Studio project. Make sure to use the "Class Library" template for the language of your choice (C# or VB.NET).
  2. Visual Studio generates the default class file for the project. Replace the contents of the class file with the code shown above.
  3. If your choice is Visual Basic, open the project properties and delete the default value in the Root Namespace field.

To install a pre-created Visual Studio project:

  1. Download the project archive file from the Requirements section at the beginning of this article. Expand the archive into a directory.
  2. Open Visual Studio and select File > Open > Project/Solution, or simply press Control + Shift + O.
  3. Navigate to the directory containing the project files and select FlexService.sln.
  4. Visual Studio will load and open the project. Navigate to the source code file in the project.

Build the project to create a compiled assembly. In the next section, you'll deploy the assembly into a virtual directory so that the StockQuoteService class is exposed as a remoting service.

Deploying the .NET assembly

Before deploying the assembly, download and install WebORB for .NET v. 3.1 or later from themidnightcoders.com. The WebORB installer creates a virtual directory in the default IIS website. You will use the virtual directory to deploy the assembly created in the previous section, "Developing a .NET Class." The directory is typically located at /Inetpub/wwwroot/weborb30.

Deploying an assembly is as simple as copying the DLL into the /bin folder located in the root of the virtual directory. Locate the compiled assembly and copy the DLL file into the following location: /Inetpub/wwwroot/weborb30/bin. The assembly is automatically deployed and is ready for Flex client invocations.