System.DirectoryServices.AccountManagement.UserPrincipalを継承するEmployeeDataという名前のクラスを作成しました。それは私が簡単にAD属性を使用してアクセスすることができます。しかし、私は今WCF Webサービスに移動しようとしています。 UserPrincipalがシリアル化可能ではないのに私のEmployeeクラスを直列化する方法はありますか? IE:特定のプロパティをシリアル化するか、別のアーキテクチャを作成するだけですか?私はこのプロセスに慣れていないので、これがひどい疑問であれば私を許してください。WCFアプリケーションのDirectoryServices.AccountManagement拡張クラスのシリアル化
WCFの場合、UserPrincipalクラスをシリアル化する必要がないように、特定のプロパティのみでDataMemberを使用できる方法はありますか?アドバイスパー
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
ワイヤで送信するデータを含むクラスを作成する方がよいでしょう。シリアル化は読み取り専用プロパティでは機能しません。ゲッターとセッターが必要です。さらに(これはIMOです)、いくつかのプロパティはかなりのロジックを含んでいるため、メソッドでなければなりません。 – Tim
ありがとうそれはまさに私がやったことです。また、プロパティでも良い呼び出し。私はちょうど継承をクラスから外して、代わりに必要な属性を取得するためのメソッドを使用しました。 – Eric