Create a C# Client
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
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 namedProgram.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:
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.
In an on-premises MATLAB Production Server installation, the library is located in
, where$MPS_INSTALL
\client\dotnet
is the location in which MATLAB Production Server is installed. Select the$MPS_INSTALL
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
.Click OK. Your Microsoft Visual Studio project now references the
MathWorks.MATLAB.ProductionServer.Client.dll
.
Deploy MATLAB Function to Server
Write a MATLAB function mymagic
that uses the magic
(MATLAB) function to create a magic square, package mymagic
into a deployable archive called mymagic_deployed
, then deploy it to a
server. The function mymagic
takes a single int
input
and returns a magic square as a 2-D double
array. The example assumes
that the server instance is running at http://localhost:9910
.
function m = mymagic(in)
m = magic(in);
For information on creating and deploying an archive to the server, see Create Deployable Archive for MATLAB Production Server and Deploy Archive to MATLAB Production Server.
Design .NET Interface in C#
Invoke the deployed MATLAB function mymagic
from a .NET client through a .NET
interface. Design a C# interface Magic
to match the MATLAB function mymagic
.
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-Ddouble
.In your C# client program, use the interface
Magic
to specify the type of the proxy object reference in theCreateProxy
method. TheCreateProxy
method requires the URL to the deployable archive that contains themymagic
function (http://localhost:9910/mymagic_deployed
) as an input argument.
public interface Magic { double[,] mymagic(int in1); }
Write, Build, and Run .NET Application
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