This site uses cookies to bring you the best experience. Find out more
Skip to main content

blog

Creating a RESTful Web API communicating with LabVIEW

In a recent blog post we described how we had used a RESTful API to transfer data between a cRIO and a SQL Server database. In this post we go into a bit more detail about how we created the API on the server side.

For all of our non-LabVIEW programming requirements we use Visual Studio 2017, C# and MVC. Our data modelling and access is based on Linq and the Entity Framework. From experience we know that there are many different ways of approaching a particular programming task. Invariably one of those ways is much more effective than all the others. It is always very satisfying when you discover a method where a lot of the work is done for you and the resultant solution is very flexible. We believe our solution for this API is such a case.

In our C# solution for AMRC, which includes a great deal of functionality to access documents, manage the workflow of experiments and report on results, we created a new project in Visual Studio by selecting ASP.NET Web Application with an Empty Template. The important part here was to select the option to add folders and core references for Web API. This provided the framework for our Web API project.

We then added a controller by selecting the ‘Web API 2 Controller with actions, using Entity Framework’ version and selecting the ‘Use async controller actions’. This enabled us to set up asynchronous methods to both get data from the database and save data to it. The value of using Entity Framework is that we could send and receive instances of classes in our database without having to manage the transfer.

For example a simple GET method to retrieve an instance of the parameters table can be called using a command such as http://localhost:50796/api/CRIO/GetParameters?ID=1 and it will return all the values from the appropriate record.

An additional significant advantage of using the Entity Framework is that fields can be added or removed from tables and there is no need to change the API methods.

Similar arrangements can be made to pass data for a specific class to the API to be saved to the database without the need to specify which fields are being saved.

One tool which we found particularly useful in developing the API was Postman. This enables you to test the various resources in the API by emulating a client application.

If you have a data or connectivity challenge, get in touch.

Back to Blog listings

ARGENTA IS A LEADING PROVIDER OF PRODUCT RELIABILITY AND PROCESS IMPROVEMENT SOLUTIONS.

Call 0121 514 2290 to discuss your requirements