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
1. Create simple web service
- 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
- 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 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
Web service definition - WSDL
Directly from URL link
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
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
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.csanother 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
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
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"]; } } }