You are on page 1of 5

Assignment 4

Web Services

Create Web Service

Table: TemperatureTable

Web Service: Temperature.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/Temperature.cs"


Class="Temperature" %>

Web Service Code File: Temperature.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Temperature
/// </summary>

[WebService(Namespace = "http://marwadieducation.edu.in/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Temperature : System.Web.Services.WebService {

public Temperature () { }

[WebMethod]
public int CitytoTemperature(string City) {
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Program\App_Data\Database.mdf;Integrated
Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Temperature FROM
TemperatureTable WHERE City=@City", con);
cmd.Parameters.AddWithValue("@City", City);
int temp = (int)cmd.ExecuteScalar();
con.Close();
return temp;
}
}

Web Service Testing

Web Service WSDL

Nishith Bhimani - 151163107005


Web Service SOAP

<?xml version="1.0" encoding="utf-8"?>


<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CitytoTemperature xmlns="http://marwadieducation.edu.in/">
<CityName>string</CityName>
</CitytoTemperature>
</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>


<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CitytoTemperatureResponse xmlns="http://marwadieducation.edu.in/">
<CitytoTemperatureResult>int</CitytoTemperatureResult>
</CitytoTemperatureResponse>
</soap12:Body>
</soap12:Envelope>

Nishith Bhimani - 151163107005


Consume Web Service:

Add Service Reference

Web Page: 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>
<asp:TextBox ID="CityTextBox" runat="server"></asp:TextBox>
<br /><br />

Nishith Bhimani - 151163107005


<asp:Button ID="SearchButton" runat="server" onclick="SearchButton_Click"
Text="Button" />
<br /><br />
<asp:Label ID="TempLabel" runat="server" Text=""></asp:Label>

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

Web Page Code behind: Default.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page


{
protected void SearchButton_Click(object sender, EventArgs e)
{
MEF.TemperatureSoapClient t1 = new MEF.TemperatureSoapClient();
TempLabel.Text = t1.CitytoTemperature
(CityTextBox.Text).ToString();
}
}

Nishith Bhimani - 151163107005