Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 9983 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > Remoting, Web Services, XML, XSLT Interview Questions > How to Create And Delploy A WebService U ...

How to Create And Delploy A WebService Using C#

Interview question and answer by: Charugoel | Posted on: 2/4/2009 | Category: Remoting, Web Services, XML, XSLT Interview questions | Views: 4547 |


Answer:

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
{
[WebMethod]
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
Files\Microsoft.NET\FrameworkSDK\Bin)
NameSp -> Name of the NameSpace which will be used in client code for deploying the webservice.
2.Compilation
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();
response.writeline(str);
}
</script>
Note
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 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Charugoel

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 8:55:02 AM