C# ASP.NET WebAPI + 肥皂

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11317572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:23:09  来源:igfitidea点击:

ASP.NET WebAPI + Soap

c#asp.net-mvc-4asp.net-web-api

提问by John Mitchell

Does WebAPI support SOAP? I'm trying to write a SOAP Server in MVC4 and whilst I can do it in WCF it seems that WebAPI is replacing this but I see no ways to utilize SOAP in this yet, just JSON / XML using a REST Style interface.

WebAPI 是否支持 SOAP?我正在尝试在 MVC4 中编写一个 SOAP 服务器,虽然我可以在 WCF 中做到这一点,但似乎 WebAPI 正在取代它,但我还没有看到在这方面使用 SOAP 的方法,只是使用 REST 样式接口的 JSON/XML。

采纳答案by Sandman

To quote Scott Guthrie: The last few years have seen the rise of Web APIs - services exposed over plain HTTP rather than through a more formal service contract (like SOAP or WS*).

引用 Scott Guthrie 的话:过去几年见证了 Web API 的兴起 - 通过普通 HTTP 而不是通过更正式的服务合同(如 SOAP 或 WS*)公开的服务。

So I would say no.

所以我会说不。

回答by Paolo del Mundo

WEB API is Microsoft's answer to REST based apis. If you want SOAP, go with WCF.

WEB API 是微软对基于 REST 的 api 的回答。如果您想要 SOAP,请使用 WCF。

回答by Eamon Nerbonne

You might want to look at ServiceStackwhich should support both SOAP and REST in one interface without any hassle. It claims to be a better match than WebAPI for web services.

您可能想看看ServiceStack,它应该在一个界面中同时支持 SOAP 和 REST,没有任何麻烦。它声称比 WebAPI 更适合用于 Web 服务

I can't claim to know everything about the difference, but the problems they claim are inherent in the Web API approach I can say from experience arereal - API evolution (inevitable in a real project) is quite tricky in web api. And of course, web api doesn't support SOAP.

我不能声称知道差异的所有内容,但他们声称的问题是 Web API 方法固有的问题,我可以从经验真实的 - API 演变(在实际项目中不可避免)在 Web API 中非常棘手。当然,web api 不支持 SOAP。

回答by mythz

You should consider looking at ServiceStackwhich allows your same service to support REST + SOAP APIs, although since SOAP only works over HTTP POST it has some restrictions

您应该考虑查看ServiceStack,它允许您的相同服务支持 REST + SOAP API,尽管由于 SOAP 仅适用于HTTP POST,因此它有一些限制

Add ServiceStack Reference

添加 ServiceStack 参考

As an alternative to SOAP, ServiceStack offers a better alternative to WCF's Add Service Referencewhich can generate a typed API from a URL using Add ServiceStack Referencefeature that's built into ServiceStackVS.

作为替代SOAP,ServiceStack提供了一个更好的替代WCF的添加服务引用,可以生成从使用URL类型化的API添加ServiceStack参考的是内置于功能ServiceStackVS

Advantages over WCF

与 WCF 相比的优势

  • SimpleUses a small T4 template to save generated POCO Types. Updating as easy as re-running T4 template
  • VersatileClean DTOs works in all JSON, XML, JSV, MsgPack and ProtoBuf generic service clients
  • ReusableGenerated DTO's are not coupled to any endpoint or format. Defaults are both partial and virtual for maximum re-use
  • ResilientMessaging-based services offer a number of advantages over RPC Services
  • FlexibleDTO generation is customizable, Server and Clients can override built-in defaults
  • IntegratedRich Service metadata annotated on DTO's, Internal Servicesare excluded when accessed externally
  • Simple使用一个小的 T4 模板来保存生成的 POCO 类型。更新就像重新运行 T4 模板一样简单
  • VersatileClean DTOs 适用于所有 JSON、XML、JSV、MsgPack 和 ProtoBuf通用服务客户端
  • 可重用生成的 DTO 不耦合到任何端点或格式。默认值既是部分又是虚拟的,以实现最大程度的重用
  • 基于弹性消息传递的服务提供了许多优于 RPC 服务优势
  • 灵活的DTO 生成是可定制的,服务器和客户端可以覆盖内置默认值
  • 在 DTO 上注释的集成富服务元数据,外部访问时排除内部服务

WebAPI and WCF both promote RPC method signatures

WebAPI 和 WCF 都促进了 RPC 方法签名

What's interesting is that despite WebAPI ApiController methodshaving taken the same RPC approach as WCFin using C# RPC methods to create and define chatty web services with, that they're still not able to support their own SOAP standard made by the same company.

有趣的是,尽管WebAPI ApiController 方法采用与 WCF相同的RPC 方法来使用 C# RPC 方法来创建和定义健谈的 Web 服务,但它们仍然无法支持由同一家公司制定的自己的 SOAP 标准。

ServiceStack supports REST, SOAP, HTML and MQ endpoints with same service

ServiceStack 支持具有相同服务的 REST、SOAP、HTML 和 MQ 端点

