Extending GeneXus Server REST API

Official Content

The GeneXus Server REST API allows you to query both the server itself and the Knowledge Bases hosted in it. Eventually, a client may need to make another type of query for which we have no services.

This document attempts to explain how to develop an extension for GeneXus Server to expose REST services with the queries required by the user.

Creation

To create the extension we have to create a DLL (Class Library) with the logic that the service will contain. In this case, the same library will include both the interface (contract) of the service and its implementation.

Contract

Add an interface to the project that we’ve just created, and add the methods that we want to expose in our service. For this interface to be presented, it must be "decorated" with the ServiceContract attribute of System.ServiceModel. Every method that you want to present in the service must have the OperationContract attribute (also of System.ServiceModel) and WebGet attribute of System.ServiceModel.Web. The WebGet attribute must have the format of the URL that will solve the UriTemplate property (for more info http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.uritemplate.aspx) .

Service

Once the contract has been defined, we have to create the service implementation. To do this, add a class to the library that will implement the interface (Contract) previously described. 

Exposure

For this service to be exposed by GXserver, you need to create an entry point (endpoint). To do so, create a file with .svc extension with the following line:

<%@ ServiceHost Language="C#" Service="MyLib.MyService" Factory="GeneXus.Server.Services.Factories.RESTfullFactory" %>

The upper line must be modified so that the Service attribute has the full name (including namespaces) of the class implementing the service. The Factory attribute must be left as is.

Deployment

Once our service has been implemented, its deployment consists of two parts. First, the .svc file previously created must be copied to the root execution directory of GXserver (<GXserver>\Vdir), and the DLL compiled with the service must go in the Bin directory of GXserver (<GXserver>\VDir\Bin)

After deployment, you can test it by making a call from a browser (as the GET method is defined) to the URL of the exposed method. This URL depends on the name of the .svc file and the string set in the UriTemplate property in the contract definition.

For example, the following call http://myserver/GenexusServer/TestService.svc/SayHello(World) can be made to a service whose endpoint file is called TestService.svc and the method contract was defined in this way:

[WebGet(UriTemplate = "SayHello({name})")]
[OperationContract]
string SayHello(string name);

A sample extension with two approaches can be downloaded from here.

Make sure to fix the Visual Studio project references pointing to the appropriate GeneXus SDK.

See Also

GeneXus Server Rest services

 



Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.