Access Remotable .NET Assembly Using 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 Compare MWArray and Native .NET API for Remotable Assemblies.
Coding and Building the Hosting Server Application and Configuration File
The server application hosts the remote component built in Create Remotable .NET Assembly.
Build the server using the Microsoft®
Visual Studio® project file
MagicSquareServer\MagicSquareMWServer.csproj
:
Change the references for the generated component assembly to
MagicSquareComp\for_redistribution_files_only\MagicSquareComp.dll
.Select the appropriate build platform.
Select Debug or Release mode.
Build the
MagicSquareMWServer
project.Supply the configuration file for the
MagicSquareMWServer
.
MagicSquareServer Code
Use the C# code for the server located in the file
MagicSquareServer\MagicSquareServer.cs
:
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 numberThe message formatter (
binary
) and the permissions for the communication channel (full
trust)The server debugging option
Build 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
and the generated component interface assembly
matlabroot
\toolbox\dotnetbuilder\bin\win64\v4.0\
MWArray.dllMagicSquareComp\for_redistribution_files_only\IMagicSquareComp
.
To create the remote client using Microsoft Visual Studio:
Select the appropriate build platform.
Select Debug or Release mode.
Build the
MagicSquareMWClient
project.Supply the configuration file for the
MagicSquareMWServer
.
MagicSquareClient Code
Use the C# code for the client located in the file
MagicSquareClient\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
methodFrom 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 numberThe message formatter (
binary
) and the permissions for the communication channel (full
trust)
Start Server Application
Starting the server by doing the following:
Open a DOS or UNIX® command window and navigate to
MagicSquareServer\bin\x86\v4.0\Debug
.Run
MagicSquareServer.exe
. You will see the message:Magic Square Server started...
Start Client Application
Start the client by doing the following:
Open a DOS or UNIX command window and navigate to
MagicSquareClient\bin\x86\v4.0\Debug
.Run
MagicSquareClient.exe
. After MATLAB® Runtime initializes, you should see the following output:Magic square of order 4 162313 511108 97612 414151