How to do it: Developing an ASP.NET 2.0 Web Service

Code Samples
Table of Contents
Summary

Visual Studio solution-ASP.NET2.0 Web Service

Note

Implementing a basic ASP.NET 2.0 web service requires no configuration  on the file “Web.Config” file

Operations
Project "Services.Definition", File "Common.Data.cs"

This file contains the definition of the complex class types sent on the service request and expected as service response

How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;

namespace ASPNET
{
    [Serializable]
    [XmlRoot(Namespace="http://www.training.com", IsNullable=false,ElementName="Category")]
    public class Category
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }
    }
}
Project "Services.Definition", File "ITestService.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;

namespace ASPNET
{
    /// <summary>
    /// Summary description for TestService service contract
    /// </summary>
    [WebService]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public interface ITestService
    {

        [WebMethod]
        Category getCategory(Int32 pId);

        [WebMethod]
        int addCategory(Category pCategory);

        [WebMethod]
        [return: XmlRoot(ElementName = "Categories")]
        List<Category> getCategories();
    }
}
Project "Services.ASPNET20", File "TestServiceImpl.asmx.cs"

Web-service definition with hosting configuration

How to do it
<%@ WebService Language="C#" CodeBehind="TestServiceImpl.asmx.cs" Class="ASPNET.TestServiceImpl" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace ASPNET
{
    /// <summary>
    /// Summary description for TestService service contract implementation
    /// </summary>
    [WebService(Namespace = "http://www.training.com/")]
    public class TestServiceImpl : ITestService
    {

        #region "Operations/Services"
        public Category getCategory(Int32 pId)
        {
            //TODO: Put code here to retrieve category...
            return loadCategory(pId);
        }

        public int addCategory(Category pCategory)
        {
            //TODO: Put code here to save category defintion and return new identifier
            return 10;
        }

        public List<Category> getCategories()
        {
            //TODO: Put code here to retrieve category objects...
            return loadCategories();
        }
        #endregion

        #region "Private methods"
        private Category loadCategory(int pId)
        {
            return new Category { Id = 1, Name = string.Format("category #{0}", pId), Description = string.Format("test category #{0}", pId) };
        }

        private List<Category> loadCategories()
        {
            return new List<Category>() {
                            new Category { Id=1, Name="category #1", Description= "test category #1"},
                            new Category { Id=2, Name="category #2", Description= "test category #2"} };
        }
        #endregion
    }
}