How to use basic ASP.NET WebAPI in a C # UWP application?

I am trying to understand how ASP.NET Core WebAPI can be used in a UWP / UAP application. I thought it would be possible to use a WebAPI similar to using a WCF service, but haven’t found anything yet.

In addition, I tried installing Microsoft.AspNet.WebApi.Core, but was unsuccessful as it is incompatible with UAP (Version = V10.0).

Now I'm a little lost. Maybe someone can give me a hint how I can use WebApi in a UWP application.

+7
rest asp.net-core uwp wcf
source share
3 answers

This is the same as any API call. Just use the HttpClient class to call the endpoint and process the response, there is no difference in the expected behavior.

Imagine an ASP.NET Core Web API endpoint is defined as follows:

 public class StackoverflowController : Controller { // I wanted to exemplify async capabilities. // You'd use async/await for getting database values, etc. [ HttpGet, AllowAnonymous, Route("api/greeting") ] public Task<GreetingResult> Greeting() => Task.FromResult(new GreetingResult { Message = "Hello world!" }); } public class GreetingResult { public string Message { get; set; } } 

Assuming this was hosted on `localhost: 5000 ', you could do the following:

 public class Consumer { public async Task<string> GetGreetingAsync() { using (var client = new HttpClient()) { var response = await client.GetStringAsync("http://localhost:5000/api/greeting"); // The response object is a string that looks like this: // "{ message: 'Hello world!' }" } } } 

Alternatively, you can deserialize this as using a strongly typed Newtonsoft.Json object. I have an example of my UWP application that does just that here .

+6
source share

I will add additional information:

The best way to use Web Api in UWP is to use HttpClient, as mentioned earlier.

Here are a few examples that I think might be useful.

It’s good practice to create a MobileServiceClient class, where you can collect all the operations that you can perform at the Web Api level:

 public class MobileServiceClient { //access token if you use authentication: private string _accessToken; //address of your Web Api: private string _serviceAddress; //Constructor: public MobileServiceClient(string accessToken, string serviceAddress) { _accessToken = accessToken; _serviceAddress = serviceAddress; } //Now you can implement methods that you will invoke to perform selected operation related with Web Api: #region Methods //You can perform "Get" to retrieve object from the Web Api and then deserialize it (using Json .NET): public async Task<SampleClass> GetSampleClass() { SampleClass sampleClass= null; try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var data = await client.GetAsync(string.Concat(_serviceAddress, "routeName")); var jsonResponse = await data.Content.ReadAsStringAsync(); if (jsonResponse != null) sampleClass= JsonConvert.DeserializeObject<SampleClass>(jsonResponse); return sampleClass; } } catch (WebException exception) { throw new WebException("An error has occurred while calling GetSampleClass method: " + exception.Message); } } //You can perform "Get" to retrieve list of objects and then deserialize it: public async Task<List<SampleClass>> GetSampleClassObjects() { List<SampleClass> SampleClassObjectsList = null; try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var data = await client.GetAsync(string.Concat(_serviceAddress, "routeName")); var jsonResponse = await data.Content.ReadAsStringAsync(); if (jsonResponse != null) SampleClassObjectsList = JsonConvert.DeserializeObject<List<SampleClass>>(jsonResponse); return SampleClassObjectsList; } } catch (WebException exception) { throw new WebException("An error has occurred while calling GetSampleClassObjects method: " + exception.Message); } } //You can also "Post" some object: public async Task<bool> PostSomeObject(SampleClass sampleClassObject) { try { using (HttpClient client = new HttpClient()) { var sampleClassObjectJson = JsonConvert.SerializeObject(sampleClassObject); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken); var content = new StringContent(sampleClassObjectJson, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(string.Concat(_serviceAddress + "routeName"), content); if (response.StatusCode == HttpStatusCode.OK) return true; else throw new WebException("An error has occurred while calling PostSomeObject method: " + response.Content); } } catch (WebException exception) { throw new WebException("An error has occurred while calling PostFeedback method: " + exception.Message); } } #endregion } 

Please note that with this implementation, you can share this code in the future if, for example, you decide to support other platforms (for example, Xamarin Android or iOS). Hope this helps you.

+5
source share

David Pines answer is good enough if you want to do all things manually and / or you are not using swagger.

If you use Swagger (i.e. Swashbuckle Swagger 6.0 for the ASP.NET kernel) to describe your RESTful API and create documentation for it, you can use the swagger definition file (swagger.json) to create a Rest Rest. One such tool is AutoRest , created by the Azure team. It will also require Microsoft.Rest.ClientRuntime , which already supports DotNet.

I'm not sure that it works with ASP.NET Core RTM, but problems were fixed / closed, indicating that it supported previous versions and beta versions.

NuGet Package Links

+3
source share

All Articles