WCF,WebAPI,WCFREST和WebService的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WCF,WebAPI,WCFREST和WebService的区别相关的知识,希望对你有一定的参考价值。

参考技术A Web Service
It is based on SOAP and return data in XML form.
It support only HTTP protocol.
It is not open source but can be consumed by any client that understands xml.
It can be hosted only on IIS.
WCF
It is also based on SOAP and return data in XML form.
It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
The main issue with WCF is, its tedious and extensive configuration.
It is not open source but can be consumed by any client that understands xml.
It can be hosted with in the applicaion or on IIS or using window service.
WCF Rest
To use WCF as WCF Rest service you have to enable webHttpBindings.
It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
It support XML, JSON and ATOM data format.
Web API
This is the new framework for building HTTP services with easy and simple way.
Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
It can be hosted with in the application or on IIS.
It is light weight architecture and good for devices which have limited bandwidth like smart phones.
Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
To whom choose between WCF or WEB API
Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.本回答被提问者和网友采纳

将 WCF 转换为 WEB API:在 WEB API 中使用包装器重用 WCF 服务是个好主意吗?

【中文标题】将 WCF 转换为 WEB API:在 WEB API 中使用包装器重用 WCF 服务是个好主意吗?【英文标题】:Converting WCF to WEB API : Is it a good idea to reuse the WCF services with a wrapper in WEB API [duplicate] 【发布时间】:2018-02-19 07:50:39 【问题描述】:

我想从我的 wcf rest 服务迁移到 web api。拥有包装服务是否是个好主意,即从 API 端点调用 WCF 服务?请提出建议。

谢谢

【问题讨论】:

【参考方案1】:

如果您的服务没有 WCF 特定代码并且仅包含业务逻辑,那么这将是一个好主意。您的 WEB API 控制器将只是包装器,主要处理路由、参数映射和返回 HTTP 状态代码以及适当的结果。

编辑:

如果您的 WCF 服务有一些 WCF 特定代码,例如身份验证,那么我建议提取独立于基础架构的代码。然后,您将能够使用从您的 WEB API 控制器中提取的新类。

在这种大规模重构的情况下,我通常建议使用集成或端到端测试来覆盖整个组件。可以在现有代码和基础设施之上编写测试。提取代码并使用 WEB API 控制器包装后,您将能够运行相同的测试套件以确保没有重大更改。

关于 WEB API 中的身份验证,我建议查看以下 SO:Web API Authentication best practice

【讨论】:

WCF 休息服务有自己的逻辑。在 Wcf 服务中,我有一个从“ServiceAuthorizationManager”继承的类和下面的逻辑来进行身份验证。 var reqMsgProp = (HttpRequestMessageProperty)operationContext.IncomingMessageProperties["httpRequst"];字符串 authHeader = reqMsgProp.Headers[HttpRequestHeader.Authorization]; var authMessage = CheckIfAuthenticateMethod(operationContext); if (authMessage == true) 返回 true;在 WEB API 2 架构中,我使用基于令牌的身份验证。那么如何进行身份验证部分呢?

以上是关于WCF,WebAPI,WCFREST和WebService的区别的主要内容,如果未能解决你的问题,请参考以下文章

WCF、Web API、WCF REST 和 Web 服务之间的区别?

WCF REST 到 Web API [关闭]

何时使用 WCF REST 和 WEB API

wcf RESTful服务和WEB API的区别

将 WCF 转换为 WEB API:在 WEB API 中使用包装器重用 WCF 服务是个好主意吗?

如果我有一个来自Web API 2项目的身份验证过滤器,我可以在WCF服务中重用它吗?