2013-05-21 93 views
1

我有一個實體框架5的DbContext頂部的DataService類:消費WCF數據服務行動提供了實體框架

public class MyDataService : DataService<MyDbContext>, IServiceProvider 
{ 

    [WebGet] 
    public IQueryable<Product> GetProducts1(int category) 
    { 
     return from p in this.CurrentDataSource.Products 
       where p.Category == category 
       select p; 
    } 
} 

現在我要揭露從我的DbContext到DataService的方法和使用此示例代碼:http://efactionprovider.codeplex.com/

public class MyDbContext : DbContext 
{ 
    public DbSet<Product> Products { get; set; } 

    [NonBindableAction] 
    public IQueryable<Product> GetProducts2(int category) 
    { 
     return from p in this.CurrentDataSource.Products 
       where p.Category == category 
       select p; 
    } 

} 

訪問http://localhost:12345/MyDataService.svc/$metadata show that both methods are known, but the first one hat a米:列舉HTTPMethod = 「GET」`attribut

<EntityContainer Name="MyDbContext" m:IsDefaultEntityContainer="true"> 
    ... 
    <FunctionImport Name="GetProducts1" ReturnType="Collection(MyNameSpace.Product)" EntitySet="Products" m:HttpMethod="GET"> 
     <Parameter Name="category" Type="Edm.Int32" Nullable="false"/> 
    </FunctionImport> 
    <FunctionImport Name="GetProducts1" ReturnType="Collection(MyNameSpace.Product)" EntitySet="Products"> 
     <Parameter Name="category" Type="Edm.Int32" Nullable="false"/> 
    </FunctionImport> 
    ... 

我可以通過訪問網址

http://localhost:12345/MyDataService.svc/GetProducts1?category=1 

這並不爲GetProducts2(因爲propably GET不允許) 工作執行GetProducts1但我設法通過使用招來執行GetProducts2:

POST: http://localhost:12345/MyDataService.svc/GetProducts1 
Request Headers: 
    User-Agent: Fiddler 
    Host: localhost:12345 
    Content-Length: 12 
    Content-Type: application/json 

Request Body: 
    {category:1} 

好吧,現在是我的問題:我使用服務引用使用Windows應用程序使用此服務。由於DataServiceContext派生類中的代碼生成不包含我需要自己調用它們的操作。

因爲我可以做的第一個(GetProducts1):

public IEnumerable<Product> GetProducts1(int category) 
    { 
     var proxy = new MyDataServiceContext(
      "http://localhost:12345/MyDataService.svc"); 
     var queryString = String.Format("{0}/GetProducts1?category={1}", 
      proxy.BaseUri, category); 
     var uri = new Uri(queryString, UriKind.RelativeOrAbsolute); 
     return proxy.Execute<Product>(uri); 
    } 

但我有第二個掙扎。我想:

public IEnumerable<Product> GetProducts2(int category) 
    { 
     var proxy = new MyDataServiceContext(
      "http://localhost:12345/MyDataService.svc"); 
     var queryString = String.Format("{0}/GetProducts2", 
      proxy.BaseUri); 
     var uri = new Uri(queryString, UriKind.RelativeOrAbsolute); 
     return proxy.Execute<Product>(uri, "POST", false, 
      new UriOperationParameter("category", category)); 
    } 

但我得到一個DataServiceClientException: Content-Type-Header value missing.(狀態代碼400)

有什麼辦法來調用這個方法我使用Execute方法?我寧願繼續使用DataServiceContext而不自己提出任何原始請求。

在此先感謝

btw。

我使用視覺studion 2010和的NuGet,而不是默認System.Data.Services的dll Microsoft.Data.ServicesMicrosoft.Data.Services.Client包,因爲我相信我需要設置config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;在首位執行行動。

回答

1

嘗試使用BodyOperationParameter而不是UriOperationParameter。 Action請求中包含Action參數,而不是Uri。

+0

事實上,它就像這樣簡單。非常感謝。我用這個掙扎了很長時間。即使在瞭解了BodyOperationParameter的存在之後,我發現的唯一例子就是http://msdn.microsoft.com/en-us/library/hh859851(v=vs.103).aspx(底部的最後一個代碼塊)我已經訪問過這個頁面,但忽略了最後一部分。 –