Main Content

Create a C# Client Using MWHttpClient

This example shows how to write a C# application to call a MATLAB® function deployed to MATLAB Production Server™. The C# application uses the MATLAB Production Server .NET client library.

A .NET application programmer typically performs this task. The tutorial assumes that you have Microsoft® Visual Studio® and .NET installed on your computer.

Create Microsoft Visual Studio Project

  1. Open Microsoft Visual Studio.

  2. Click File > New > Project.

  3. In the New Project dialog box, select the template you want to use. For example, if you want to create a C# console application in Visual Studio 2017, select Visual C# > Windows Desktop in the left navigation pane, then select the Console App (.Net Framework).

  4. Type the name of the project in the Name field (for example, Magic).

  5. Click OK. Your Magic source shell is created, typically named Program.cs, by default.

Create Reference to Client Runtime Library

Create a reference in your Magic project to the MATLAB Production Server client runtime library. In Microsoft Visual Studio, perform the following steps:

  1. In the Solution Explorer pane within Microsoft Visual Studio (usually on the right side), right-click your Magic project, select Add > Browse.

  2. Browse to the MATLAB Production Server .NET client runtime library location.

    In an on-premises MATLAB Production Server installation, the library is located in $MPS_INSTALL\client\dotnet, where $MPS_INSTALL is the location in which MATLAB Production Server is installed. Select the MathWorks.MATLAB.ProductionServer.Client.dll file.

    The client library is also available for download at https://www.mathworks.com/products/matlab-production-server/client-libraries.html.

  3. Click OK. Your Microsoft Visual Studio project now references the MathWorks.MATLAB.ProductionServer.Client.dll.

Design .NET Interface in C#

In this example, you invoke a MATLAB function mymagic.m hosted by the server from a .NET client through a .NET interface. The mymagic function uses the magic (MATLAB) function to create a magic square. The function mymagic takes a single int input and returns a magic square as a 2-D double array.

function m = mymagic(in)
    m = magic(in);

Design a C# interface Magic to match the MATLAB function mymagic.m. In your C# client program, use this interface to specify the type of the proxy object reference in the CreateProxy method.

  • The .NET interface has the same number of inputs and outputs as the MATLAB function.

  • Since you are deploying one MATLAB function on the server, you define one corresponding .NET method in your C# code.

  • Both the MATLAB function and the .NET interface process the same data types—input type int and output type 2-D double.

 public interface Magic 
        {
          double[,] mymagic(int in1);
        }

For information on creating and deploying an archive to the server, see Create a Deployable Archive for MATLAB Production Server and Share the Deployable Archive.

Write, Build, and Run .NET Application

  1. Open the Microsoft Visual Studio project Magic that you created earlier.

  2. In the Program.cs tab, paste in the code below.

    using System;
    using System.Net;
    using MathWorks.MATLAB.ProductionServer.Client;
    
    namespace Magic
    {
        public class MagicClass
        {
    
            public interface Magic
            {
                double[,] mymagic(int in1);
            }
    
            public static void Main(string[] args)
            {
                MWClient client = new MWHttpClient();
                try
                {
                    Magic me = client.CreateProxy<Magic>
                              (new Uri("http://localhost:9910/mymagic_deployed"));
                    double[,] result1 = me.mymagic(4);
                    print(result1);
                }
                catch (MATLABException ex)
                {
                    Console.WriteLine("{0} MATLAB exception caught.", ex);
                    Console.WriteLine(ex.StackTrace);
                }
                catch (WebException ex)
                {
                    Console.WriteLine("{0} Web exception caught.", ex);
                    Console.WriteLine(ex.StackTrace);
                }
                finally
                {
                    client.Dispose();
                }
                Console.ReadLine();
            }
    
            public static void print(double[,] x)
            {
                int rank = x.Rank;
                int[] dims = new int[rank];
    
                for (int i = 0; i < rank; i++)
                {
                    dims[i] = x.GetLength(i);
                }
    
                for (int j = 0; j < dims[0]; j++)
                {
                    for (int k = 0; k < dims[1]; k++)
                    {
                        Console.Write(x[j, k]);
                        if (k < (dims[1] - 1))
                        {
                            Console.Write(",");
                        }
                    }
                    Console.WriteLine();
                }
            }
        }
    }

    The URL value ("http://localhost:9910/mymagic_deployed") used to create the proxy contains three parts.

    • the server address (localhost).

    • the port number (9910).

    • the archive name (mymagic_deployed).

  3. Build the application. Click Build > Build Solution.

  4. Run the application. Click Debug > Start Without Debugging. The program returns the following console output.

    16,2,3,13
    5,11,10,8
    9,7,6,12
    4,14,15,1 

Related Topics