How to Create And Delploy A WebService Using C#

 Posted by Charugoel on 2/4/2009 | Category: Web Services, Remoting Interview questions | Views: 7660

First, let's start off by creating a very simple webservice.
Creating A Webservice
1.Create a folder named Webservice under wwwroot
2. Create a File
<%@ WebService Language="c#" Class="AddNumbers"%>

using System;
using System.Web.Services;
public class AddNumbers : WebService
public int Add(int a, int b){
int sum;
sum = a + b;
return sum;
3.Save this file as AddService.asmx [asmx-> file extension]
4.Now the webservice is created and ready for the clients to use it.
5. Now we can call this webservice using
http://ip address/Webservice/Addservice.asmx/Add?a=10&b=5
This will return the result in XML format
Deploying the Webservice in the Client Machine
1.At the command prompt:
WSDL http://ip address ofthe site/WebService/MathService.asmx /n:NameSp /out:FileName.cs]
-This will create a file called FileNmame.cs .
WSDL -> WebServices Description Language (This is an application available at C:\Program
NameSp -> Name of the NameSpace which will be used in client code for deploying the webservice.
CSC /t:library /r:system.web.dll /r:system.xml.dll CreatedFile.cs
This will create a dll with the name of the public class of the asmx file.( In our case, it is "AddNumbers.dll" )
CSC is an application available at C:\WINNT\Microsoft.NET\Framework\v1.0.2914
3.Put the dll file inside WWWRooT\BIN [Create a BIN Folder in WWWRoot]
Making use of WebService in client asp/aspx page
<%@ import Namespace = "NameSp" %>
<script language = "c#" runat = "server">
public void Page_Load(object o, EventArgs e){
int x = 10;
int y = 5;
int sum;
//Instantiating the public class of the webservice
AddNumbers AN = new AddNumbers();
sum = AN.Add(x,y);
string str = sum.ToString();
It is advisable to
1. Copy the .asmx file to the folder containing WSDL aplication (C:\Program Files\Microsoft.NET\FrameworkSDK\Bin) before creating cs file.
2. Copy the created .cs file to the folder containing CSC application

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response