Reputation: 10139
I've decided to use Web API (as a middle tier) for an app I'm developing, but can't seem to figure out how to "tie it in" to the front end (front end being an ASP.NET MVC4 project). Normally, I would just right click Services on the front end, choose "Add Service Reference", and put the URL for my service in. But with Web API, I can't do that. What are some options for creating a client proxy class off of my Web API to be used on my front end, and why doesn't a Web API support being added as a reference the same way a WCF or ASMX is added?
Upvotes: 31
Views: 24065
Reputation: 10844
To consume or use a REST service you just need the api and method to call
And is a good practice to have the API_BASE into single place (either a variable or config file), in case you need to test the same published into another server
public class ProductosDAL
{
public static string API_BASE = "http://localhost:54234/api/" ;
private static string apiPath(string method)
{
return string.Format("{0}{1}", API_BASE, method);
}
public static async Task<List<ProductoEntity>> GetProductosAsync()
{
List<ProductoEntity> lstProd = new List<ProductoEntity>();
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync(apiPath("productos")))
{
string apiResponse = await response.Content.ReadAsStringAsync();
lstProd = JsonConvert.DeserializeObject<List<ProductoEntity>>(apiResponse);
}
}
return lstProd;
}
}
My example is a little improvement over this great guide https://www.yogihosting.com/aspnet-core-consume-api/
Upvotes: 0
Reputation: 6367
There's a generic WebAPI client to be found here:
https://github.com/CamSoper/CamTheGeek
It's not a proxy, as requested, but it does fill the gap.
Here's the source code:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
namespace CamTheGeek
{
public class GenericWebApiClient<T> : IDisposable where T : class
{
HttpClient client = new HttpClient();
Uri ServiceBaseUri;
public GenericWebApiClient(Uri ServiceUri)
{
if(ServiceUri == null)
{
throw new UriFormatException("A valid URI is required.");
}
ServiceBaseUri = ServiceUri;
}
public List<T> GetAll()
{
var response = client.GetAsync(ServiceBaseUri).Result;
if(response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<List<T>>().Result as List<T>;
}
else if (response.StatusCode != HttpStatusCode.NotFound)
{
throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString());
}
return null;
}
public T GetById<I>(I Id)
{
if (Id == null)
return default(T);
var response = client.GetAsync(ServiceBaseUri.AddSegment(Id.ToString())).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<T>().Result as T;
}
else if (response.StatusCode != HttpStatusCode.NotFound)
{
throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString());
}
return null;
}
public void Edit<I>(T t, I Id)
{
var response = client.PutAsJsonAsync(ServiceBaseUri.AddSegment(Id.ToString()), t).Result;
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException("Edit failed with " + response.StatusCode.ToString());
}
public void Delete<I>(I Id)
{
var response = client.DeleteAsync(ServiceBaseUri.AddSegment(Id.ToString())).Result;
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException("Delete failed with " + response.StatusCode.ToString());
}
public void Create(T t)
{
var response = client.PostAsJsonAsync(ServiceBaseUri, t).Result;
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException("Create failed with " + response.StatusCode.ToString());
}
public void Dispose(bool disposing)
{
if (disposing)
{
client = null;
ServiceBaseUri = null;
}
}
public void Dispose()
{
this.Dispose(false);
GC.SuppressFinalize(this);
}
~GenericWebApiClient()
{
this.Dispose(false);
}
}
static class UriExtensions
{
public static Uri AddSegment(this Uri OriginalUri, string Segment)
{
UriBuilder ub = new UriBuilder(OriginalUri);
ub.Path = ub.Path + ((ub.Path.EndsWith("/")) ? "" : "/") + Segment;
return ub.Uri;
}
}
}
Upvotes: 2
Reputation: 1110
Do you mean a Rest Web Service? With Rest, there is no service definition page, like with WCF or ASMX. Usually people want to use a Rest API with JSON.. however.. if you are just looking for a JSON output, and you want your clients to quickly be able to connect to your service, you should consider OData. It's really easy to create and it makes your data layer accessible for a large number of client languages. They have the OData client library ported for a ton of languages. Submitted as an answer, as requested. : )
Upvotes: 21
Reputation: 19311
why doesn't a Web API support being added as a reference the same way a WCF or ASMX is added
WCF or ASMX-based web services are SOAP-based and there typically is an associated WSDL. WSDL allows tooling to be built around to generate proxy classes and all that but ASP.NET Web API is meant to build REST (or HTTP based) services and there is no meta data in the form of WSDL or anything similar and hence adding service reference through VS is not applicable for ASP.NET Web API. WADL (Web Application Description Language) is supposed to be the WSDL for REST but that spec went no where.
Upvotes: 15