我正在编写一个REST风格的Web服务,并且在自身基本步骤方面遇到很大麻烦。ReSTful Web服务发送和接收soap + xml
我可以做如下:
可以创建RESTful Web服务发送和接收XML(不能用肥皂+ XML)返回给客户端与
webHttpBinding
可以创建RESTful Web服务和发送和接收使用所述合同肥皂+ 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网络服务来发送和接收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>[email protected]</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)。谷歌应该提供大量关于如何做到这一点的例子。 – Tim
创建两个不同的点需要使用两个不同的地址,但协议对GET和POST使用相同的URI。我如何使用两个不同端点的相同URI? –
有趣的是在你的#2你没有指定一个模板Uri所以它怎么可能是RESTful! :)整个休息点是公开Uri模板,这意味着在这里你需要放置一些东西,所以我甚至不知道这是如何接近于RESTful。我在WCF看到很多人认为他们正在创造一些RESTful但他们甚至没有指定Uri,这是没有意义的 – PositiveGuy