Creating and calling web service with a namespace

Now add a web services

Now change the name space to the actual web address.

AddupService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for AddupService
/// </summary>
[WebService(Namespace = "http://manas6/aspnet.35/WebServices2/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AddupService : System.Web.Services.WebService {
public AddupService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public int AddNumber(int n1, int n2)
{
return (n1 + n2);
}
}

 

Now if you run , the only name of the services will show up (unlike, when we used temp.uri ; Link) ,  and would be ready to take user's input. 

Note the difference between two

http://manas6/aspnet.35/WebServices1/Service.asmx

 

Comparing specific namespace with default namespace created by WSDL compiler

targetNamespace="http://manas6/aspnet.35/WebServices2/"

targetNamespace="http://tempuri.org/"
  <?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://manas6/aspnet.35/WebServices2/" 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://manas6/aspnet.35/WebServices2/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://manas6/aspnet.35/WebServices2/">
- <s:element name="AddNumber">
- <s:complexType>
- <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="n1" type="s:int" />
  <s:element minOccurs="1" maxOccurs="1" name="n2" type="s:int" />
  </s:sequence>
  </s:complexType>
  </s:element>
- <s:element name="AddNumberResponse">
- <s:complexType>
- <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="AddNumberResult" type="s:int" />
  </s:sequence>
  </s:complexType>
  </s:element>
  </s:schema>
  </wsdl:types>
- <wsdl:message name="AddNumberSoapIn">
  <wsdl:part name="parameters" element="tns:AddNumber" />
  </wsdl:message>
- <wsdl:message name="AddNumberSoapOut">
  <wsdl:part name="parameters" element="tns:AddNumberResponse" />
  </wsdl:message>
- <wsdl:portType name="AddupServiceSoap">
- <wsdl:operation name="AddNumber">
  <wsdl:input message="tns:AddNumberSoapIn" />
  <wsdl:output message="tns:AddNumberSoapOut" />
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="AddupServiceSoap" type="tns:AddupServiceSoap">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="AddNumber">
  <soap:operation soapAction="http://manas6/aspnet.35/WebServices2/AddNumber" 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="AddupServiceSoap12" type="tns:AddupServiceSoap">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="AddNumber">
  <soap12:operation soapAction="http://manas6/aspnet.35/WebServices2/AddNumber" 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="AddupService">
- <wsdl:port name="AddupServiceSoap" binding="tns:AddupServiceSoap">
  <soap:address location="http://manas6/aspnet.35/WebServices2/AddupService.asmx" />
  </wsdl:port>
- <wsdl:port name="AddupServiceSoap12" binding="tns:AddupServiceSoap12">
  <soap12:address location="http://manas6/aspnet.35/WebServices2/AddupService.asmx" />
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

  <?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://tempuri.org/" 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://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
- <s:element name="Addup">
- <s:complexType>
- <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="n1" type="s:int" />
  <s:element minOccurs="1" maxOccurs="1" name="n2" type="s:int" />
  </s:sequence>
  </s:complexType>
  </s:element>
- <s:element name="AddupResponse">
- <s:complexType>
- <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="AddupResult" type="s:int" />
  </s:sequence>
  </s:complexType>
  </s:element>
  </s:schema>
  </wsdl:types>
- <wsdl:message name="AddupSoapIn">
  <wsdl:part name="parameters" element="tns:Addup" />
  </wsdl:message>
- <wsdl:message name="AddupSoapOut">
  <wsdl:part name="parameters" element="tns:AddupResponse" />
  </wsdl:message>
- <wsdl:portType name="ServiceSoap">
- <wsdl:operation name="Addup">
  <wsdl:input message="tns:AddupSoapIn" />
  <wsdl:output message="tns:AddupSoapOut" />
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="Addup">
  <soap:operation soapAction="http://tempuri.org/Addup" 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="ServiceSoap12" type="tns:ServiceSoap">
  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="Addup">
  <soap12:operation soapAction="http://tempuri.org/Addup" 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="Service">
- <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
  <soap:address location="http://manas6/aspnet.35/WebServices1/Service.asmx" />
  </wsdl:port>
- <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
  <soap12:address location="http://manas6/aspnet.35/WebServices1/Service.asmx" />
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

 

Let us test the function

now add some numbers and click on invoke

Step : Calling this service from another location. Create a new website elsewhere in the net work or as a new application

http://manas6/aspnet.35/WebServices2Client/Default.aspx

Add a web reference ( using http://manas6/aspnet.35/WebServices2/AddupService.asmx?wsdl )

Watch the service is found and now add to your solution explorer, clicking add-reference

Now the the class and web method in the web service would be accessible to the client. Few years ago, webservice.htc allowed the developer to use JavaScript from an web page to consume the web services; but I found that it had been archived. 

Code in Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="width: 520px">
Enter two numbers<br />
<asp:TextBox ID="T1" runat="server" Height="20px" Width="74px"></asp:TextBox>&nbsp &nbsp
<asp:TextBox ID="T2" runat="server" Height="20px" Width="96px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Height="28px" Text="Calculate "
Width="133px" onclick="Button1_Click" /><br />Results:
<asp:Label ID="L1" runat="server" Text=""></asp:Label>

</div>
</form>
</body>
</html>
 

Code in Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
int n1 = Convert.ToInt32(T1.Text);
int n2 = Convert.ToInt32(T2.Text);
manas6.AddupService addNum = new manas6.AddupService();
L1.Text =" The total is " + addNum.AddNumber(n1, n2).ToString();
}
}
 

Runtime analysis

http://manas6/aspnet.35/WebServices2Client/Default.aspx