从 JavaScript 使用 XML WCF REST web 服务 [英] consuming an XML WCF REST webservice from JavaScript

查看:27
本文介绍了从 JavaScript 使用 XML WCF REST web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 应用程序,它公开了一个 REST WCF 服务(通过 WebServiceHost),它的合同看起来像这样(简化):

I have a WPF application that exposes a REST WCF service (via WebServiceHost) with a contract that looks something like this (simplified):

[ServiceContract]
public interface IItemServiceContract
{
    [WebGet(UriTemplate = "Items/{id}")]
    Item GetItem(string id);

    [WebGet(UriTemplate = "Items")]
    IEnumerable<Item> GetItems();

    [WebInvoke(UriTemplate = "Items", Method = "PUT")]
    IEnumerable<Item> CreateItems(IEnumerable<Item> list);
}

当我使用浏览器导航到 http://localhost:8070/Contoso/Services/Items/ItemService/Items 时,我得到如下所示的响应:

When I navigate to http://localhost:8070/Contoso/Services/Items/ItemService/Items with a browser, I get a response that looks something like this:

<ArrayOfItem xmlns="http://schemas.datacontract.org/2004/07/Contodo.Services.Items" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Item>
    <ItemState>
      <Comment i:nil="true"/>
      <DeactivationTime>0001-01-01T00:00:00</DeactivationTime>
      <Id>18f1a5e4-a94a-4f37-a533-3a75a10e7373</Id>
      <IsSpecial>false</IsSpecial>
    </ItemState>
    <ItemTypeId>10</ItemTypeId>
    <HelpInfo/>
    <Identity>Ident1</Identity>
    <AdditionalInfo>
      &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;
      &lt;Content&gt;
        &lt;SpecialContent /&gt;
      &lt;/Content&gt;</AdditionalInfo>
    <TextParameter>kjhdfsjh kj dkfjg kj</TextParameter>
    <UserName i:nil="true"/>
  </Item>
</ArrayOfItem>

使用 JavaScript 使用此服务的简单且无摩擦的方法是什么?客户端如何快速构建 http 请求和相应的 XML?

What would be an easy and friction-free approach to consume this service with JavaScript? How can a client quickly build the http requests and the appropriate XML for it?

我相当熟悉 Html5/javaScript 世界,但在 C# 中,我会有一个以序列化为 XML 的 Item 对象为中心的 API.但这是要走的路吗?

I am fairly in the Html5/javaScript world but in a C# I would have an API in place that is centered around the Item object that gets serialized to XML. But is that the way to go for here?

更新:

根据第一条评论和答案,XML 似乎不是 JavaScript/网络浏览器消费者的理想格式,但我不能只是将格式更改为 JSON,因为这可能会破坏已经依赖此 XML 的现有客户端格式.所以理想情况下,我会进行 REST 内容类型协商并放置/获取 JSON XML.但是这可以通过 WCF REST 服务完成吗?

Based on the first comments and answers, it seems that XML is not the ideal format for a JavaScript/webbrowser consumer but I can't just change the format to JSON because that would probably break existing clients that already rely on this XML format. So ideally I would do REST content type negotiation and put/get JSON or XML. But can this be done with WCF REST services?

推荐答案

我想你使用的是 ASP.NET 4.X.

I suppose that you use ASP.NET 4.X.

WCF 4 支持基于 HTTP接受"的自动格式选择和请求的Content-Type"标头.一种在 web.config 文件中指定 automaticFormatSelectionEnabled="true" 属性:

WCF 4 supports automatic format selection based on HTTP "Accept" and "Content-Type" headers of requests. One specify automaticFormatSelectionEnabled="true" attribute in web.config file:

<configuration>
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- the "" standard endpoint is used for auto creating a web endpoint. -->
        <standardEndpoint name=""
                          helpEnabled="true"
                          automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

请参阅文章文章了解更多信息.您可以结合 带有 属性"nofollow">defaultOutgoingResponseFormat(您可以设置为xml"或json",默认已经是xml").您可以只为一个特定端点指定属性,而不是像上面的示例那样使用 standardEndpoint.

See "Message Format Selection" part of the article and the article for more information. You can combine automaticFormatSelectionEnabled attribute with defaultOutgoingResponseFormat (which you can set to "xml" or "json", default is already "xml"). You can specify the attributes only for one specific endpoint instead of usage standardEndpoint as in the example above.

因此,如果您使用相应的 WCF 配置,您的现有 WCF 服务只会为 JavaScript 请求提供 JSON 数据,并且仍会为其他客户端返回 XML 数据.

So your existing WCF service will just provide JSON data for JavaScript requests and still returns XML data for other client if you would use the corresponding WCF configuration.

这篇关于从 JavaScript 使用 XML WCF REST web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