How to: Create a JSON web service with Oxygene

This Howto uses Visual Studio 2017 and Server 2016 Internet Information Services for the examples.

With Oxygene it is very easy to create a JSON web service.

First follow the steps to create a WDSL Webservice in this post.
Then import the JSON library: Visual Studio Menu - Tools - NuGet Package Manager - NetGet Package Console. In the console, type Install-Package Newtonsoft.Json and press Enter.




The Newtonsoft.Jason Library will be referenced from your project now.

Next, make sure that all WDSL (XML) methods return a class instead of a simple datatype.
Then remove the return value from the webmethod and write the Json to the HTTP context:

The old WDSL (XML) implementation was:
And this returned:



The new JSON implementation is:
And this returns:



The last step is to allow the standard Get method, so you can call the Web service with a Query String instead of a HTTP Post. In the Wen.Config add the following code below the <system.web> tag:



And then you can call the service in the form of:
http://yoursite/yourWebService.asmx/MethodName?param=value&param2=value...

In this case, the method has no parameters, making the call
http://yoursite/Service1.asmx/HelloWorld


No comments:

Post a Comment