首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我们不能像使用WCF或ASMX那样在Visual中添加一个Web作为“服务引用”呢?

为什么我们不能像使用WCF或ASMX那样在Visual中添加一个Web作为“服务引用”呢?
EN

Stack Overflow用户
提问于 2013-09-11 14:46:44
回答 4查看 22.7K关注 0票数 30

我已经决定在我正在开发的应用程序中使用Web (作为中间层),但似乎不知道如何将它“绑定”到前端(前端是ASP.NET MVC4项目)。通常,我只需在前端右键单击Services,选择“”,并将我的服务的URL放入其中。但是使用Web,我无法做到这一点。在我的Web API中创建一个客户机代理类,在我的前端使用哪些选项,为什么不以添加WCF或ASMX的方式将一个Web API支持作为引用添加?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-11 15:11:59

你是说Rest Web服务吗?对于Rest,没有服务定义页面,比如WCF或ASMX。通常,人们希望在JSON中使用Rest。但是..。如果您只是在寻找JSON输出,并且希望您的客户能够快速地连接到您的服务,那么应该考虑OData。它非常容易创建,它使您的数据层可以被大量的客户端语言访问。他们为大量的语言移植了OData客户端库。应要求作为答复提交。:)

票数 20
EN

Stack Overflow用户

发布于 2013-09-11 15:06:35

为什么不以添加WCF或ASMX的方式将Web API支持添加为引用

WCF或基于ASMX的web服务是基于SOAP的,通常有一个相关的WSDL。WSDL允许围绕生成代理类和所有这些构建工具,但是ASP.NET Web的目的是构建REST (或基于HTTP )服务,并且没有任何类似的元数据,因此通过VS添加服务引用不适用于ASP.NET Web。WADL ()本应是REST的WSDL,但该规范没有实现。

票数 14
EN

Stack Overflow用户

发布于 2017-10-17 22:57:21

在这里可以找到一个通用的WebAPI客户机:

https://github.com/CamSoper/CamTheGeek

它不是所要求的代理,但它确实填补了空白。

下面是源代码:

代码语言:javascript
复制
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;
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18744407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档