Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Use Inheritance in a Web Service 

The following code example demonstrates how to use inheritance to create a Web service that performs math calculations. This example illustrates one of the guidelines explained in the topic, Design Guidelines for XML Web Services Created Using ASP.NET.

Example

<%@ WebService Language="C#" Class="Add" %>
using System;
using System.Web.Services;
abstract public class MathService : WebService 
{
   [WebMethod]
   abstract public float CalculateTotal(float a, float b);
}
public class Add : MathService 
{
   [WebMethod]
   override public float CalculateTotal(float a, float b)
   {
       return a + b;
   }
}
public class Subtract : MathService 
{
   [WebMethod]
   override public float CalculateTotal(float a, float b)
   {
       return a - b;
   }
}
public class Multiply : MathService 
{
   [WebMethod]
   override public float CalculateTotal(float a, float b)
   {
       return a * b;
   }
}
public class Divide : MathService 
{
   [WebMethod]
   override public float CalculateTotal(float a, float b)
   {
       if (b==0) 
          return -1;
       else
          return a / b;
   }
}

See Also

Community Additions

ADD
Show:
© 2015 Microsoft