Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a WebAPI from Windows Service

I have an application written in Windows Service and this app need to make a call to a WebAPI written in Asp.Net MVC 4 WebAPi. this method in WebAPI return a DTO with primitive type, something like:

class ImportResultDTO {
   public bool Success { get; set; }
   public string[] Messages { get; set; }
}

and in my webapi

public ImportResultDTO Get(int clientId) {
   // process.. and create the dto result.
   return dto;
}

My question is, how can I call the webApi from the Windows Service? I have my URL and value of parameter, but I don't know how to call and how to deserialize the xml result to the DTO.

Thank you

like image 295
Felipe Oriani Avatar asked Oct 17 '12 20:10

Felipe Oriani


People also ask

Can we call API from windows service?

In this article, we are going to learn how to call a web API from a windows service at a scheduled time. We will create a sample ASP.NET Web API project and publish the project to IIS Manager. Once our API is completed, we will create a windows service that calls our Web API at a scheduled time.

Can we host Web API in windows service?

You can host a Web API as separate process than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework. You need to do following steps in order to self-host a web API.

How do you call a windows service method into a web application?

If you want to call a windows service method on the server side of your web application then take a look at the WCF or RestSharp and Nancy. Shortly, you need to create a RESTfull service in the windows service application that will be using a http://localhost/myservice/transfer address to expose the Transfer method.


2 Answers

You could use System.Net.Http.HttpClient. You will obviously need to edit the fake base address and request URI in the example below but this also shows a basic way to check the response status as well.

// Create an HttpClient instance
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:8888/");

// Usage
HttpResponseMessage response = client.GetAsync("api/importresults/1").Result;
if (response.IsSuccessStatusCode)
{
    var dto = response.Content.ReadAsAsync<ImportResultDTO>().Result;
}
else
{
    Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
like image 135
blins Avatar answered Sep 21 '22 04:09

blins


You can install this NuGet package Microsoft ASP.NET Web API Client Libraries to your Windows Service project.

Here is a simple code snippet demonstrating how to use HttpClient:

        var client = new HttpClient();
        var response = client.GetAsync(uriOfYourService).Result;
        var content = response.Content.ReadAsAsync<ImportResultDTO>().Result;

(I'm calling .Result() here for the sake of simplicity...)

For more sample of HttpClient, please check this out: List of ASP.NET Web API and HttpClient Samples.

like image 27
Maggie Ying Avatar answered Sep 23 '22 04:09

Maggie Ying