0
我有一個類,我創建了名爲EmployeeData的類,它繼承自System.DirectoryServices.AccountManagement.UserPrincipal。它允許我使用輕鬆訪問AD屬性。但我現在想把它移到WCF的Web服務上。即使UserPrincipal不可序列化,是否有序列化Employee類的方法? IE:只是序列化某些屬性或創建不同的體系結構?我對這個過程很陌生,所以如果這是一個可怕的問題,請原諒我。WCF App中的DirectoryServices.AccountManagement擴展類序列化
對於WCF,有沒有一種方法可以在某些屬性上使用DataMember使其成爲UserPrincipal類不需要可序列化?
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
你可能最好寫一個類來包含通過電線發送的數據。請注意,序列化對於只讀屬性不起作用。你需要一個getter和一個setter。另外(這是一個IMO)你的一些屬性應該是方法,因爲它們包含相當多的邏輯。 – Tim
謝謝這正是我所做的。也很好的電話的屬性以及。我最終只是從我的類中取出繼承,並使用方法來獲取我需要的屬性。 – Eric