我有一个有趣的情况。我有两个应用程序调用 Microsoft WCF OData 服务上的函数:
- Android 使用 OData4J 库 (v0.7)
- iOS 使用 OData4ObjC 库(iOS 6.1 的Elizabeth Duncan fork )
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 返回集合。
我的问题是:
- 是否可以让 OData4ObjC 库使用 OData v1 协议进行通信?
- 是否可以让 WCF 数据服务使用 OData v3 协议进行响应?
- 我需要做什么才能使第 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>