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