简体   繁体   English

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

[英]How send and receive soap+xml in RESTful services in C#

I want write the RESTful web service for Enterprise Mobile Device Management in C# to post the request show below which is defined by Microsoft:我想用 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 RESPONSE INFO发布回复信息

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>

I would like to know how should I create WebInvoke method for POST call in my web service?我想知道我应该如何在我的网络服务中为 POST 调用创建 WebInvoke 方法?

With the below prototype I am able to send and receive the soap xml, but not sure whether this right to way to implement?使用下面的原型,我可以发送和接收soap xml,但不确定这种方法是否可以实现?

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

Also I have tried using the class object as defined below, but with WCF Test client I could see the soap xml getting generated, but when tried to access this web service from the test application, I get only the xml content for the class object and not seeing any soap header and body elements.我也尝试过使用下面定义的类对象,但是使用 WCF 测试客户端我可以看到生成的 soap xml,但是当尝试从测试应用程序访问这个 Web 服务时,我只得到类对象的 xml 内容和没有看到任何肥皂标题和正文元素。

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

where DiscoverResponse is DataContract class for POST response and Discover is the DataContract class for POST request.其中 DiscoverResponse 是 POST 响应的 DataContract 类, Discover 是 POST 请求的 DataContract 类。

What changes do I need to make to above method to get response with entire soap xml instead of only the body xml?我需要对上述方法进行哪些更改才能获得整个soap xml而不是仅body xml的响应?

The WebInvoke will never return soap+xml with the WebHttpBinding . WebInvoke永远不会返回带有WebHttpBinding的 soap+xml。 It only supports returning either JSON or pure XML.它只支持返回 JSON 或纯 XML。 If you want soap-based XML, just call the service on endpoints using either the BasicHttpBinding or the WsHttpBinding .如果您想要基于肥皂的 XML,只需使用BasicHttpBindingWsHttpBinding在端点上调用服务。 They're both soap-based bindings.它们都是基于肥皂的绑定。 You could create two endpoints for the service, one with the WebHttpBinding for standard XML and one with either of the other bindings to get soap+xml.您可以为服务创建两个端点,一个使用WebHttpBinding用于标准 XML,一个使用其他任何绑定来获取 soap+xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM