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

时间:2010-10-03 18:56:03

标签: 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中运行得非常好。

1 个答案:

答案 0 :(得分:2)

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