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.
Open Microsoft Visual Studio.
Click File > New > Project.
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).
Type the name of the project in the Name field (for example,
Magic
).
Click OK. Your Magic
source shell is created, typically named Program.cs
, by default.
Create a reference in your Magic
project to the MATLAB
Production Server client runtime library. In Microsoft
Visual Studio, perform the following steps:
In the Solution Explorer pane within Microsoft
Visual Studio (usually on the right side),
right-click your Magic
project, select Add >
Browse.
Browse to the MATLAB Production Server .NET client runtime library location.
The library is located in
.
Select the matlabroot
\toolbox\compiler_sdk\mps_client\dotnetMathWorks.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
.
Click OK. Your Microsoft
Visual Studio project now references the
MathWorks.MATLAB.ProductionServer.Client.dll
.
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
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); }
Open the Microsoft
Visual Studio project Magic
that you created earlier.
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
).
Build the application. Click Build > Build Solution.
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