如何通过 Web 服务获取 NHibernate 实体列表

发布于 2024-11-28 08:30:41 字数 892 浏览 0 评论 0原文

我的网络服务是:

[WebMethod]        
public List<Activity> ActivityInf()
{
       List<Activity> operationActivities = (List<Activity>)operationActivityCtrl.Result;
       return operationActivities;
}

事实上,当我更改列表时,我看到了操作活动。但是当我运行它时,我遇到了一些错误。

“/”应用程序中的服务器错误。

无法序列化 System.Collections.Generic.IList`1[[Test.Core.Model.DS.PCus、Test.Core.Model、Version=1.0.0.0、Culture] 类型的成员 Test.Core.Model.DS.Act.PCustomerList =neutral, PublicKeyToken=null]] 因为它是一个接口。

描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.NotSupportedException:无法序列化 System.Collections.Generic.IList`1 类型的成员 Test.Core.Model.DS.Act.PCustomerList[[Test.Core.Model.DS.PCus, Test.Core.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 因为它是一个接口。

源错误:

当前 Web 请求执行期间生成未处理的异常。有关异常来源和位置的信息可以使用下面的异常堆栈跟踪来识别。

My web service is:

[WebMethod]        
public List<Activity> ActivityInf()
{
       List<Activity> operationActivities = (List<Activity>)operationActivityCtrl.Result;
       return operationActivities;
}

In fact when i changed List, i saw operationActivities. but while i was run it, i got some error.

Server Error in '/' Application.

Cannot serialize member Test.Core.Model.DS.Act.PCustomerList of type System.Collections.Generic.IList`1[[Test.Core.Model.DS.PCus, Test.Core.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Cannot serialize member Test.Core.Model.DS.Act.PCustomerList of type System.Collections.Generic.IList`1[[Test.Core.Model.DS.PCus, Test.Core.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-12-05 08:30:41

从异常看来,您的 Activity 对象包含不可序列化的属性(不可序列化的 IList)。通常,当您需要通过WCFWeb服务传输NHibernate实体时,您实际上并不传输实体,而是使用DTO

DTO 是比您的实体更简单的对象,仅包含您需要传递的信息,而不是包含复杂对象图的整个 NHibernate 实体。这些还将包含列表而不是 IList 的具体实现。基本上它们是可序列化的。如果您搜索 DTO 和 NHibernate,可以找到有关此主题的更多信息。

From the exception it looks like your Activity object contains properties that are not serializable (an IList which is not serializable). Generally when you need to transfer NHibernate entities over WCF or web services you don't actually transfer the entity but instead use a DTO.

DTOs are even simpler objects than your entities containing only the information you need to pass rather than the whole NHibernate entity which can contain a complex object graph. These will also contain concrete implementations of Lists instead of IList. Basically they are serializable. There is much more information on this topic if you search for DTOs and NHibernate.

独﹏钓一江月 2024-12-05 08:30:41

您可能在 Activity 对象中拥有仍然附加到 NHibernate 代理的属性。避免这种情况的快速方法是使用 Eager [Not.LazyLoad()] 加载。如果您使用的是 Fluent NHibernate,则如下所示:

References<Activity>(x => x.ActivityType, "ActivityType_Id")
    .Nullable()
    .Column("ActivityType_Id")
    .Not.LazyLoad()
    .ForeignKey("Id");

我通常喜欢在我的 Dtos 模型项目中拥有一个命名空间。就像 MyCompany.MyModel.Entities (对于 NHibernate)和 MyCompany.MyModel.Dtos (对于我需要序列化的东西)。

然后我使用 AutoMapper 将给定的实体对象转换为相应的 Dto 并将其序列化,例如:

var activityDto = Mapper.Map<Activity, Dtos.Activity>(activity);
return JsonSerializer.SerializeToString<Dtos.Activity>(activityDto);

You likely have properties in the Activity object that are still attached to the NHibernate proxy. The quick way to avoid this would be to use Eager [Not.LazyLoad()] loading. If you're using Fluent NHibernate this looks like:

References<Activity>(x => x.ActivityType, "ActivityType_Id")
    .Nullable()
    .Column("ActivityType_Id")
    .Not.LazyLoad()
    .ForeignKey("Id");

I generally like to have a namespace in my model project for Dtos. Like MyCompany.MyModel.Entities (for NHibernate) and MyCompany.MyModel.Dtos (for stuff I need to serialize).

Then I use AutoMapper to convet a given Entity object to the respective Dto and serialize it, like:

var activityDto = Mapper.Map<Activity, Dtos.Activity>(activity);
return JsonSerializer.SerializeToString<Dtos.Activity>(activityDto);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文