You are on page 1of 8

Web Services

Building Web service


1. Create a ASP.NET web service project

The default web service file created is


Service.asmx
And the code-behind file
Service.cs

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed


components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}

Delete Service.asmx and


To create new webservice file

Changing the existing file


using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace =
"http://www.ws.com/hello",Name="HelloGreetings")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Hello : System.Web.Services.WebService {

public Hello () {

//Uncomment the following line if using designed components


//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string name) {
return "Hello " + name;
}
}
Testing Web Service

Running it on the browser (say yes to creation of web.config file)

Service Description: WSDL file


<?xml version="1.0" encoding="utf-8" ?>
- <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://www.ws.com/hello"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="http://www.ws.com/hello"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified"
targetNamespace="http://www.ws.com/hello">
- <s:element name="HelloWorld">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
- <s:element name="HelloWorldResponse">
- <s:complexType>
- <s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult"
type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
- <wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
- <wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
</wsdl:message>
- <wsdl:portType name="HelloGreetingsSoap">
- <wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloGreetingsSoap" type="tns:HelloGreetingsSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://www.ws.com/hello/HelloWorld"
style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="HelloGreetingsSoap12" type="tns:HelloGreetingsSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://www.ws.com/hello/HelloWorld"
style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloGreetings">
- <wsdl:port name="HelloGreetingsSoap" binding="tns:HelloGreetingsSoap">
<soap:address location="http://localhost:49181/WebServices/Hello.asmx"
/>
</wsdl:port>
- <wsdl:port name="HelloGreetingsSoap12"
binding="tns:HelloGreetingsSoap12">
<soap12:address location="http://localhost:49181/WebServices/Hello.asmx"
/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
On clicking a test page for HelloWorld appears
Creating Web Service Client

For any type of client- either a web client or a console based application
invoke web service as shown below

Console application
Change the Web reference to some other meaningful name other than the
local host

Console application
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HelloDotNetWS.HelloGreetings h = new
HelloDotNetWS.HelloGreetings();
Console.WriteLine(h.SayHello("Narada"));
Console.Read();
}
}
}

For web client on adding webservice reference


in web.config

<add key="HelloDotNetWS.Hello"
value="http://localhost:49237/WebService/Hello.asmx"/>

is automatically added

in the web form add a textbox, a button and a label

protected void Button1_Click(object sender, EventArgs e)


{
HelloDotNetWS.HelloGreetings h = new
HelloDotNetWS.HelloGreetings();

Label1.Text = h.SayHello(TextBox1.Text);

}
Similarly try web services from Jboss application also

using System.Text;

namespace FromJbossWS
{
class Program
{
static void Main(string[] args)
{
JbossWS.HelloService s = new JbossWS.HelloService();
Console.WriteLine(s.hello("Gayatri"));
Console.Read();
}
}
}

You might also like