如何在C#中的RESTful服务中发送和接收soap + xml

时间:2013-06-17 19:41:16

标签: xml web-services rest soap mdm

我想在C#中为企业移动设备管理编写RESTful Web服务,以发布下面由Microsoft定义的请求:

POST REQUEST INFO:

**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>

POST回复信息

Header:
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>

我想知道如何在我的Web服务中为POST调用创建WebInvoke方法?

使用下面的原型我能够发送和接收soap xml,但不确定这是否正确实现?

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

此外,我尝试使用下面定义的类对象,但是使用WCF测试客户端我可以看到生成的soap xml,但是当尝试从测试应用程序访问此Web服务时,我只获取了xml内容class对象,没有看到任何soap标题和body元素。

[OperationContract]
[WebInvoke(Method = "POST",
    UriTemplate = "",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Xml,
    ResponseFormat = WebMessageFormat.Xml)]
DiscoverResponse Discover(Discover request);

其中DiscoverResponse是用于POST响应的DataContract类,而Discover是用于POST请求的DataContract类。

我需要对上面的方法进行哪些更改才能获得整个soap xml而不是body xml的响应?

提前感谢您的答案。

1 个答案:

答案 0 :(得分:0)

WebInvoke永远不会返回带有WebHttpBinding的soap + xml。它只支持返回JSON或纯XML。如果您需要基于SOAP的XML,只需使用BasicHttpBindingWsHttpBinding在端点上调用服务即可。它们都是基于肥皂的绑定。您可以为服务创建两个端点,一个使用标准XML的WebHttpBinding,另一个使用其他任何绑定来获取soap + xml。