Why can't we add a Web API as a “service refer

2020-02-08 10:55发布

问题:

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?

回答1:

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. : )



回答2:

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.



回答3:

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;
        }
    }
}