I assume I am missing something very basic, but here is my plight. In VB.net, I have created a class that inherits MembershipUser and returns the object from a web service:
Public Class ModifiedUser
Inherits MembershipUser
The user logs in:
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
When the object is created in the web service, ModifiedUser contains all of the properties of MembershipUser as well as the properties of the new class.
In my example, the ValidateUser function validates the user and adds the additional properties:
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
When Member is returned to Login1_Authenticate, the readonly properties are dropped from the object while they were included in the object being returned from the web service.
In the reference.vb file for the web service, the auto-generated code includes a partial MembershipUser class including only the updateable properties with the associated getters and setters:
Partial Public Class MembershipUser
Private emailField As String
Private commentField As String
Private isApprovedField As Boolean
Private lastLoginDateField As Date
Private lastActivityDateField As Date
If anyone can let me know how to include the readonly properties in the returned object, I’d really appreciate it.
When a web service returns an object that contains read-only properties, the read-only properties are always ignored. That is because on the client-side, it would not be able to deserialize the data by setting the value of the read-only property. One way to fix this is to add a setter to your property which does nothing or throws an exception. For instance:
See this question for more ideas:
Is it possible to create read only elements in SOAP web services?