This is a testament to ServiceStack's message-based designwhich offers numerous advantagesnot withstanding being able for the same serviceto support multiple endpoints and formats including REST, SOAP and MQ endpoints as well as generating server-side or client-side HTML websitesif you need it. Here's an example of a rich Northwind database editorthat because it was built with ServiceStack automatically enables a typed REST APIs that is able to be called with rich native Desktop clients, Mobile Apps and Single Page Apps.

这是对 ServiceStack基于消息的设计的证明,它提供了许多优势,尽管能够让同一服务支持多个端点和格式,包括 REST、SOAP 和 MQ 端点以及生成服务器端或客户端 HTML 网站,如果您需要它。这是一个丰富的Northwind 数据库编辑器的示例,因为它是使用 ServiceStack 构建的,因此可以自动启用类型化的 REST API,该 API 可以被丰富的原生桌面客户端、移动应用程序和单页应用程序调用。

SOAP is still a poor option for remote services

SOAP 仍然是远程服务的糟糕选择

Although despite supporting SOAP for interoperability, accessibility and backwards compatibility reasons, we don't recommend it for building web services platforms with as it's un-necessarily complex, brittle, slow and verbose and there are much better alternatives to use. I explain more in detail in my interview on InfoQ.

尽管出于互操作性、可访问性和向后兼容性原因支持 SOAP,但我们不建议将它用于构建 Web 服务平台,因为它不必要地复杂、脆弱、缓慢和冗长,并且有更好的替代方案可供使用。我在 InfoQ 的采访中做了更详细的解释

回答by Konamiman

WebApi does not support SOAP out of the box, indeed. But it is a quite flexible framework and you could "adapt" it to handle SOAP: nothing prevents you from manually parsing the received SOAP messages (they are plain XML after all) and manually generating the responses as XML strings, then sending them with the approproate content-type header (you could even write your own content formatterfor this).

事实上,WebApi 不支持开箱即用的 SOAP。但它是一个非常灵活的框架,您可以“调整”它来处理 SOAP:没有什么可以阻止您手动解析接收到的 SOAP 消息(毕竟它们是纯 XML)并手动将响应生成为 XML 字符串,然后使用适当的内容类型标头(您甚至可以为此编写自己的内容格式化程序)。

Depending on your needs and your existing codebase, this may be worth the effort or you may want to use a more SOAP-firendly technology such as WCF or the already mentioned ServiceStackframework.

根据您的需要和您现有的代码库,这可能是值得的,或者您可能想要使用更 SOAP 友好的技术,例如 WCF 或已经提到的ServiceStack框架。

回答by mavi

It is not like Wed API supports SOAP but due to SOAP is just a standard that uses XML and it travels throw HTTP you can use Web API to expose a POST Service to read the XML and find the nodes you need using XPath and then Deserialize the nodes to objects.

它不像 Wed API 支持 SOAP,但由于 SOAP 只是一个使用 XML 的标准,它会传递 HTTP,您可以使用 Web API 公开 POST 服务以读取 XML 并使用 XPath 找到您需要的节点,然后反序列化节点到对象。

First, you need to add XML support to the ConfigureServices

首先,您需要向 ConfigureServices 添加 XML 支持

public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddXmlSerializerFormatters();
    }

Then in your controller, you just need to add a method that receives an XMLDocument and with XPath search for the node you are interested (Basically removing the soap envelope, the header, the body) and then you can deserialize the object. In my case, I add the service reference using the WSDL and with that, I deserialize the object.

然后在您的控制器中,您只需要添加一个接收 XMLDocument 的方法,并使用 XPath 搜索您感兴趣的节点(基本上删除肥皂信封、标题、正文),然后您可以反序列化该对象。就我而言,我使用 WSDL 添加服务引用,然后反序列化对象。

[HttpPost("reservationxml")]
    public void CreateReservationFromTSW(XmlDocument soapCreateReservationRq)
    {
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(soapCreateReservationRq.NameTable);
        nsmgr.AddNamespace("r", "http://soa.company.com/ReservationEnt");
        nsmgr.AddNamespace("s", "http://www.w3.org/2003/05/soap-envelope");

        XmlNodeList xmlNodeList = soapCreateReservationRq.SelectNodes("s:Envelope/s:Body/r:CreateReservationRq",nsmgr);
        XmlNode xmlnode = xmlNodeList[0];
        XmlSerializer serial = new XmlSerializer(typeof(ServiceReference1.CreateReservationRqType));
        ServiceReference1.CreateReservationRqType rq = (ServiceReference1.CreateReservationRqType)serial.Deserialize(new XmlNodeReader(xmlnode));


    }

As you can see in the next image the service that tries to consume your service uses a Request Method POST with the Accept-Encoding: gzip. That is why you can expose a Web API that can be consumed for a SOAP service. enter image description here

正如您在下图中所见,尝试使用您的服务的服务使用带有 Accept-Encoding: gzip 的请求方法 POST。这就是为什么您可以公开可用于 SOAP 服务的 Web API。 在此处输入图片说明