3

我想我错过了一些非常基本的东西,但这是我的困境。在 VB.net 中,我创建了一个MembershipUser从 Web 服务继承并返回对象的类:

Public Class ModifiedUser
    Inherits MembershipUser

用户登录:

Private Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
    Dim ws As New MembersWS.Members
    Dim Member As New MembersWS.ModifiedUser
    ws.Credentials = System.Net.CredentialCache.DefaultCredentials()
    Member = ws.ValidateUser(Login1.UserName, Login1.Password)
End Sub

在 Web 服务中创建对象时,ModifiedUser包含所有属性MembershipUser以及新类的属性。

在我的示例中,该ValidateUser函数验证用户并添加其他属性:

Public Function ValidateUser(ByVal UserName As String, ByVal Password As String) As ModifiedUser
    Dim BaseUser As MembershipUser = Membership.Provider.GetUser(UserName, False)
    Dim Member As New ModifiedUser
    If Membership.Provider.ValidateUser(UserName, Password) = True Then 
        Member = New ModifiedUser(BaseUser.ProviderName, BaseUser.UserName, BaseUser.ProviderUserKey, BaseUser.Email, BaseUser.PasswordQuestion, BaseUser.Comment, BaseUser.IsApproved, BaseUser.IsLockedOut, BaseUser.CreationDate, BaseUser.LastLoginDate, BaseUser.LastActivityDate, BaseUser.LastPasswordChangedDate, BaseUser.LastLockoutDate, 0, "", "", "", "", True)
        ' ... (Set new property values)
    End if
    Return Member
End Function

Member返回到Login1_Authenticate时,只读属性将从对象中删除,同时它们包含在从 Web 服务返回的对象中。

reference.vbWeb 服务的文件中,自动生成的代码包含一个部分MembershipUser类,其中仅包含可更新的属性以及关联的 getter 和 setter:

Partial Public Class MembershipUser
    Private emailField As String
    Private commentField As String
    Private isApprovedField As Boolean
    Private lastLoginDateField As Date
    Private lastActivityDateField As Date

如果有人可以让我知道如何在返回的对象中包含只读属性,我将不胜感激。

4

1 回答 1

4

当 Web 服务返回包含只读属性的对象时,只读属性总是被忽略。这是因为在客户端,它无法通过设置只读属性的值来反序列化数据。解决此问题的一种方法是向您的属性添加一个不执行任何操作或引发异常的设置器。例如:

Public Property Id() As Integer
    Get
        Return nID
    End Get
    Set(ByVal Value As Integer)
        Throw New Exception("Cannot set read-only property 'Id'")
    End Set
End Property

有关更多想法,请参阅此问题:

是否可以在 SOAP Web 服务中创建只读元素?

于 2012-09-25T16:24:49.273 回答