2010-07-08 22 views
0

我和我的一些好友正在嘗試開始使用WCF數據服務,所以讓我先介紹一下我們迄今所做的:WCF數據服務不能在飼料條目處理更新

  1. 我們創建了一個相當簡單的WCF數據服務,其數據源實現了IUpdatable接口,並通過一些公共的IQueryable屬性(代碼附在底部)公開了一些數據。使用Visual Studio 2010,起初我們在IIS 7中運行我們的服務,但由於我們無法弄清楚錯誤,我們決定使用Cassini(Webdev webserver)來運行它。

  2. 我們在C#中編寫了一個客戶端來使用該服務。客戶端按所有不同的數據操作(創建,讀取,更新和刪除)工作。到現在爲止還挺好!在IIS 7 Web服務器上託管服務時,我們必須使用POST隧道來進行更新和刪除工作,但現在它按預期工作。

  3. 當我們嘗試使用我們的Java(Restlet)和Ruby(ruby_odata)客戶端使用服務時,出現問題:我們無法用這些客戶端更新數據(我們得到「500 Internal Server Error」和「Method不允許「從服務器回覆)。我們使用了兩個非常簡單的教程[a,b]來創建我們的客戶。因此我們認爲我們的問題在於我們的服務。

a。 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 Feed。

我們在監視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; 
     }*/ 
    } 
} 

回答