Self-Hosting the Telerik Reporting REST Web API
ASP.NET Web API does not require IIS. You can self-host a Web API in your own host process. This tutorial shows how to host a Telerik Reporting REST Web API inside a console application. For more information on the hosting options, see Hosting ASP.NET Web API.
Creating a Self-Hosted HTTP Service
-
On an elevated console (Run as administrator), execute the following command, for example, to allow the running user to listen on port 8080:
netsh http add urlacl url=http://+:8080/ user=DOMAIN\user -
In Visual Studio, create a Console Application project.
-
Install the Microsoft.AspNet.WebApi.SelfHost 4.0.30506 NuGet package.
The Reporting REST Web API Service is built against Web API 1. In case you have to use a newer version of Microsoft.AspNet.WebApi.SelfHost (for example, Web API 2), you have to redirect the
System.Web.HttpandSystem.Net.Http.Formattingreferences to their newer version.Visual Studio NuGet Package Manager can add the required binding redirects automatically if you update NuGet packages through it.
Alternatively, you can add the following
bindingRedirectsto yourApp.configand replace5.1.0.0in the sample code below with the exact version:XML<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.1.0.0"/> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="5.1.0.0"/> </dependentAssembly> </assemblyBinding> </runtime> </configuration> -
Make sure that the project has the following assembly references:
- System.Web
- Newtonsoft.Json.dll
- System.Web.Http.dll
- System.Web.Http.SelfHost.dll
- System.Net.Http.dll
- System.Net.Http.Formatting.dll
-
Implement the reports controller as explained in the article How to implement the ReportsController in an application.
-
Implement the starting point of the application:
C#using System; using System.Linq; using System.Web.Http.SelfHost; using Telerik.Reporting.Services.WebApi; class Program { static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:8080"); // use appropriate address ReportsControllerConfiguration.RegisterRoutes(config); var server = new HttpSelfHostServer(config); server.OpenAsync().Wait(); Console.WriteLine("Server is opened"); Console.ReadKey(); } } -
Run the console application.
-
To verify whether the service works correctly, you can make a sample request for the available document formats using the following URL:
http://localhost:[portnumber]/api/reports/formatsIf the request succeeds, you should receive the document formats encoded in JSON. For more information, see: Get Available Document Formats.
-
When you are finished self-hosting, be sure to delete the reservation:
netsh http delete urlacl url=http://+:8080/