DirectoryServices.AccountManagement 扩展 Class WCF 应用程序中的序列化

DirectoryServices.AccountManagement Extension Class Serialization in WCF App

我有一个 class 我创建的名为 EmployeeData 的继承自 System.DirectoryServices.AccountManagement.UserPrincipal。它允许我使用 .但我现在正在尝试将其移至 WCF Web 服务。有没有办法让我序列化我的 Employee class 即使 UserPrincipal 不可序列化? IE:只是序列化某些属性或创建不同的架构?我是这个过程的新手,如果这是一个可怕的问题,请原谅我。

对于 WCF,有没有一种方法可以仅在某些属性上使用 DataMember,从而使 UserPrincipal class 不需要可序列化?

Imports System.DirectoryServices.AccountManagement

<DirectoryRdnPrefix("CN")>
<DirectoryObjectClass("Person")>
Public Class EmployeeData
    Inherits UserPrincipal

    Private _dataAccess As New DataAccess()

    Public Sub New(ByVal context As PrincipalContext)
        MyBase.New(context)
    End Sub

    <DirectoryProperty("samAccountName")>
    Public ReadOnly Property Username() As String
        Get
            Return SamAccountName
        End Get
    End Property

    <DirectoryProperty("givenName")>
    Public ReadOnly Property FirstName() As String
        Get
            Return GivenName
        End Get
    End Property

    <DirectoryProperty("sn")>
    Public ReadOnly Property LastName() As String
        Get
            Return Surname
        End Get
    End Property

    <DirectoryProperty("mail")>
    Public ReadOnly Property Email() As String
        Get
            Return EmailAddress
        End Get
    End Property

    <DirectoryProperty("employeeNumber")>
    Public ReadOnly Property EEID As String
        Get
            If ExtensionGet("employeeNumber").Length <> 1 Then
                Return Nothing
            Else
                Return ExtensionGet("employeeNumber")(0).ToString
            End If
        End Get
    End Property

    <DirectoryProperty("department")>
    Public ReadOnly Property Dept As String
        Get
            If ExtensionGet("department").Length <> 1 Then
                Return Nothing
            Else
                Return ExtensionGet("department")(0).ToString
            End If
        End Get
    End Property

    <DirectoryProperty("division")>
    Public ReadOnly Property Division As String
        Get
            If ExtensionGet("division").Length <> 1 Then
                Return Nothing
            Else
                Return ExtensionGet("division")(0).ToString
            End If
        End Get
    End Property

    '<DirectoryProperty("title")>
    Public ReadOnly Property JobTitle As String
        Get
            If EEID IsNot Nothing Then
                Return _dataAccess.GetJobTitle(EEID)
            Else
                If ExtensionGet("title").Length <> 1 Then
                    Return "No AD Title"
                Else
                    Return ExtensionGet("title")(0).ToString
                End If
            End If
        End Get
    End Property

    '<DirectoryProperty("manager")>
    Public ReadOnly Property ADManager As String
        Get
            If ExtensionGet("manager").Length <> 1 Then
                Return "No manager populated in AD"
            Else
                Dim m As UserPrincipal = UserPrincipal.FindByIdentity(Context, ExtensionGet("manager")(0).ToString)
                If m IsNot Nothing Then
                    Return m.GivenName & " " & m.Surname
                Else
                    Return "Error"
                End If
            End If
        End Get
    End Property

    <DirectoryProperty("telephoneNumber")>
    Public ReadOnly Property PhoneNumber As String
        Get
            If ExtensionGet("telephoneNumber").Length <> 1 Then
                Return "No Phone Number populated in AD"
            Else
                Return ExtensionGet("telephoneNumber")(0).ToString
            End If
        End Get
    End Property

    Public ReadOnly Property HireDate As String
        Get
            Dim r
            If String.IsNullOrEmpty(EEID) Then Return Nothing
            r = _dataAccess.GetHireDate(EEID)
            If IsNothing(r) Then Return Nothing

           Return r.ToShortDateString
        End Get
    End Property

    Public ReadOnly Property YearsOfService As String
        Get
            If IsNothing(HireDate) Then Return Nothing

            Dim dateStart = Date.Parse(HireDate)

            If dateStart.ToShortDateString = "01/01/0001" Then Return Nothing

            'todo format yos string
            If HireDate >= Date.Now Then Return "Starting employement on " & HireDate

            Dim span As TimeSpan
            Dim length As Date

            Try
                span = Date.Now.AddDays(-1) - dateStart
                length = Date.MinValue + span
            Catch ex As Exception
                Return "Not Available"
            End Try

            'note: minValue is 1/1/1 so we have to subtract
            Dim years As Integer = length.Year - 1
            Dim months As Integer = length.Month - 1
            Dim days As Integer = length.Day - 1

            Return years & IIf(years <> 1, " years, ", " year, ") &
                   months & IIf(months <> 1, " months, ", " month, ") &
                   days & IIf(days <> 1, " days ", " day")
        End Get
    End Property

    Public ReadOnly Property Supervisor As String
        Get
            If String.IsNullOrEmpty(EEID) Then Return Nothing

            Dim supervisorId As String = _dataAccess.GetSupervisor(EEID)

            If supervisorId Is Nothing Then Return Nothing

            Return _dataAccess.GetADProperty("employeeNumber", supervisorId, "givenName") & " " & _dataAccess.GetADProperty("employeeNumber", supervisorId, "sn")
        End Get
    End Property

    Public ReadOnly Property SupervisorUsername As String
        Get
            If String.IsNullOrEmpty(EEID) Then Return Nothing

            Dim supervisorId As String = _dataAccess.GetSupervisor(EEID)

            If supervisorId Is Nothing Then Return Nothing

            Return _dataAccess.GetADProperty("employeeNumber", supervisorId, "samAccountName")
        End Get
    End Property

    Public ReadOnly Property SupervisorEmail As String
        Get
            If String.IsNullOrEmpty(EEID) Then Return Nothing

            Dim supervisorId As String = _dataAccess.GetSupervisor(EEID)

            If supervisorId Is Nothing Then Return Nothing

            Return _dataAccess.GetADProperty("employeeNumber", supervisorId, "mail")
        End Get
    End Property

    Public ReadOnly Property Groups As IEnumerable
        Get
            Return _dataAccess.GetAdGroups(SamAccountName)
        End Get
    End Property

    Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityType As IdentityType,
                                                  identityValue As String) As EmployeeData
        Return DirectCast(FindByIdentityWithType(context, GetType(EmployeeData), identityType, identityValue), EmployeeData)
    End Function

    Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityValue As String) As EmployeeData
        Return DirectCast(FindByIdentityWithType(context, GetType(EmployeeData), identityValue), EmployeeData)
    End Function

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder()
        sb.AppendLine("Name: " & FirstName & " " & LastName & "<br/>")
        sb.AppendLine("EEID: " & EEID & "<br/>")
        sb.AppendLine("Email: " & Email & "<br/>")
        sb.AppendLine("Phone: " & PhoneNumber & "<br/>")
        'sb.AppendLine("Supervisor Email: " & SupervisorEmail & "<br/>")
        'sb.AppendLine("Username: " & Username & "<br/>")
        'sb.AppendLine("Dept: " & Dept & "<br/>")
        'sb.AppendLine("Division: " & Division & "<br/>")
        'sb.AppendLine("Job Title: " & JobTitle & "<br/>")
        'sb.AppendLine("Active Directory Manager: " & ADManager & "<br/>")
        sb.AppendLine()
        Return sb.ToString
    End Function
End Class

根据建议,我停止继承 UserPrincipal 和性别化方法来获取在传入时验证它们的属性。然后我能够使用数据契约属性和数据方法来序列化 class,没有任何问题.