Access a Remotable .NET Assembly Using MWArray

Why Use MWArray API?

After you create the remotable component, you can set up a console server and client using the MWArray API. For more information on choosing the right API for your access needs, see Select How Access an Assembly.

Some reasons you might use the MWArray API instead of the native .NET API are:

  • You are working with data structure arrays, which the native .NET API does not support.

  • You or your users work extensively with many MATLAB® data types.

  • You or your users are familiar and comfortable using the MWArray API.

For information on accessing your component using the native .NET API, see Access a Remotable .NET Assembly Using the Native .NET API: Magic Square.

Coding and Building the Hosting Server Application and Configuration File

The server application hosts the remote component built in Create a Remotable .NET Assembly. You can also perform these steps using the native .NET API as discussed in Access a Remotable .NET Assembly Using the Native .NET API: Magic Square.

Build the server using the Microsoft® Visual Studio® project file MagicSquareServer\MagicSquareMWServer.csproj:

  1. Change the references for the generated component assembly to MagicSquareComp\for_redistribution_files_only\MagicSquareComp.dll.

  2. Select the appropriate build platform.

  3. Select Debug or Release mode.

  4. Build the MagicSquareMWServer project.

  5. Supply the configuration file for the MagicSquareMWServer.

MagicSquareServer Code

Use the C# code for the server located in the file MagicSquareServer\MagicSquareServer.cs:

   using System;
   using System.Runtime.Remoting;

   namespace MagicSquareServer
     {
       class MagicSquareServer
         {
           static void Main(string[] args)
             {
               RemotingConfiguration.Configure
                 (@"..\..\..\..\MagicSquareServer.exe.config");

               Console.WriteLine("Magic Square Server started...");

               Console.ReadLine();
             }
         }
     }
This code does the following processing:

  • Reads the associated configuration file to determine

    • The name of the component that it will host

    • The remoting protocol and message formatting to use

    • The lease time for the remote component

  • Signals that the server is active and waits for a carriage return to be entered before terminating.

MagicSquareServer Configuration File

The configuration file for the MagicSquareServer is in the file MagicSquareServer\MagicSquareServer.exe.config. The entire configuration file, written in XML, follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="SingleCall" 
                   type="MagicSquareComp.MagicSquareClass, MagicSquareComp" 
                   objectUri="MagicSquareClass.remote" />
      </service>
      <lifetime leaseTime= "5M" renewOnCallTime="2M"
                leaseManagerPollTime="10S" />
      <channels>
        <channel ref="tcp" port="1234">    
          <serverProviders>            
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
        </channel>                
      </channels>     
    </application>
    <debug loadTypes="true"/>
  </system.runtime.remoting>
</configuration>

This code specifies:

  • The mode in which the remote component will be accessed—in this case, single call mode

  • The name of the remote component, the component assembly, and the object URI (uniform resource identifier) used to access the remote component

  • The lease time for the remote component

  • The remoting protocol (TCP/IP) and port number

  • The message formatter (binary) and the permissions for the communication channel (full trust)

  • The server debugging option

Coding and Building the Client Application and Configuration File

The client application, running in a separate process, accesses the remote component running in the server application you built previously. (See Coding and Building the Hosting Server Application and Configuration File.

Next build the remote client using the Microsoft Visual Studio project file MagicSquareClient\MagicSquareMWClient.csproj. This file references both the shared data conversion assembly matlabroot\toolbox\dotnetbuilder\bin\win32\v2.0\ MWArray.dll and the generated component interface assembly MagicSquareComp\for_redistribution_files_only\IMagicSquareComp.

To create the remote client using Microsoft Visual Studio:

  1. Select the appropriate build platform.

  2. Select Debug or Release mode.

  3. Build the MagicSquareMWClient project.

  4. Supply the configuration file for the MagicSquareMWServer.

MagicSquareClient Code

Use the C# code for the client located in the file MagicSquareClient\MagicSquareClient.cs. The client code is shown here:

using System;
using System.Configuration;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

using System.Collections;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Channels.Tcp;

using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays;

using IMagicSquareComp;

namespace MagicSquareClient
{
  class MagicSquareClient
    {
      static void Main(string[] args)
        {
          try
            {
              RemotingConfiguration.Configure
                                      (@"MagicSquareClient.exe.config");
                
              String urlServer=
                   ConfigurationSettings.AppSettings["MagicSquareServer"];

              IMagicSquareClass magicSquareComp=
                (IMagicSquareClass)Activator.GetObject
                                     (typeof(IMagicSquareClass),
                                      urlServer);
 
              // Get user specified command line arguments or set default
              double arraySize= (0 != args.Length) 
                                ? Double.Parse(args[0]) : 4;

              // Compute the magic square and print the result
              MWNumericArray magicSquare=
                (MWNumericArray)magicSquareComp.makesquare
                                  (arraySize);

              Console.WriteLine("Magic square of order {0}\n\n{1}",
                                arraySize, magicSquare);
            }

          catch (Exception exception)
            {
              Console.WriteLine(exception.Message);
            }

          Console.ReadLine();
        }
    }
}

This code does the following:

  • The client reads the associated configuration file to get the name and location of the remoteable component.

  • The client instantiates the remoteable object using the static Activator.GetObject method

  • From this point, the remoting client calls methods on the remoteable component exactly as it would call a local component method.

MagicSquareClient Configuration File

The configuration file for the magic square client is in the file MagicSquareClient\MagicSquareClient.exe.config. The configuration file, written in XML, is shown here:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="MagicSquareServer"
     value="tcp://localhost:1234/MagicSquareClass.remote"/>    
  </appSettings>
  <system.runtime.remoting>
    <application>
      <channels>
        <channel name="MagicSquareChannel" ref="tcp" port="0">        
          <clientProviders>            
            <formatter ref="binary" />
          </clientProviders>
          <serverProviders>            
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>            
        </channel>
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>

This code specifies:

  • The name of the remote component server and the remote component URI (uniform resource identifier)

  • The remoting protocol (TCP/IP) and port number

  • The message formatter (binary) and the permissions for the communication channel (full trust)

Starting the Server Application

Starting the server by doing the following:

  1. Open a DOS or UNIX® command window and cd to MagicSquareServer\bin\x86\v2.0\Debug.

  2. Run MagicSquareServer.exe. You will see the message:

    Magic Square Server started...

Starting the Client Application

Start the client by doing the following:

  1. Open a DOS or UNIX command window and cd to MagicSquareClient\bin\x86\v2.0\Debug.

  2. Run MagicSquareClient.exe. After the MATLAB Runtime initializes, you should see the following output:

    Magic square of order 4
    
    162313
    511108
    97612
    414151
Was this topic helpful?