域服务方法不编译;声明“返回类型必须是实体......”

时间:2010-04-09 02:11:24

标签: wcf-ria-services

我有一个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对象的可调用方法吗?

5 个答案:

答案 0 :(得分:4)

您在此处拥有的代码:

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

很好,但是你还需要一个IEnumerable来完成这项工作:

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

这意味着,对于WCF RIA服务返回自定义类型,它需要至少有一个方法用于返回该类型的IEnumerable的自定义类型。

答案 1 :(得分:2)

YasserMohamedMCTS在Silverlight Forum上回答了这个问题。

答案 2 :(得分:1)

简单地添加 在调用方法之上的[Query]属性。

答案 3 :(得分: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)
        {
              ....    
        } 

答案 4 :(得分:-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();
        }             
    }

}