【问题标题】:WCF Linq to SQL Table - System.Data.Linq.Table cannot be serializedWCF Linq to SQL Table - System.Data.Linq.Table 无法序列化
【发布时间】:2009-12-01 15:47:12
【问题描述】:

当我浏览似乎有效的演示时,我无法弄清楚这一点。我有一个 WCF 服务,我正在尝试使用 Linq to SQL。但是,我得到的只是错误 System.Data.Linq.Table 无法序列化。因此,我从自己的班级开始,认为我可以将其重新构建,直到出现错误。问题是即使尝试使用空类我也会收到错误消息。仅在我的方法上使用“As System.Linq.Table(Of xxx)”就会出现此错误。

无法序列化类型“System.Data.Linq.Table`1[LinqADMRequest2b]”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。


Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Runtime.Serialization
Imports System.Collections.Generic
Imports Linq

<ServiceContract(Namespace:="")> _
<ServiceBehavior(IncludeExceptionDetailInFaults:=True)> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class ComplyTrackWCFService

     _
    Public Function GetTestRequests() As System.Data.Linq.Table(Of LinqADMRequest2b)
        'Dim ct As New Linq2.ComplyTrackDataContext()
        'Dim queryresults = ct.ADMRequests 'ct.ADMRequestGetListByUser("", "155")
        'Return queryresults
    End Function

End Class

<DataContract()> _
<Serializable()> _
Public Class LinqADMRequest2b
    Implements ISerializable

    Private _firstName As String
     _
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal Value As String)
            _firstName = Value
        End Set
    End Property

    Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData

    End Sub

End Class

如您所见,GetTestRequests() 没有做任何事情,然后说它将返回 System.Data.Linq.Table(Of LinqADMRequest2b)

我无法让 LinqADMRequest2b 序列化。

无法序列化类型“System.Data.Linq.Table`1[LinqADMRequest2b]”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。

【问题讨论】:

    标签: linq wcf linq-to-sql serialization


    【解决方案1】:

    不要从您的服务返回Table&lt;T&gt;。它是一种复杂的可查询类型,依赖于其 DataContext,而不是内存中的集合。

    请务必返回List&lt;T&gt;,您可以通过调用System.Linq.Enumerable.ToList()Table&lt;T&gt; 转换为List&lt;T&gt;

    【讨论】:

      【解决方案2】:

      试试看

      &lt;DataMember&gt; 属性在你自己的类的属性上。

      另外,最好创建轻量级的 DataContract 对象来传递线路,而不是像 linq 表这样的大块头。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 1970-01-01
        • 1970-01-01
        • 2012-09-03
        • 1970-01-01
        相关资源
        最近更新 更多