July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Combine Polymorphism and Web Services

  • January 14, 2005
  • By Paul Kimmel
  • Send Email »
  • More Articles »

If you were to write a WebMethod that returned an instance of a PersonCollection called GetPerson (see Listing 4), SPROXY would generate only one proxy class, Person, and the return type would become Person().

Listing 4: A WebMethod to Return an Instance of the PersonCollection

[WebMethod]
public PersonCollection GetPeople()
{
  return PersonCollection.CreateNew();
}

If Listing 4 is all you offer the consumers, their code will compile but they will get a runtime SoapException when the Person array returned from the Web method is initialized. Ultimately, because you defined other types of Person-derived classes, you need to make those types available to your Web service consumers too.

Now that you understand all of the elements of the problem, the fix is easy. Use the XmlIncludeAttribute defined in System.Xml.Serialization to specify the other types that the consumer will need proxy classes for as well. Use the XmlIncludeAttribute on the class header itself, initializing it with the Type object for each additional type for which proxies are needed. (By convention, drop the Attribute suffix when applying attributes.) Listing 5 shows the class header of the Web service (the class containing GetPeople) in bold font with an XmlIncludeAttribute for each of Person, Customer, and Employee.

Listing 5: Use XmlInclude to Ensure Child-types Are Defined for Web Service Consumers.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using BusinessCollections;
using System.Xml.Serialization;

namespace Service
{
  /// <summary>
  /// Summary description for Service1.
  /// </summary>
  [XmlInclude(typeof(Customer)),
  XmlInclude(typeof(Person)), XmlInclude(typeof(Employee))]
  public class Service1 : System.Web.Services.WebService
  {
    public Service1()
    {
      //CODEGEN: This call is required by the ASP.NET Web services
      //Designer
      InitializeComponent();
    }

    [Component Designer generated code]
    // WEB SERVICE EXAMPLE
    // The HelloWorld() example service returns the string
    // Hello World
    // To build, uncomment the following lines then save and build
    // the project
    // To test this web service, press F5

    [WebMethod()]
    
    public PersonCollection GetPeople()
    {
      return PersonCollection.CreateNew();
    }
 
  }
}

To test the Web service and Web method, create a console application. Select Project|Add Web Reference, and add the Web service above. Declare an instance of the Person() array and invoke the Web method.

What Have You Learned?

You have just examined polymorphism in the context of XML Web services. You learned that proxy types don't include methods, and you also learned that child types are not necessarily generated for Web service consumers, which can break inheritance relationships if you are not careful.

If you include child types specifically as arguments to Web Methods, SPROXY will create those types for consumers. If not, the XmlIncludeAttribute will instruct the XML Web services tools to generate additional types as specified in the XmlIncludeAttribute.

About the Author

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET and is a Microsoft MVP. Check out his book Visual Basic .NET Power Coding from Addison-Wesley and his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.

If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.

Copyright © 2004 by Paul Kimmel. All Rights Reserved.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel