Main Content

Access Remotable .NET Assembly Using Native .NET API: Magic Square

Why Use the Native .NET API?

After the remotable component has been created, you can set up a server application and client using the native .NET API. For more information on choosing the right API for your access needs, see Compare MWArray and Native .NET API for Remotable Assemblies.

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

  • You want to pass arguments and return values using standard .NET types, and you or your users don't work extensively with data types specific to MATLAB®.

  • You want to access your component from a client machine without an installed version of MATLAB.

For information on accessing your component using the MWArray API, see Access Remotable .NET Assembly Using MWArray API.

Coding and Building the Hosting Server Application and Configuration File

The server application will host the remote component you built in Create Remotable .NET Assembly.

The client application, running in a separate process, will access the remote component hosted by the server application. Build the server with the Microsoft® Visual Studio® project file MagicSquareServer\MagicSquareServer.csproj:

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

  2. Select the appropriate build platform.

  3. Select Debug or Release mode.

  4. Build the MagicSquareServer project.

  5. Supply the configuration file for the MagicSquareServer.

MagicSquareServer Code

The C# code for the server is in the file MagicSquareServer\MagicSquareServer.cs. The MagicSquareServer.cs server code is shown here:

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:

  • Reads the associated configuration file to determine the name of the component that it will host, the remoting protocol and message formatting to use, as well as 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, is shown here:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="SingleCall"
                   type="MagicSquareCompNative.MagicSquareClass,  
                         MagicSquareCompNative" 
                   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 built in Coding and Building the Hosting Server Application and Configuration File. Build the remote client using the Microsoft Visual Studio project file MagicSquareClient\MagicSquareClient.csproj. To create the remote client using Microsoft Visual Studio:

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

  2. Change the reference for the generated interface assembly to MagicSquareComp\for_redistribution_files_only\IMagicSquareCompNative.dll.

  3. Select the appropriate build platform.

  4. Select Debug or Release mode.

  5. Build the MagicSquareClient project.

  6. Supply the configuration file for the MagicSquareServer.

MagicSquareClient Code

The C# code for the client is in the file MagicSquareClient\MagicSquareClient.cs.

 MagicSquareClient.cs

This code does the following:

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

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

  • From this point, the remoting client calls methods on the remotable 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

Start the server by doing the following:

  1. Open a DOS or UNIX® command window and navigate to MagicSquareServer\bin\x86\v4.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 navigate to MagicSquareClient\bin\x86\v4.0\Debug.

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

    Magic square of order 4
    
    Element(0,0)= 16
    Element(0,1)= 2
    Element(0,2)= 3
    Element(0,3)= 13
    Element(1,0)= 5
    Element(1,1)= 11
    Element(1,2)= 10
    Element(1,3)= 8
    Element(2,0)= 9
    Element(2,1)= 7
    Element(2,2)= 6
    Element(2,3)= 12
    Element(3,0)= 4
    Element(3,1)= 14
    Element(3,2)= 15
    Element(3,3)= 1