类序列化问题

时间:2011-06-16 14:52:22

标签: .net vb.net wcf class serialization

我在下面的课程中遇到错误,但我不确定它在哪里。该类将在我的服务中正确返回,但不会序列化并返回任何结果。任何帮助将不胜感激。

Imports System
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses
Imports System.Data.EntityClient
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Runtime.Serialization

' <summary>  
'<para>This class is used to load Email objects.</para>  
' <para>
' CreateDate: 6-14-2011
' ModifyDate: 6-14-2011
' ModifyDesc: Initial Creation
' </para>
' </summary>  
' 
#Region "Email"
''' <summary>
''' Email class.
''' </summary>
<DataContractAttribute(IsReference:=True)>
<KnownTypeAttribute(GetType(CompanyLocEmail))>
<KnownTypeAttribute(GetType(CompanyEmail))>
<KnownTypeAttribute(GetType(UserEmail))>
Partial Public MustInherit Class Email

    #Region "Declarations"
    <DataMemberAttribute()>
    Private _IsPreferred As Nullable(Of Global.System.Boolean)
    <DataMemberAttribute()>
    Private _EmailTypeL As Global.System.String
    <DataMemberAttribute()>
    Private _EmailTypeS As Global.System.String
    <DataMemberAttribute()>
    Private _EmailAddress As Global.System.String
    <DataMemberAttribute()>
    Private _XID As Global.System.Int32
    <DataMemberAttribute()>
    Private _EID As Global.System.Int32
    #End Region

    #Region "Primitive Properties"
    #Region "IsPreferred"
    ''' <summary>
    ''' IsPrefferred.
    ''' </summary>
    Public Property IsPreferred() As Nullable(Of Global.System.Boolean)
        Get
            Return _IsPreferred
        End Get
        Set(value As Nullable(Of Global.System.Boolean))
            OnIsPreferredChanging(value)
            _IsPreferred = value
            OnIsPreferredChanged()
        End Set
    End Property

    Partial Private Sub OnIsPreferredChanging(value As Nullable(Of Global.System.Boolean))
    End Sub

    Partial Private Sub OnIsPreferredChanged()
    End Sub
    #End Region

    #Region "EmailTypeL"
    ''' <summary>
    ''' EmailTypeL.
    ''' </summary>
    Public Property EmailTypeL() As Global.System.String
        Get
            Return _EmailTypeL
        End Get
        Set(value As Global.System.String)
            OnEmailTypeLChanging(value)
            _EmailTypeL = value
            OnEmailTypeLChanged()
        End Set
    End Property

    Partial Private Sub OnEmailTypeLChanging(value As Global.System.String)
    End Sub

    Partial Private Sub OnEmailTypeLChanged()
    End Sub
    #End Region

    #Region "EmailTypsS"
    ''' <summary>
    ''' EmailTypeS.
    ''' </summary>
    Public Property EmailTypeS() As Global.System.String
        Get
            Return _EmailTypeS
        End Get
        Set(value As Global.System.String)
            OnEmailTypeSChanging(value)
            _EmailTypeS = value
            OnEmailTypeSChanged()
        End Set
    End Property

    Partial Private Sub OnEmailTypeSChanging(value As Global.System.String)
    End Sub

    Partial Private Sub OnEmailTypeSChanged()
    End Sub
    #End Region

    #Region "EmailAddress"
    ''' <summary>
    ''' EmailAddress.
    ''' </summary>
    Public Property EmailAddress() As Global.System.String
        Get
            Return _EmailAddress
        End Get
        Set(value As Global.System.String)
            OnEmailAddressChanging(value)
            _EmailAddress = value
            OnEmailAddressChanged()
        End Set
    End Property

    Partial Private Sub OnEmailAddressChanging(value As Global.System.String)
    End Sub

    Partial Private Sub OnEmailAddressChanged()
    End Sub
    #End Region

    #Region "EID"
    ''' <summary>
    ''' EID.
    ''' </summary>
    Public Property EID() As Global.System.Int32
        Get
            Return _EID
        End Get
        Set(value As Global.System.Int32)
            If (_EID <> value) Then
                OnEIDChanging(value)
                _EID = value
                OnEIDChanged()
            End If
        End Set
    End Property

    Partial Private Sub OnEIDChanging(value As Global.System.Int32)
    End Sub

    Partial Private Sub OnEIDChanged()
    End Sub
    #End Region

    #Region "XID"
    ''' <summary>
    ''' XID.
    ''' </summary>
    Public Property XID() As Global.System.Int32
        Get
            Return _XID
        End Get
        Set(value As Global.System.Int32)
            If _XID <> value Then
                OnXIDChanging(value)
                _XID = value
                OnXIDChanged()
            End If
        End Set
    End Property

    Partial Private Sub OnXIDChanging(value As Global.System.Int32)
    End Sub

    Partial Private Sub OnXIDChanged()
    End Sub
    #End Region

    #End Region
End Class
#End Region

#Region "CompanyEmail"
''' <summary>
''' CompanyEmail class.
''' </summary>
<DataContractAttribute(IsReference:=True)>
Partial Public Class CompanyEmail
    Inherits Email

    #Region "Declarations"
    <DataMemberAttribute()>
    Private _Company As Company
    #End Region

    #Region "Factory Method"
    ''' <summary>
    ''' Create a new CompanyEmail object.
    ''' </summary>
    ''' <param name="emailTypeL">Initial value of the EmailTypeL property.</param>
    ''' <param name="emailTypeS">Initial value of the EmailTypeS property.</param>
    ''' <param name="emailAddress">Initial value of the EmailAddress property.</param>
    ''' <param name="eID">Initial value of the EID property.</param>
    ''' <param name="xID">Initial value of the XID property.</param>
    Public Shared Function CreateCompanyEmail(emailTypeL As Global.System.String,             emailTypeS As Global.System.String, emailAddress As Global.System.String, eID As Global.System.Int32, xID As Global.System.Int32) As CompanyEmail
        Dim companyEmail As CompanyEmail = New CompanyEmail
        companyEmail.EmailTypeL = emailTypeL
        companyEmail.EmailTypeS = emailTypeS
        companyEmail.EmailAddress = emailAddress
        companyEmail.EID = eID
        companyEmail.XID = xID
        Return companyEmail
    End Function
    #End Region

    #Region "ParentProperties"
    <XmlIgnoreAttribute()>
    <SoapIgnoreAttribute()>
    Public Overridable Property Company() As Company
        Get
            Return _Company
        End Get
        Set(value As Company)
            _Company = value
        End Set
    End Property
    #End Region
