Content:
1. Create simple web service
   - Deployment
2. Accessing the web service
   - From web browser
   - Web service definition - WSDL
   - Directly from URL link
   - From an HTML form
   - From .net client
3. Configuration
4. Using session


MSDN

Code example:Code example

1. Create simple web service

ASP.net web services use .asmx file extension. Requirements:
- web service class must inherit from System.Web.Services.WebService
- web service class must be attributed with [WebService]
- web service method must be attributed with [WebMethod]

The code below show a web service with one methid that adds 2 integers.

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

namespace MyServiceNs
{
  [WebService(Namespace="http://localhost/myservice")]
  public class WebServiceSimple : WebService 
  {
    [WebMethod(Description="This method sums 2 integers")]
    public int Add(int a, int b)
    {
      return a+b;
    }
  }
}

Deployment

In order to deploy the web service you need a IIS server installed. Then follow these steps:
- create WebServiceSimpleEmbedded.asmx file and copy paste the code sample above
- create c:\intepub\wwwroot\WebServiceSimple folder and copy the .asmx file under the folder
- Open IIS Manager
- expand Default web site node
- right click on WebServiceSimple and select Convert to Application. This will associate an Application pool with your web service.

Deployment options

Option 2: split the .asmx file above into header in .asmx and code App_Code\*.asmx.cs
Option 3: compile web service code into a .dll a put it under \bin folder

The code example demonstrates how to build web service and deploy the web service from command line without Visual Studio. It can come handy, becase Visual Studio 2012 and newer does not contain a template for creating ASP.net web services, as Microsoft recommends using WCF services.
Open 0_set_environment.cmd in notepad and check if the paths match to your machine. Update if needed.
start cmd.exe
Run 1_compile.cmd to create files needed to deploy the web service
Run 2_deploy.cmd to deploy the files to the web server. You need run this as administrator.

2. Accessing the web service

From web browser

Open your web browser and type: http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx. You will see the Add link. if you click on the link, you the web page will provide 2 entry fields where you can enter parameters and when click Invoke button, you will see result presented as XML.

Web service definition - WSDL

To see web service definition in SOAP, type http://localhost/WebServiceSimple/WebServiceSimpleEmbedded.asmx?WSDL

Directly from URL link

You can access a web method directly by following this pattern: http://myserver/MyService.asmx/MyMethod?MyParam=MyValue
http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx/Add?a=1&b=2
This will work only if you update your web.config as below

XML
<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
      </protocols>
    </webServices>
  </system.web>
</configuration>

From an HTML form

You can also create a HTML form and let user enter data

HTML
<FORM action="http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx/Add" method="post"> 
  A <input type="text" name="a"> +
  B <input type="text" name="b">
    <input type="submit" value="Calculate">
</FORM>

From .net client

In order to call web service from a .net client you need to create a proxy. You can do it from command line

CMD
"c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\WSDL.exe" 
   http://localhost/WebserviceSimple/WebServiceSimpleEmbedded.asmx?WSDL 
   /out:WebServiceSimpleProxy.cs 
another option is to create web service proxy from Visual Studio by right clicking on the References folder and selecting Add service reference You need to include WebServiceSimpleProxy.cs that defines "WebServiceSimple" client class in your project and compile the code below

C#
using System;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            WebServiceSimple client = new WebServiceSimple();  // class defined in proxy 
            int result = client.Add(1, 2);
            client.Close();
        }
    }
}

3. Configuration

Sometimes you need to configure default web service behavior, e.g. when you have long running requests or you need transfer large amount of data. Here is list of all parameters. Example of common configuration below.

XML
<configuration>
<system.web>
     <httpRuntime
      executionTimeout="3600"
      maxRequestLength="2097151"
      shutdownTimeout="3500"/>
     
     <compilation debug="false" />
     
     <authentication mode="None" />
     <identity impersonate="false"/>   

  </system.web>
</configuration>

4. Using session

If you need to keep session specific data, you can follow the example below

C#
public class SampleService : System.Web.Services.WebService
{
    [WebMethod(Description = "This method sums 2 integers", EnableSession=true)]
    public int Add(int a, int b)
    {
        if (Session["AccessCounter"] == null) { Session["AccessCounter"] = 1; }   // using Session requires "EnableSession=true" attribute
        else { Session["AccessCounter"] = ((Int32)Session["AccessCounter"]) + 1; }

        return a + b;
    }

    [WebMethod(EnableSession=true)]
    public int AccessCount()
    {
        if (Session["AccessCounter"] == null) { return 0; }
        else { return (Int32)Session["AccessCounter"]; }
    }
}