2010-04-09 28 views
4

我有一個WCF RIA域服務包含我想,當用戶點擊一個按鈕來調用一個方法:域服務方法不編譯;索賠「返回類型必須是實體......」

[Invoke] 
public MyEntity PerformAnalysis(int someId) 
{ 
    return new MyEntity(); 
} 

然而,當我嘗試編譯我提示以下錯誤:

Operation named 'PerformAnalysis' does not conform to the required signature. 
Return types must be an entity, collection of entities, or one of the 
predefined serializable types. 

的事情是,據我所知,myEntity所實體:

[Serializable] 
public class MyEntity: EntityObject, IMyEntity 
{ 
    [Key] 
    [DataMember] 
    [Editable(false)] 
    public int DummyKey { get; set; } 

    [DataMember] 
    [Editable(false)] 
    public IEnumerable<SomeOtherEntity> Children { get; set; } 
} 

我想我在這裏錯過了一些簡單的東西。有人能告訴我如何創建一個返回單個MyEntity對象的可調用方法嗎?

回答

2

這個問題由YasserMohamedMCTS答覆Silverlight Forum

+0

鏈接不起作用。 – gius 2012-11-11 17:29:11

+0

謝謝,我已將它們更新到web.archive.org鏈接。 – 2012-11-11 22:02:51

-1

在服務器端項目創建自己的類,如:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Runtime.Serialization; 
using System.ComponentModel.DataAnnotations; 
using System.Data.Objects.DataClasses; 

namespace yournamespace 
{ 
    [DataContract]  
    public class Custom : EntityObject 
    { 

     [DataMember()] 
     [Key()] 
     public int id { set; get; } 

     [DataMember()] 
     public string name { set; get; } 

     public Custom() 
     { 
      name = "Pouya"; 
     } 
    } 
} 

添加你的方法到您的DomainService在服務器端的項目,如:

public Custom GetCustom() 
    { 
     return new Custom(); 
    } 

在客戶端這些代碼添加到您的網頁之一側項目

public partial class Admin : Page 
{ 
    LoadOperation<Custom> operation; 
    Custom ali = new Custom(); 
    public Admin() 
    { 
     InitializeComponent(); 
    } 

    // Executes when the user navigates to this page. 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     operation = DomainContext.Load(DomainContext.GetCustomQuery());    
     operation.Completed += new EventHandler(operation_Completed); 
    } 

    void operation_Completed(object sender, EventArgs e) 
    { 
     if (!operation.HasError) 
     { 
      ali = operation.Entities.FirstOrDefault(); 
     }    
    } 

} 
1

只需加上 [查詢] attribut e在invoke方法之上。

4

,你在這裏的代碼:

[Invoke] 
public MyEntity PerformAnalysis(int someId) 
{ 
    return new MyEntity(); 
} 

是好的,但你還需要一個IEnumerable的,使這項工作:

public IEnumerable<MyEntity> GetMyEntities() 
{ 
    throw new NotImplementedException(); 
} 

這也就意味着,對於WCF RIA服務來回報自定義類型,它需要至少有一個自定義類型的方法返回該類型的IEnumerable。

0

有時你必須拿出一個類構造函數,它會編譯沒有錯誤。

這是一個編譯正確的示例。

public class PluginControlCommandView 
    { 
     public Nullable<DateTime> CreationTime { get; set; } 

     public string Description { get; set; } 

     public Nullable<Guid> PlayerControlCommandID { get; set; } 

     public Nullable<Guid> EventFramePluginID { get; set; } 

     public Nullable<DateTime> ExecutionTime { get; set; } 

     public Nullable<Guid> ID { get; set; } 

     public Nullable<bool> IsConsole { get; set; } 

     public Nullable<bool> IsExecuted { get; set; } 

     public PluginCommands PluginCommand { get; set; } 
     // !!! You can see that here is a IEnumerable! :) 
     public IEnumerable<PluginCommandDetailView> PluginCommandDetails { get; set; } 

     public PluginStates PluginState { get; set; } 
    } 


     [Invoke] 

     public void UpdatePluginControlCommandView(PluginControlCommandView commandView) 
     { 
       ....  
     } 
相關問題