【发布时间】:2013-06-19 19:22:44
【问题描述】:
我正在编写一个RESTful Web 服务,但在自己进行基本步骤时遇到了很大的麻烦。
我可以做到以下几点:
可以创建 RESTful Web 服务并使用
webHttpBinding将 XML(不是 soap+xml)发送和接收回客户端-
可以创建 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,这是没有意义的