Create simple web service as .asmx

<%@ 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;
  }
 }
}


More info and samples on: www.devarchweb.net

How to deploy web service (3 options) - 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

More info and samples on: www.devarchweb.net

How to get web service definition (WSDL) To see web service definition in SOAP, type http://localhost/WebServiceSimple/WebServiceSimpleEmbedded.asmx?WSDL

More info and samples on: www.devarchweb.net

What needs to be done in order to access a web method directly from URL

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


More info and samples on: www.devarchweb.net

How to access method from HTML form

<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>


More info and samples on: www.devarchweb.net

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

"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

More info and samples on: www.devarchweb.net

Implement .net client in C# that calls a method You need to include WebServiceSimpleProxy.cs that defines "WebServiceSimple" client class in your project and compile the code below

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();
    }
  }
}


More info and samples on: www.devarchweb.net

How to use Session

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"]; }
  }
}





More info and samples on: www.devarchweb.net