Main Content

.NET Client Coding Best Practices

Static Proxy Interface Guidelines

When writing .NET interfaces to invoke MATLAB® code, remember these guidelines:

  • The method name exposed by the interface must match the name of the MATLAB function being deployed. The method must have the same number of inputs and outputs as the MATLAB function.

  • The method input and output types must be convertible to and from MATLAB.

  • The number of inputs and outputs must be compatible with those supported by MATLAB.

  • If you are working with MATLAB structures, remember that the field names are case sensitive and must match in both the MATLAB function and corresponding user-defined .NET type.

  • The name of the interface can be any valid .NET name.

  • Your code should support exception handling.

.NET Client Prerequisites

Complete these steps to prepare your MATLAB Production Server™ .NET development environment.

  1. Install Microsoft® Visual Studio®. For a list of supported software, including IDEs and Microsoft .NET Framework, see Supported and Compatible Compilers.

  2. Verify that your application is deployed to a running server instance.

Handling Exceptions

The following table lists errors and the corresponding method to use to declare exceptions.

Error MethodException
MATLAB errorsMATLABExceptionMathWorks.MATLAB.ProductionServer.Client. MWClient.MATLABException
Transport errors occurring during client-server communicationWebExceptionSystem.Net.WebException

Managing System Resources

The connections between a .NET client and the servers with which it interacts are managed by one or more instances of MWHttpClient. You can use a single instance to communicate with more than one server or you can create multiple instances to manage multiple servers. Proxy objects, created using an instance of MWHttpClient, communicate with the server until the Dispose method of that instance is invoked. Therefore, it is important to call the Dispose method only when the MWHttpClient instance is no longer needed, to reclaim system resources.

Call the Dispose method on unneeded client instances to free native resources, such as open connections created by an instance of MWHttpClient.

You call Dispose in either of two ways:

  • Call Dispose directly — Call the method directly on the object whose resources you want to free:

    client.Dispose();

  • The using keyword — Implicitly invoke Dispose on theMWHttpClient instance with the using keyword. By doing this, you don’t have to explicitly call the Dispose method—the .NET Framework handles cleanup for you.

    Following is a code snippet that demonstrates use of the using keyword:

     using (MWClient client = new MWHttpClient(new TestConfigDispose()))
     { 
            // Use client to create proxy instances and invoke 
            //   MATLAB functions.... 
     }          

Caution

Calling Dispose on instances of MWClient closes all open sockets bound to the instance.

Data Conversion for .NET and MATLAB Types

For information regarding supported MATLAB types for client and server marshaling, see Supported MATLAB Data Types for Client and Server Marshaling

Where to Find the API Documentation

The API doc for the .NET client is installed in $MPS_INSTALL/client.

Related Topics