Self-Hosting the Telerik Reporting REST Service Web API in a Console Application
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
To create a self-hosted HTTP service, follow the steps below:
-
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 WebAPI Service is built against WebAPI 1. In case you have to use newer version of Microsoft.AspNet.WebApi.SelfHost (e.g., WebAPI 2) you have to redirect the
System.Web.HttpandSystem.Net.Http.Formattingto their newer version.Visual Studio NuGet Package Manager can add the required binding redirects automatically, if you update NuGet packages through it.
Alternatively, you add the following
bindingRedirectsto yourweb.configand replace5.1.0.0in the sample code below with the exact version:XML<?xml version="1.0" encoding="utf-8" ?> <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 app
-
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/