简体   繁体   English

ASP.NET Web API接口(WSDL)

[英]ASP.NET Web API interface (WSDL)

I have get some information about ASP Web API. 我已经获得了一些有关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 ? 它看起来像Web服务的好东西但是如何像WCF服务那样为我的API创建类似WSDL的东西呢? how 3d party component can use my service? 3d party组件如何使用我的服务? Or i need to describe each my method manually ? 或者我需要手动描述我的每个方法?

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是一个RESTful API,而不是基于SOAP的,因此没有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

There is no SOAP or WSDL support in WebApi. WebApi中没有SOAP或WSDL支持。 If you like WebApi you will love ServiceStack who has support for both REST and SOAP. 如果你喜欢WebApi,你会喜欢支持REST和SOAP的ServiceStack You can genereate WSDL with service stack even for REST Services. 即使对于REST服务,您也可以使用服务堆栈生成WSDL。

This is a slightly different scenario than what the OP probably intended to ask about, but is a more broad interpretation of "how to create something like WSDL for my API like WCF service does?" 这比什么OP可能为了问一个稍微不同的情景,而是更宽泛的解释“如何为我的API像WCF服务并创造像WSDL?”

I had a situation where we were not able to expose a WCF service, and the only option was WebAPI. 我遇到了无法公开WCF服务的情况,唯一的选择是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. 但是,使用API​​的一方只支持SOAP / WSDL,并且有一个预定义的WSDL,它们需要集成商来托管和遵守。

Serving the WSDL file 提供WSDL文件

One WebAPI action served the WSDL file, which was just a static WSDL file. 一个WebAPI操作服务于WSDL文件,该文件只是一个静态WSDL文件。 This approach does not support querying parts of the WSDL. 此方法不支持查询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. 因此客户端必须使用URL请求yourdomain.com/SomeRoot/SomeApiPath?wsdl ,之后的任何查询字符串参数都将被忽略,并且将提供完整的WSDL。 The parameter [FromUri] string wsdl ensures this action is chosen for the URL with ?wsdl in it but will not have any value in it. 参数[FromUri] string wsdl确保为其中包含?wsdl的URL选择此操作,但不会包含任何值。

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. 虽然WebAPI可以将参数绑定到XML请求,但我选择在我的操作中没有参数,而是在每个操作中使用Request.Content.ReadAsStringAsync()来获取请求主体(这是XML SOAP请求),然后使用它来解析它XML到LINQ以获取我需要的特定值。 This saved me from trying to reverse engineer an XML serializable POCO to match the WSDL defined request structure. 这使我免于尝试对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. 您可以使用Svcutil.exe等工具与Visual Studio生成XML可序列化的POCO。 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. 由于我们没有使用WCF,因此您不会使用完整的服务合同,而只需提取C#类POCO,以便您可以使用数据填充它们并将它们序列化为XML以创建响应。 Creating SOAP envelopes that have all the correct namespace references is extremely challenging though. 创建具有所有正确命名空间引用的SOAP信封是非常具有挑战性的。 I hacked around in some places and actually used string concatenation instead of XML serialization. 我在一些地方乱砍,实际上使用字符串连接而不是XML序列化。 Serialize to an XML string, and return that in a StringContent response: 序列化为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. 注意:必须捕获偶然的异常并将其作为SOAP信封内的SOAP Fault转换为XML。

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. 所有上述可怕的解决方法都证明,如果您绝对必须支持SOAP,那么使用除WebAPI之外的任何东西都会容易得多。 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. 我喜欢WebAPI,但是当你必须与另一个只支持SOAP / WSDL的系统集成时,它肯定不是工作的工具。 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. 我提供以上内容作为解决此问题的方法的摘要,当您没有其他选项时,但建议使用除了具有SOAP支持的WebAPI之外的框架。 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. 您肯定会遇到上述问题,并且需要具备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. 对于拥有预定义WSDL并要求其他人实现公开该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公开的服务,并且您必须实现客户端来使用它,这通常要容易得多。

ServiceStack is a good alternative which includes built-in support for SOAP which 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 Reference which can generate a typed API from just a URL using Add ServiceStack Reference . ServiceStack还提供了一个更好的WCF 添加服务引用的替代方法,它可以使用Add ServiceStack Reference从URL生成类型化的API。

Advantages over WCF 优于WCF的优势

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM