【问题标题】:ReSTful web service to send and receive soap+xmlReSTful web 服务发送和接收soap+xml
【发布时间】:2013-06-19 19:22:44
【问题描述】:

我正在编写一个RESTful Web 服务,但在自己进行基本步骤时遇到了很大的麻烦。

我可以做到以下几点:

  1. 可以创建 RESTful Web 服务并使用webHttpBinding 将 XML(不是 soap+xml)发送和接收回客户端

  2. 可以创建 RESTful web 服务并使用如下定义的合约发送和接收soap+xml:

    [OperationContract]
         [WebInvoke(Method = "POST",
             UriTemplate = "",
             BodyStyle = WebMessageBodyStyle.Bare,
             RequestFormat = WebMessageFormat.Xml,
             ResponseFormat = WebMessageFormat.Xml)]        
         XmlElement PostRequestXML(Stream xmlData);
    

第(2)点是发送和接收soap+xml数据的正确方法吗?

我在网上做了很多搜索,但找不到更好的链接,该链接解释了创建 web 服务以发送和接收soap+xml 的详细步骤。

我想知道:

  • 设计我的 RESTful Web 服务以发送和接收soap+xml 的更好方法是什么?你能告诉我操作合同的定义吗?

  • 我需要使用哪个绑定?如果有任何例子,请分享。

    • 你能告诉我写OperationContract和配置web.config的详细信息吗?

请求和响应应该如下所示。

请求:

Header:
 POST /EnrollmentServer/Discovery.svc HTTP/1.1
 Content-Type: application/soap+xml; charset=utf-8
 User-Agent: Windows Phone 8 Enrollment Client
 Host: EnterpriseEnrollment.Contoso.com
 Content-Length: xxx
 Cache-Control: no-cache

<?xml version="1.0"?>
 <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing"
 xmlns:s="http://www.w3.org/2003/05/soap-envelope">
 <s:Header>
 <a:Action s:mustUnderstand="1">
http://schemas.microsoft.com/windows/management/2012/01/enrollment/IDiscoveryService/Discover
 </a:Action>
 <a:MessageID>urn:uuid: 748132ec-a575-4329-b01b-6171a9cf8478</a:MessageID>
 <a:ReplyTo>
 <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
 </a:ReplyTo>
 <a:To s:mustUnderstand="1">
https://ENROLLTEST.CONTOSO.COM/EnrollmentServer/Discovery.svc
 </a:To>
 </s:Header>
 <s:Body>
 <Discover xmlns="http://schemas.microsoft.com/windows/management/2012/01/enrollment/">
 <request xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <EmailAddress>user@contoso.com</EmailAddress>
 <RequestVersion>1.0</RequestVersion>
 </request>
 </Discover>
 </s:Body>
 </s:Envelope>

回应:

标题:

 HTTP/1.1 200 OK
 Content-Length: 865
 Content-Type: application/soap+xml; charset=utf-8
 Server: EnterpriseEnrollment.Contoso.com
 Date: Tue, 02 Aug 2012 00:32:56 GMT



<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
 xmlns:a="http://www.w3.org/2005/08/addressing">
 <s:Header>
 <a:Action s:mustUnderstand="1">
http://schemas.microsoft.com/windows/management/2012/01/enrollment/IDiscoveryService/DiscoverResponse
 </a:Action>
 <ActivityId>
 d9eb2fdd-e38a-46ee-bd93-aea9dc86a3b8
 </ActivityId>
 <a:RelatesTo>urn:uuid: 748132ec-a575-4329-b01b-6171a9cf8478</a:RelatesTo>
 </s:Header>
 <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema">>
 <DiscoverResponse
 xmlns="http://schemas.microsoft.com/windows/management/2012/01/enrollment">
 <DiscoverResult>
 <AuthPolicy>OnPremise</AuthPolicy>
 <AuthUrl/>
 <EnrollmentPolicyServiceUrl>
https://enrolltest.contoso.com/ENROLLMENTSERVER/DEVICEENROLLMENTWEBSERVICE.SVC
 </EnrollmentPolicyServiceUrl>
 <EnrollmentServiceUrl>
https://enrolltest.contoso.com/ENROLLMENTSERVER/DEVICEENROLLMENTWEBSERVICE.SVC
 </EnrollmentServiceUrl>
 <FederatedServiceName/>
 <FederatedServicePolicy/>
 </DiscoverResult>
 </DiscoverResponse>
 </s:Body>
 </s:Envelope>

【问题讨论】:

  • 我现在不能举个例子(没时间),但您可以使用选项 2 并为您的服务公开端点 - 例如 WebHttpBinding(用于 REST)和 BasicHttpBinding(用于 SOAP )。 Google 应该提供大量示例来说明如何做到这一点。
  • 创建两个不同的点需要使用两个不同的地址,但协议对 GET 和 POST 使用相同的 URI。如何将相同的 URI 与两个不同的端点一起使用?
  • 有趣的是在你的 #2 中你没有指定模板 Uri 那么它怎么可能是 RESTful 的! :) 休息的全部意义在于公开 Uri 模板,这意味着您需要在这里放入一些东西,所以我什至看不出这与 RESTful 有多么接近。我在 WCF 中看到很多人们认为他们正在创建 RESTful 的东西,但他们甚至没有指定 Uri,这是没有意义的

标签: wcf rest


【解决方案1】:

我创建了许多 WCF API,其中一些是 REST。 我的主要理解是 - 不要使用它! WCF 很好地使您能够选择在服务器和客户端之间使用的协议类型。 使用它,您需要为性能和耐用性付出代价。 如果您已经决定使用 REST(这是一个很好的选择),我建议您深入研究ServiceStack。这是迄今为止最成熟、维护和先进的 .NET REST 框架 它易于使用并通过在途中做出许多明智的选择来帮助您正确构建 API。 试一试,永不回头!

【讨论】:

  • Microsoft 移动设备管理协议说我需要实现 RESTful Web 服务。因为 Web 服务客户端的某些部分是由 Microsoft 编写的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
相关资源
最近更新 更多