How can I call Web Services from MATLAB 7.14 (R2012a) using .NET Assemblies generated using svcutil?

4 views (last 30 days)
In Microsoft Visual Studio I can add "Service References" to my .NET Applications. This will automatically generate some classes which allow me to easily call the Web Services from my application. Given that MATLAB can call .NET Assemblies, is it possible to use a similar approach to call a Web Service from MATLAB?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Oct 2013
It is indeed possible to call (.NET) Web Services in MATLAB by using automatically generated .NET Client Classes.
===========================================
Example Setup
===========================================
In this example the Web Service which we use is the example/template service which you get when creating a new "WCF Service Application" in Microsoft Visual Studio 2010 Professional. So this would be a service which exports the following method:
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
Further, if you would call this service from a .NET Client Application you would add the Service Reference through the following dialog in Visual Studio:
After you have added the reference the following code should be able to call the service:
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Console.WriteLine(client.GetData(42));
Console.ReadLine();
}
===========================================
MATLAB Approach
===========================================
To call the service described above in MATLAB you will first need to generate the client class/assembly. To do so you will need svcutil and csc (C# compiler). To make sure these tools are available on your PATH start a "Visual Studio Command Prompt" or "Windows SDK 7.1 Command Prompt" from your start menu (under "Visual Studio XXXX" or "Windows SDK" group). Hint: If you start MATLAB from this prompt you can eventually write a MATLAB script to automate the commands below, when doing so do not forget the bang (!) operator to run externals commands from within MATLAB.
=== SVCUTIL ===
To generate the same client class as through the "Add Service Reference" dialog shown above one would call svcutil as follows:
svcutil /n:*,ServiceReference1 <http://localhost:54661/Service1.svc>
Where /n:*,ServiceReference1 is the namespace in which the client will be generated and http://localhost:54661/Service1.svc is the endpoint of your service.
=== CSC ===
The generated class can then be compiled into an Assembly named Service1Client.dll using:
csc /t:library /out:Service1Client.dll Service1.cs
Where csc it the C# compiler, /t:library specifies that we want to create a library assembly, /out:Service1Client.dll is the output DLL filename which we choose and Service1.cs was the class generated by svcutil.
=== Importing the Assembly into MATLAB ===
Once you have obtained this Service1Client.dll, it can be imported into MATLAB using the NET.addAssembly function:
NET.addAssembly(fullfile(pwd,'Service1Client.dll'));
Note that NET.addAssembly always works with full paths and the example above we assumed that the DLL is in the current directory.
Further we will also be needing classes from System.ServiceModel, so we will also need to import this:
NET.addAssembly('System.ServiceModel');
=== Instantiating the client in MATLAB ===
Now you may have noticed that svcutil also generated a CONFIG-file. This CONFIG-file is used if you instantiate the client through the ServiceReference1.Service1Client() constructor without any input arguments (as in the C# example above). The CONFIG-file is an application wide configuration file though, which makes it cumbersome to use in MATLAB. Therefore in MATLAB it is easier and more flexible to use the 2 arguments constructor:
client = ServiceReference1.Service1Client(...
System.ServiceModel.BasicHttpBinding(),...
System.ServiceModel.EndpointAddress('<http://localhost:54661/Service1.svc'>));
Where in this example we use a BasicHttpBinding, you can use a different Binding if your service uses a different Binding type.
=== Calling the methods ===
Once the client has been instantiated you should be able to call the methods in a way very similar to C#:
response = client.GetData(42);
disp(response);

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2012a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!