End Class
#End Region

#Region "CompanyLocEmail"
''' <summary>
''' CompanyLocEmail class.
''' </summary>
<DataContractAttribute(IsReference:=True)>
Partial Public Class CompanyLocEmail
    Inherits Email

    #Region "Declarations"
    Private _CompanyLoc As CompanyLoc
    #End Region

    #Region "Factory Method"
    ''' <summary>
    ''' Create a new CompanyLocEmail object.
    ''' </summary>
    ''' <param name="emailTypeL">Initial value of the EmailTypeL property.</param>
    ''' <param name="emailTypeS">Initial value of the EmailTypeS property.</param>
    ''' <param name="emailAddress">Initial value of the EmailAddress property.</param>
    ''' <param name="eID">Initial value of the EID property.</param>
    ''' <param name="xID">Initial value of the XID property.</param>
    Public Shared Function CreateCompanyLocEmail(emailTypeL As Global.System.String, emailTypeS As Global.System.String, emailAddress As Global.System.String, eID As Global.System.Int32, xID As Global.System.Int32) As CompanyLocEmail
        Dim companyLocEmail As CompanyLocEmail = New CompanyLocEmail
        companyLocEmail.EmailTypeL = emailTypeL
        companyLocEmail.EmailTypeS = emailTypeS
        companyLocEmail.EmailAddress = emailAddress
        companyLocEmail.EID = eID
        companyLocEmail.XID = xID
        Return companyLocEmail
    End Function
    #End Region

    #Region "ParentProperties"
    <DataMemberAttribute()>
    <XmlIgnoreAttribute()>
    <SoapIgnoreAttribute()>
    Public Overridable Property CompanyLoc() As CompanyLoc
        Get
            Return _CompanyLoc
        End Get
        Set(value As CompanyLoc)
            _CompanyLoc = value
        End Set
    End Property
    #End Region
End Class
#End Region

#Region "UserEmail"
''' <summary>
''' UserEmail class.
''' </summary>
<DataContractAttribute(IsReference:=True)>
Partial Public Class UserEmail
    Inherits Email

    #Region "Declarations"
    Private _UserRole As UserRole
    #End Region

    #Region "Factory Method"
    ''' <summary>
    ''' Create a new UserEmail object.
    ''' </summary>
    ''' <param name="emailTypeL">Initial value of the EmailTypeL property.</param>
    ''' <param name="emailTypeS">Initial value of the EmailTypeS property.</param>
    ''' <param name="emailAddress">Initial value of the EmailAddress property.</param>
    ''' <param name="eID">Initial value of the EID property.</param>
    ''' <param name="xID">Initial value of the XID property.</param>
    Public Shared Function CreateUserEmail(emailTypeL As Global.System.String, emailTypeS As Global.System.String, emailAddress As Global.System.String, eID As Global.System.Int32, xID As Global.System.Int32) As UserEmail
        Dim userEmail As UserEmail = New UserEmail
        userEmail.EmailTypeL = emailTypeL
        userEmail.EmailTypeS = emailTypeS
        userEmail.EmailAddress = emailAddress
        userEmail.EID = eID
        userEmail.XID = xID
        Return userEmail
    End Function
    #End Region

    #Region "ParentProperties"
    <DataMemberAttribute()>
    <XmlIgnoreAttribute()>
    <SoapIgnoreAttribute()>
    Public Overridable Property UserRole() As UserRole
        Get
            Return _UserRole
        End Get
        Set(value As UserRole)
            _UserRole = value
        End Set
    End Property
    #End Region
End Class
#End Region

接口定义

<OperationContract()> _
Function GetEmail(iID As Integer) As UserEmail

实际通话

    Public Function GetEmail(iID As Integer) As UserEmail Implements InsightDataAPIInterface.GetEmail
    Dim ret = (From c In db.Emails Where c.EID = iID Select c).Take(1)
    Dim y As UserEmail
    For Each x In ret
        y = CType(x, UserEmail)
    Next x
    'Breakpoint  here shows correct data in y, so y returns the class
    Return y
End Function

这不是GetEmail的最后一次迭代,但是我在它的当前状态下使用它来试图弄清楚为什么我没有让类正确地序列化。如果有人能够朝着正确的方向推动我,我将非常欠他们的债务。 :)

1 个答案:

答案 0 :(得分:0)

尝试在<DataContract>声明中添加命名空间属性。在Visual Basic中,我看到很多情况,其中客户端/服务器项目的根命名空间属性是不同的(它们默认为项目名称,预计会有所不同) - 并且根命名空间最终用于定义CLR类的名称空间。如果没有显式设置数据协定的命名空间,那么WCF将使用CLR命名空间,如果客户端/服务器类位于不同的命名空间中,您将看到类似于您拥有的错误。

<DataContractAttribute(IsReference:=True, Namespace:="http://my.namespace.com")>