PRB: Read-Only and Write-Only Class Properties Not Accessible from a Web Service (317270)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft .NET Framework 1.1
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition
  • Microsoft .NET Framework 1.0

This article was previously published under Q317270

SYMPTOMS

When you try to access read-only and write-only properties of a class that you obtain from a Web service, you cannot view the properties.

CAUSE

XML serialization and ASP.NET Web services do not support read-only and write-only properties. Information about these properties is not published in Web Services Description Language (WSDL) documentation.

WORKAROUND

To access the properties, use the Web methods in the WebService class. To do this, follow these steps:
  1. In the Service1.asmx.vb file, paste the following Web methods after the #Region " Web Services Designer Generated Code" block:
    <WebMethod()> Public Function GetValue1() As Integer
            Dim c1 As New Class1()
            GetValue1 = c1.Value1
    End Function
    
    <WebMethod()> Public Function SetValue2(ByVal x As Integer) As Boolean
            Dim c2 As New Class1()
            c2.Value2 = x
            Return True
    End Function
    					
  2. In the WebForm1.aspx.vb file, instead of code that tries to directly access the properties, paste the following code in the Page_Load method:
     data="Dim i as Integer
    i = myVar2.GetValue1
    myVar2.SetValue2(5)"?>Dim myvar As New localhost.Service1
    Dim myInt As Int32
    myInt = myvar.GetValue1
    myvar.SetValue2(100)
    					

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Use Visual Basic to create a new Web service project.
  2. In Service.asmx.vb, paste the following code in the Service1 class:
    <WebMethod()> Public Function GetObject() As Class1
            GetObject = New Class1()
           End Function
  3. Paste the following class after the Service1 class:
     
    Public Class Class1
        Private mVal1 As Integer
        Private mVal2 As Integer
    
        Public ReadOnly Property Value1() As Integer
            Get
                Value1 = mVal1
            End Get
        End Property
    
        Public WriteOnly Property Value2() As Integer
            Set(ByVal Value As Integer)
                mVal2 = Value
            End Set
        End Property
    End Class
    					
  4. Build the Web service.
  5. Create a new Web application.
  6. Add a Web reference to the Service1.asmx Web service. The default location for this file is the http://localhost/WebService1 folder.
  7. Declare an instance of localhost.Class1, and then attempt to access the Value1 and Value2 properties in the Page_Load method of Webform1.aspx, as follows:
         Dim myvar as localhost.Class1
         Dim myInt as Int32
    
         myInt = myvar.Value1
         myvar.Value2 = 100
    					

Modification Type:MajorLast Reviewed:2/11/2004
Keywords:kbprb KB317270 kbAudDeveloper