发送和接收soap + xml的ReSTful Web服务

时间:2013-06-19 19:22:45

标签: wcf rest

我正在撰写 RESTful网络服务,并且在完成基本步骤方面遇到了很大麻烦。

我可以做到以下几点:

  1. 可以使用webHttpBinding

  2. 创建RESTful Web服务并将XML(而不是soap + xml)发送回客户端
  3. 可以使用下面定义的合同创建RESTful Web服务并发送和接收soap + xml:

    [OperationContract]
         [WebInvoke(Method = "POST",
             UriTemplate = "",
             BodyStyle = WebMessageBodyStyle.Bare,
             RequestFormat = WebMessageFormat.Xml,
             ResponseFormat = WebMessageFormat.Xml)]        
         XmlElement PostRequestXML(Stream xmlData);
    
  4. 点(2)是否是发送和接收soap + xml数据的正确方法?

    我在网上进行了大量搜索,但找不到更好的链接,这解释了创建发送和接收soap + xml的Web服务的详细步骤。

    我想知道:

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

    • 我需要使用哪种绑定? SHare,如果有任何例子。

      • 您能否与我分享有关撰写OperationContract和配置web.config?
      • 的详细信息

    请求和回复应如下所示。

    REQUEST:

    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>
    

1 个答案:

答案 0 :(得分:3)

我创建了许多WCF API,其中一些是REST。 我的主要理解是 - 不要使用它! WCF可以让您选择在服务器和客户端之间使用的协议类型。 使用它,您可以获得性能和耐用性。 如果你已经开始关注REST(这是一个很好的选择),我建议你深入研究ServiceStack。这是迄今为止.NET最成熟,最专业,最先进的REST框架 通过在途中做出许多明智的选择,它易于使用并帮助您正确构建API。 试一试,永不回头!

相关问题