Web Services 如何在C#的REST风格服务中发送和接收soap+xml

8xiog9wr  于 2022-11-15  发布在  C#
关注(0)|答案(1)|浏览(352)

我想用C#编写用于企业移动的设备管理的REST风格的Web服务,以发布由Microsoft定义的以下请求:

发布请求信息:

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

发布响应信息

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内容,而看不到任何soap头和正文元素。

[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的响应?

pieyvz9o

pieyvz9o1#

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

相关问题