gpt4 book ai didi

c# - WCF 服务 - 序列化 Linq 参数

转载 作者:太空狗 更新时间:2023-10-29 21:59:55 27 4
gpt4 key购买 nike

我有一个简单的类:

[DataContract]
public class Book
{
[DataMember]
public Int32 ID { get; set; }

[DataMember]
public Nullable<Int32> AuthorID { get; set; }
}

这是通过 WCF 服务检索的,例如:

public class BookService : IBookService
{
IService _service;

public BookService()
{
_service = new BookService();
}

public IEnumerable<Book> GetBooks()
{
var Books = _service.GetBooks();
return Books;
}
}

我现在想扩展它以包含一个附加函数,该函数允许 LINQ 查询包含 Book 类的附加属性(上面的简化示例中未显示)。这个函数(在包含并从 MVC 应用程序中调用时有效)是:

    public IEnumerable<Book> GetBooks(params Expression<Func<Book, object>>[] includes)
{
var Books = _service.GetBooks(Expression<Func<Book, object>>[]>(includes));
return Books;
}

但是,当从 WCF 服务运行时,这会生成错误,因为它无法序列化:

System.ServiceModel.Description.DataContractSerializerOperationBehavior contract: http://tempuri.org/:IBookService ----> System.Runtime.Serialization.InvalidDataContractException: Type 'System.Linq.Expressions.Expression1[System.Func2[Foo.Book,System.Object]]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute.

我将 DataContract/DataMember 属性添加到类中,以便它可以被序列化,但是我不确定我需要做什么才能允许函数参数被序列化。

有人知道我可以做些什么来允许在 WCF 服务中使用此功能吗?

最佳答案

这可能与表达式的使用有关吗?:

Why would you use Expression<Func<T>> rather than Func<T>?

关于c# - WCF 服务 - 序列化 Linq 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49176428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com