我和我的一些伙伴正在尝试开始使用 WCF 数据服务,所以让我先描述一下我们到目前为止所做的工作:
我们创建了一个相当简单的 WCF 数据服务,其数据源实现了 IUpdatable 接口并通过一些公共 IQueryable<> 属性公开了一些数据(代码附在底部)。起初我们使用 Visual Studio 2010 在 IIS 7 中运行我们的服务,但由于我们无法弄清楚的错误,我们决定改用 Cassini(Webdev 网络服务器)来运行它。
我们用 C# 编写了一个客户端来使用该服务。客户端按预期使用所有不同的数据操作(创建、读取、更新和删除)。到目前为止,一切都很好!在 IIS 7 网络服务器上托管服务时,我们必须使用 POST 隧道来进行更新和删除工作,但现在它按预期工作。
当我们尝试使用我们的 Java (Restlet) 和 Ruby (ruby_odata) 客户端使用该服务时,我们的问题出现了:我们无法使用这些客户端更新数据(我们收到“500 Internal Server Error”和“Method Not Allowed”的回复服务器) 。我们使用了两个相当简单的教程 [a,b] 来创建我们的客户。因此,我们相信我们的问题在于我们的服务。
一个。ruby_odata: http://rdoc.info/projects/visoft/ruby_odata
b. restlet:http://wiki.restlet.org/docs_2.0/13-restlet/28-restlet/287-restlet/288-restlet.html
这两个客户端都被列为 OData SDK ( http://www.odata.org/developers/odata-sdk ) 并且应该可以正常工作以使用 OData 提要。
我们在监控 HTTP 请求时注意到的一件事是,C# 客户端使用 HTTP MERGE 动词进行更新(查看此处了解更多信息:http://blogs.msdn.com/b/astoriateam/archive/2008/05/ 20/merge-vs-replace-semantics-for-update-operations.aspx),而 Java 和 Ruby 都使用 HTTP PUT 进行更新。这可能是只有我们的 C# 客户端工作的原因吗?我们可以做些什么来启用 PUT 更新?
我们刚刚开始使用 .NET,如果您在回答时可以考虑到这一点,我们将不胜感激
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Linq;
using System.Data.Services.Providers;
using System.Reflection;
using System.ServiceModel.Web;
using System.Data.Services.Common;
namespace WCFDataServiceApp
{
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public Category ProductCategory { get; set; }
}
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
}
public class AWData : IUpdatable
{
static List<Category> categories;
static List<Product> products;
static AWData()
{
categories = new List<Category>() {
new Category { ID = 1, Name = "Bikes" },
new Category { ID = 2, Name = "Parts" },
new Category { ID = 3, Name = "Wheels"},
};
products = new List<Product>() {
new Product { ID = 1, Name = "Red Bike", Color = "Red", ProductCategory = categories[0] },
new Product { ID = 2, Name = "Blue Bike", Color = "Blue", ProductCategory = categories[0] },
new Product { ID = 3, Name = "Green Bike", Color = "Green", ProductCategory = categories[0] },
new Product { ID = 4, Name = "Yellow Bike", Color = "Yellow", ProductCategory = categories[0] },
new Product { ID = 5, Name = "Pink Bike", Color = "Pink", ProductCategory = categories[0] },
new Product { ID = 6, Name = "Black Bike", Color = "Black", ProductCategory = categories[0] }
};
}
public IQueryable<Category> Categories
{
get { return categories.AsQueryable(); }
}
public IQueryable<Product> Products
{
get { return products.AsQueryable(); }
}
void IUpdatable.AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
{
System.Diagnostics.Debug.WriteLine("No support for AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)");
}
void IUpdatable.ClearChanges()
{
System.Diagnostics.Debug.WriteLine("ClearChanges()");
}
public object CreateResource(string containerName, string fullTypeName)
{
System.Diagnostics.Debug.WriteLine("CreateResource(string containerName, string fullTypeName)");
Type t = Type.GetType(fullTypeName);
// Check if resource exists
if (t != null)
{
object resource = Activator.CreateInstance(t);
if (containerName.Equals("Categories"))
{
categories.Add((Category)resource);
}
else if (containerName.Equals("Products"))
{
products.Add((Product)resource);
}
return resource;
}
// Current resource does not exist
return new Exception("Could not create a resource of type " + containerName);
}
void IUpdatable.DeleteResource(object targetResource)
{
// 1. Check object type
if (targetResource.GetType().IsInstanceOfType(new Category()))
{
System.Diagnostics.Debug.WriteLine("Category deleted!");
categories.Remove((Category)targetResource);
}
else if (targetResource.GetType().IsInstanceOfType(new Product()))
{
System.Diagnostics.Debug.WriteLine("Product deleted!");
products.Remove((Product)targetResource);
}
}
object IUpdatable.GetResource(IQueryable query, string fullTypeName)
{
System.Diagnostics.Debug.WriteLine("GetResource(IQueryable query, string fullTypeName)");
object obj = null;
foreach (object o in query)
{
obj = o;
}
return obj;
}
object IUpdatable.GetValue(object targetResource, string propertyName)
{
System.Diagnostics.Debug.WriteLine("GetValue(object targetResource, string propertyName)");
return null;
}
void IUpdatable.RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
{
System.Diagnostics.Debug.WriteLine("RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)");
}
object IUpdatable.ResetResource(object resource)
{
System.Diagnostics.Debug.WriteLine("ResetResource(object resource)");
return null;
}
object IUpdatable.ResolveResource(object resource)
{
return resource;
}
void IUpdatable.SaveChanges()
{
System.Diagnostics.Debug.WriteLine("SaveChanges()");
}
void IUpdatable.SetReference(object targetResource, string propertyName, object propertyValue)
{
System.Diagnostics.Debug.WriteLine("SetReference(object targetResource, string propertyName, object propertyValue)");
}
void IUpdatable.SetValue(object targetResource, string propertyName, object propertyValue)
{
PropertyInfo pi = targetResource.GetType().GetProperty(propertyName);
if (pi == null)
throw new Exception("Can't find property");
pi.SetValue(targetResource, propertyValue, null);
System.Diagnostics.Debug.WriteLine("Object " + targetResource + " updated value " + propertyName + " to " + propertyValue);
}
public void SetConcurrencyValues(object resourceCookie, bool? checkForEquality, IEnumerable<KeyValuePair<string, object>> concurrencyValues)
{
System.Diagnostics.Debug.WriteLine("SetConcurrencyValues(object resourceCookie, bool? checkForEquality, IEnumerable<KeyValuePair<string, object>> concurrencyValues) was called");
throw new Exception("SetConcurrencyValues(object resourceCookie, bool? checkForEquality, IEnumerable<KeyValuePair<string, object>> concurrencyValues) not implemented");
}
}
public class aw : DataService<AWData> //, IServiceProvider
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
//config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
/*
public object GetService(Type serviceType)
{
System.Diagnostics.Debug.WriteLine(serviceType.ToString());
return this;
}*/
}
}