Help and Support

Article ID: 313584 - Last Review: February 23, 2007 - Revision: 6.2

PRB: Read-Only Properties Cannot Be Exposed by XML Web Services

This article was previously published under Q313584

On This Page

Expand all | Collapse all

SYMPTOMS

Note The following .NET Framework Class Library namespaces are referenced in this article:

System.Web.Services

If a Web service contains a Web method that either accepts a parameter or returns a value that is an object reference, and the class definition of the object contains a read-only property, the read-only property is not available when you build a proxy assembly for the Web service.

Note The following .NET Framework Class Library namespace is referenced in this article:

System

CAUSE

When an object is passed (that is, marshalled) to or from a Web service, it must be serialized into an XML stream and then deserialized back into an object. The XML Serializer cannot deserialize the XML back into an object because it cannot load the read-only properties. Consequently, the read-only properties are not exposed through the Web Services Description Language (WSDL). Because the Web service proxy is generated from the WSDL, the proxy also excludes any read-only properties.

RESOLUTION

To work around this problem, add a SET procedure for the property. You can either leave the procedure empty, so that that the procedure has no effect, or raise a custom exception to inform clients that the property is read-only, as follows:
    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
				

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Microsoft Visual Basic ASP.NET Web service project.
  2. Paste the following code in the Service1.asmx.vb module:
    'This Web service has one Web method.
    'This Web method returns a Testkb object.
    'The Testkb class has 2 properties:
    '    Name is a read/write property
    '    Id is a read-only property (this property is not available in the WSDL and Web proxy).
    'Adjust the namespace if necessary.
    
    Imports System.Web.Services
    
    <WebService(Namespace:="http://localhost/XmlWebServices/")> _
    Public Class TestKb
    
        Private m_sName As String
        Private m_nID As Integer
    
        Public Sub New()
            m_sName = "Hello world"
            m_nID = 10
        End Sub
    
        Public Property Name() As String
            Get
                Return m_sName
            End Get
            Set(ByVal Value As String)
                m_sName = Value
            End Set
        End Property
    
        Public ReadOnly Property Id() As Integer
            Get
                Return m_nID
            End Get
        End Property
    End Class
    
    Public Class Service1
        Inherits System.Web.Services.WebService
        <WebMethod()> Public Function Test() As TestKb
            Return New TestKb()
        End Function
    End Class
    					
    When you build the proxy assembly for this Web service, the proxy exposes only the Name property for the Testkb class. The proxy does not expose the ID property because ID property is read-only.

REFERENCES

301273  (http://support.microsoft.com/kb/301273/EN-US/ ) HOW TO: Write a Simple Web Service by Using Visual Basic .NET

APPLIES TO
  • Microsoft Web Services Enhancements for Microsoft .NET 1.1
  • Microsoft Web Services (included with the .NET Framework) 1.0
Keywords: 
kbprb kbwebservices KB313584
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Article Translations