ASP.NET Web API 接口 (WSDL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9500037/
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
ASP.NET Web API interface (WSDL)
提问by Arbejdsgl?de
I have get some information about ASP Web API. It is look like good stuff for web services but how to create something like WSDL for my API like WCF service does ? how 3d party component can use my service? Or i need to describe each my method manually ?
我得到了一些关于 ASP Web API 的信息。对于 Web 服务来说,这看起来很不错,但是如何像 WCF 服务那样为我的 API 创建类似 WSDL 的东西呢?3d 派对组件如何使用我的服务?或者我需要手动描述我的每个方法?
回答by Pharabus
As to whether it looks good, thats an opinion so try it and see (I personally like it)
至于好看不好看,那是一个意见,试试看(我个人喜欢)
As far as a WDSL the Web API is a RESTful API not SOAP based so there is no WSDL support, WCF has REST support and SOAP so that may be a better choice if you require a SOAP service and WSDL, ScottGu's latest blog on the API is quite interesting and has links to tutorials (the WSDL generation question is answered in the comments too)
就 WDSL 而言,Web API 是基于 SOAP 的 RESTful API,因此没有 WSDL 支持,WCF 具有 REST 支持和 SOAP,因此如果您需要 SOAP 服务和 WSDL(ScottGu 关于 API 的最新博客),这可能是更好的选择非常有趣,并且有教程链接(WSDL 生成问题也在评论中得到了回答)
http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx
http://weblogs.asp.net/scottgu/archive/2012/02/23/asp-net-web-api-part-1.aspx
回答by riadh gomri
There is no SOAP or WSDL support in WebApi. If you like WebApi you will love ServiceStackwho has support for both REST and SOAP. You can genereate WSDL with service stack even for REST Services.
WebApi 中没有 SOAP 或 WSDL 支持。如果您喜欢 WebApi,您一定会喜欢同时支持 REST 和 SOAP 的ServiceStack。您甚至可以为 REST 服务生成带有服务堆栈的 WSDL。
回答by AaronLS
This is a slightly different scenario than what the OP probably intendedto ask about, but is a more broad interpretation of "how to create something like WSDL for my API like WCF service does?"
这与 OP 可能打算询问的情况略有不同,但它是对“如何像 WCF 服务那样为我的 API 创建类似 WSDL 之类的东西”的更广泛解释?
I had a situation where we were not able to expose a WCF service, and the only option was WebAPI. However the party consuming the API only supported SOAP/WSDL and had a predefined WSDL that they required integrator to host and conform to.
我遇到过无法公开 WCF 服务的情况,唯一的选择是 WebAPI。然而,使用 API 的一方仅支持 SOAP/WSDL,并且有一个预定义的 WSDL,他们要求集成商托管并遵守该 WSDL。
Serving the WSDL file
提供 WSDL 文件
One WebAPI action served the WSDL file, which was just a static WSDL file. This approach does not support querying parts of the WSDL. So the client must use URL request yourdomain.com/SomeRoot/SomeApiPath?wsdl, any query string parameters after that will be ignored and the full WSDL will be served. The parameter [FromUri] string wsdlensures this action is chosen for the URL with ?wsdlin it but will not have any value in it.
一个 WebAPI 操作为 WSDL 文件提供服务,该文件只是一个静态 WSDL 文件。这种方法不支持查询 WSDL 的部分内容。因此客户端必须使用 URL request yourdomain.com/SomeRoot/SomeApiPath?wsdl,之后的任何查询字符串参数都将被忽略并提供完整的 WSDL。该参数[FromUri] string wsdl确保为其中包含的 URL 选择此操作?wsdl,但其中没有任何值。
public IHttpActionResult SomeApiPath([FromUri] string wsdl)
{
System.IO.FileStream wsdlFileStream = System.IO.File.OpenRead(System.Web.HttpContext.Current.Server.MapPath("~/Content/SomeThing.wsdl"));
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(wsdlFileStream);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
return ResponseMessage(response);
}
This means your API Action methods need to handle and respond to XML SOAP requests.
这意味着您的 API Action 方法需要处理和响应 XML SOAP 请求。
Handling SOAP request
处理 SOAP 请求
While WebAPI can bind parameters to XML requests, I opted to have no parameters in my actions and instead I used Request.Content.ReadAsStringAsync()in each action to get the request body (which is the XML SOAP request) and then parsed it using XML to LINQ to get specific values I needed. This saved me from trying to reverse engineer an XML serializable POCO to match the WSDL defined request structure.
虽然 WebAPI 可以将参数绑定到 XML 请求,但我选择在我的操作中不使用参数,而是Request.Content.ReadAsStringAsync()在每个操作中使用来获取请求正文(即 XML SOAP 请求),然后使用 XML 解析它到 LINQ 以获取特定值我需要。这使我免于尝试对 XML 可序列化 POCO 进行逆向工程以匹配 WSDL 定义的请求结构。
Creating the SOAP response
创建 SOAP 响应
You can use tools such as Svcutil.exe with Visual Studio to generate XML serializable POCO's. Since we aren't using WCF, you won't use the full service contract, but just pull out the C# class POCOs so that you can populate them with data and serialize them to XML to create a response. Creating SOAP envelopes that have all the correct namespace references is extremely challenging though. I hacked around in some places and actually used string concatenation instead of XML serialization. Serialize to an XML string, and return that in a StringContent response:
您可以将 Svcutil.exe 等工具与 Visual Studio 结合使用来生成 XML 可序列化的 POCO。由于我们没有使用 WCF,因此您不会使用完整的服务契约,而只需提取 C# 类 POCO,以便您可以使用数据填充它们并将它们序列化为 XML 以创建响应。但是,创建具有所有正确名称空间引用的 SOAP 信封极具挑战性。我在一些地方进行了修改,实际上使用了字符串连接而不是 XML 序列化。序列化为 XML 字符串,并在 StringContent 响应中返回:
return ResponseMessage(
new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(soapResponseBody, System.Text.Encoding.UTF8, "text/xml")
});
Note: Even exceptions must be caught and converted to XML as a SOAP Fault inside a SOAP envelope.
注意:即使异常也必须被捕获并转换为 XML 作为 SOAP 信封内的 SOAP 错误。
All of the above terrible workarounds are evidence that if you absolutely must support SOAP, using anything besides WebAPI is going to be much easier.I love WebAPI, but when you have to integrate with another system that only supports SOAP/WSDL, it is certainly not the tool for the job. I provide the above as a summary of the approach to working around this problem when you have no other option, but recommend using a framework besides WebAPI that has SOAP support.You most certainly will run into problems with the above, and will need to have lots of experience with XML serialization and XML schemas to understand how to get through these problems.
所有上述糟糕的解决方法都证明,如果您绝对必须支持 SOAP,那么使用除 WebAPI 之外的任何东西都会容易得多。我喜欢 WebAPI,但是当您必须与另一个仅支持 SOAP/WSDL 的系统集成时,它肯定不是这项工作的工具。当您别无选择时,我将上述内容作为解决此问题的方法的摘要,但建议使用除 WebAPI 之外的具有 SOAP 支持的框架。您肯定会遇到上述问题,并且需要在 XML 序列化和 XML 模式方面具有丰富的经验才能理解如何解决这些问题。
It's also pretty odd/rare for someone to have a predefined WSDL and ask others to implement services that expose that WSDL. In other words, they integrate from their side as a client, and you are the host, but they dictate the structure of the requests. Usually it's the other way around, where someone has a service they expose with a predefined WSDL, and you must implement a client to consume it, which is generally a lot easier.
某人拥有预定义的 WSDL 并要求其他人实现公开该 WSDL 的服务也很奇怪/很少见。换句话说,他们作为客户端从他们这边进行集成,而您是主机,但他们决定了请求的结构。通常情况相反,有人使用预定义的 WSDL 公开服务,您必须实现一个客户端来使用它,这通常要容易得多。
回答by mythz
ServiceStackis a good alternative which includes built-in support for SOAPwhich automatically generates WSDLs, XSDs and Schema Descriptions from your Service Definitions, available from your auto-generated Metadata Pages.
ServiceStack是一个很好的替代方案,它包括对 SOAP 的内置支持,它从您的服务定义自动生成 WSDL、XSD 和架构描述,可从您的自动生成的元数据页面获得。
Add ServiceStack Reference
添加 ServiceStack 参考
ServiceStack also offers a better alternative to WCF's Add Service Referencewhich can generate a typed API from just a URL using Add ServiceStack Reference.
ServiceStack 还为 WCF 的Add Service Reference提供了更好的替代方案,它可以使用Add ServiceStack Reference仅从 URL 生成类型化 API 。
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

