Microsoft KB Archive/313584

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

Article ID: 313584

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft Web Services Enhancements for Microsoft .NET 2.0

-



This article was previously published under Q313584



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(&quot;Cannot set read-only property 'Id'&quot;) End Set End Property



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create a new Microsoft Visual Basic ASP.NET Web service project.  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

 _ Public Class TestKb

Private m_sName As String Private m_nID As Integer

Public Sub New m_sName = &quot;Hello world&quot; 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  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. 

