【问题标题】:How to get OData v3 WCF Data Service call to communicate using v3 protocol如何获取 OData v3 WCF 数据服务调用以使用 v3 协议进行通信
【发布时间】:2013-10-18 00:17:14
【问题描述】:

我遇到了一个有趣的情况。我有两个应用程序调用 Microsoft WCF OData 服务上的函数:

  1. Android 使用 OData4J 库 (v0.7)
  2. iOS 使用 OData4ObjC 库(@​​987654321@ 用于 iOS 6.1)

OData 服务是使用 Microsoft WCF 数据服务 v5.6 编写的。我在我的服务类中的方法上使用了[WebGetAttribute()] 属性:

namespace MyServices
{
    public class MyService : DataService<MyEntities>
    {
        public static void InitializeService(DataServiceConfiguration config)
            {
                config.UseVerboseErrors = true;
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
                config.SetServiceOperationAccessRule("MyServiceCall", ServiceOperationRights.ReadSingle);
            }

        [WebGet()]
        public IQueryable<MyComplexType> MyServiceCall()
        {
            return AListOfMyComplexTypes();
        }
    }
}

当 Android 应用调用 OData 函数时,WCF 数据服务会以复杂类型的 v1 集合进行响应。当 iOS 库对 WCF 数据服务进行函数调用时,它期望使用 v3 返回集合。

我的问题是:

  1. 是否可以让 OData4ObjC 库使用 OData v1 协议进行通信?
  2. 是否可以让 WCF 数据服务使用 OData v3 协议进行响应?
  3. 我需要做什么才能使第 1 项和/或第 2 项工作?


这就是我所说的 OData v1 响应的意思:
<MyServiceCall
    xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <element m:type="MyService">
        <Id m:type="Edm.Int16">1</Id>
        <Name>Bariatric</Name>
        <IsEnabled m:type="Edm.Boolean">true</IsEnabled>
    </element>
    <element m:type="MyService">
        <Id m:type="Edm.Int16">2</Id>
        <Name>General</Name>
        <IsEnabled m:type="Edm.Boolean">true</IsEnabled>
    </element>
</MyServiceCall>

这就是我所说的 OData v3 响应的意思:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="http://services.odata.org/V3/(S(ii5qwgb20wk0ptgfvvtlpwoy))/OData/OData.svc/" 
    xmlns="http://www.w3.org/2005/Atom" 
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"         
    xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>
http://services.odata.org/V3/(S(ii5qwgb20wk0ptgfvvtlpwoy))/OData/OData.svc/GetProductsByRating</id>
<title type="text">GetProductsByRating</title>
<updated>2013-10-17T21:56:42Z</updated>
<link rel="self" title="GetProductsByRating" href="GetProductsByRating" />
<entry>
<id>http://services.odata.org/V3/(S(ii5qwgb20wk0ptgfvvtlpwoy))/OData/OData.svc/Products(1)/id>
    <category term="ODataDemo.Product" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" title="Product" href="Products(1)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Category" type="application/atom+xml;type=entry" title="Category" href="Products(1)/Category" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Supplier" type="application/atom+xml;type=entry" title="Supplier" href="Products(1)/Supplier" />
    <title type="text">Milk</title>
    <summary type="text">Low fat milk</summary>
    <updated>2013-10-17T21:56:42Z</updated>
    <author>
        <name />
    </author>
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Category" type="application/xml" title="Category" href="Products(1)/$links/Category" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Supplier" type="application/xml" title="Supplier" href="Products(1)/$links/Supplier" />
    <m:action metadata="http://services.odata.org/V3/(S(ii5qwgb20wk0ptgfvvtlpwoy))/OData/OData.svc/$metadata#DemoService.Discount" title="Discount" target="http://services.odata.org/V3/(S(ii5qwgb20wk0ptgfvvtlpwoy))/OData/OData.svc/Products(1)/Discount" />
        <content type="application/xml">
            <m:properties>
                <d:ID m:type="Edm.Int32">1</d:ID>
                <d:ReleaseDate m:type="Edm.DateTime">1995-10-01T00:00:00</d:ReleaseDate>
                <d:DiscontinuedDate m:null="true" />
                <d:Rating m:type="Edm.Int32">3</d:Rating>
                <d:Price m:type="Edm.Decimal">3.5</d:Price>
            </m:properties>
        </content>
    </entry>
</feed>

【问题讨论】:

    标签: android wcf ios6 odata odata4j


    【解决方案1】:

    也许我误解了您的情况,但从您在此处发布的示例来看,我认为这与版本控制无关。对MyServiceCall 的调用是返回一个复杂值的集合,而对GetProductsByRating 的调用是返回一个实体的集合。由于 Atom 没有“复杂值的集合”的概念,因此 OData 定义了自己的 XML 格式。但 Atom 确实定义了“实体集合”的概念,因此 OData 重用了来自 Atom 的提要概念。

    所以我在这里没有看到版本控制的问题。这只是不同类型的有效载荷格式化方式之间的区别。

    如果您正在查看 HTTP 标头,并且您对为什么 v3 服务器会以指示有效负载为 v1 的标头进行响应感到困惑,请注意 WCF 数据服务服务器始终以所需的最低版本进行响应那个特别的反应。因此,如果您向支持 v3 的服务器询问没有 v3 功能的有效负载,服务器会将有效负载标记为 v2 或 v1(否则,它可能会阻止与 v3 不兼容但实际上能够读取此有效负载的客户端)。

    【讨论】:

    • 谢谢,珍。我明白你的意思了。这只是我的一个误解。
    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多