ASP.NET

WebService without Visual Studio

Introduction#

A very basic ASP.Net example of the bare minimum of code to create a WebService.

Remarks#

In a separate StackOverflow Documentation post, we’ll look at consuming this Calculator WebService.

Calculator WebService

<%@ WebService Language="C#" Class="Util" %>
using System;
using System.Web.Services;

public class Util: WebService
{
    [WebMethod]
    public int CalculatorAdd(int operandA, int operandB)
    {
        return operandA + operandB;
    }

    [WebMethod]
    public int CalculatorSubtract(int operandA, int operandB)
    {
        return operandA - operandB;
    }

    [WebMethod]
    public long CalculatorMultiply(int operandA, int operandB)
    {
        return operandA * operandB;
    }
    
    [WebMethod]
    public long CalculatorDivide(int operandNumerator, int operandDenominator)
    {
        if (operandDenominator == 0)
            return System.Int64.MaxValue;    // Should really do better error handling overall & return an error
        else
            return operandNumerator / operandDenominator;
    }
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow