c# - 无法从 WCF 数据服务返回自定义类

标签 c# wcf-data-services return-type service-operations

我正在尝试从我的 WCF 数据服务返回自定义类。我的自定义类是:

[DataServiceKey("ID")]
public class Applist {
    public int ID { get; set; }
    public string Name { get; set; }
}

我的数据服务看起来像:

public static void InitializeService(IDataServiceConfiguration config)
{
    config.RegisterKnownType(typeof(Applist));
    config.SetEntitySetAccessRule("*", EntitySetRights.All);
    config.SetServiceOperationAccessRule("GetApplications", ServiceOperationRights.AllRead);
}

[WebGet]
public IQueryable<Applist> GetApplications() {
    var result = (from p in this.CurrentDataSource.Applications
          orderby p.ApplicationName
          group p by p.ApplicationName into g
          select new Applist { ID = g.Min(p => p.id), Name = g.Key });

    return result.AsQueryable();
}

但是当我运行该服务时,它给我一个错误:

Request Error Request Error The server encountered an error processing the request. 
The exception message is 'Unable to load metadata for return type
'System.Linq.IQueryable`1[ApplicationService.Applist]' of method
'System.Linq.IQueryable`1[ApplicationService.Applist] GetApplications()'

相同的查询在 LINQPad 中运行得非常好。

最佳答案

引用下面的博客。它详细解释了这种情况和可能的解决方案: http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/

关于c# - 无法从 WCF 数据服务返回自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851133/

相关文章:

c# - 'Service Reference' 和 'Web Reference' 之间的不同行为

c# - 字段 'LogicalName' 缺少必需的成员 'Target'

silverlight-4.0 - 在 “system.runtime.serialization.iextensibledataobject”上编译错误

android - 使用mysql数据库在远程服务器上托管Wcf服务并在Android中使用它

c# - 返回匿名类型

c# - 具有相同类的两个独立单例路径的依赖注入(inject)

c# - 如何从pdf文件中找到页眉页脚

c# - asp.net 中的第一个数据 api 证书创建错误

javascript - 为什么我的 HTML 构造函数对象返回 [object Object] 而不是 [HTMLElementElement]?

c++ - 从模板函数返回迭代器到 STL 容器