Sample WCF Service Application in .NET


WCF is a Windows Communication Foundation is the latest service execution environment from Microsoft, introduced in .NET 3.0. It is a unified programing model which supports features like XML Web Services, .NET Remoting, MSMQ, and COM+ .

WCF is a framework for building service oriented applications, you can send the send the data from one service to anpther Asynchronously.

Creating sample WCF service in C#:


To day we will create a sample WCF Service Application and consume it from the WebApplication. Click on Microsoft Visual Studio 2010 and create a new WCF Service Application template, name the service and click on Ok. The visaul studio will be loaded and Service.svc,Iservice1.cs and web.config files will be created by default.
First rename the Service.svc to MyWeatherReport.svc and Iservice1.cs as IWeatherReport.cs. Now please add the MyCity.xml file to solution.

<?xml version="1.0" encoding="utf-8" ?>
<Cities>
<City>
<Name>Dilsukh Nagar</Name>
<State>Hyderabad</State>
<MaxTemp>40</MaxTemp>
<MinTemp>32</MinTemp>
</City>
<City>
<Name>Nakrekal</Name>
<State>Nalgonda</State>
<MaxTemp>43</MaxTemp>
<MinTemp>34</MinTemp>
</City>
<City>
<Name>Bangalore</Name>
<State>Karnataka</State>
<MaxTemp>44</MaxTemp>
<MinTemp>31</MinTemp>
</City>
<City>
<Name>Chennai</Name>
<State>Tamilnadu</State>
<MaxTemp>35</MaxTemp>
<MinTemp>28.5</MinTemp>
</City>
<City>
<Name>Bhopal</Name>
<State>Madhya Pradesh</State>
<MaxTemp>53</MaxTemp>
<MinTemp>40</MinTemp>
</City>
<City>
<Name>Palair</Name>
<State>Kammam Pradesh</State>
<MaxTemp>42.4</MaxTemp>
<MinTemp>30.4</MinTemp>
</City>

</Cities>


Write the following code in MyWeatherReport.svc file.

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

namespace WeatherForecast
{
public class WeatherReport : IWeatherReport
{
public WeatherData GetCityTemprature(string city)
{
XElement xe = XElement.Load(@".\WeatherForecast\City.xml");
var query = from xElem in xe.Elements("City")
where xElem.Element("Name").Value == city
select new WeatherData
{
City = (string)xElem.Element("Name"),
State = (string)xElem.Element("State"),
MaxTemprature = Convert.ToDouble(xElem.Element("MaxTemp").Value),
MinTemprature = Convert.ToDouble(xElem.Element("MinTemp").Value),
};

WeatherData wd = query.First();
return wd;
}
}
}




Now, build the solution and run. Next step is needs to consume the service in Asp.Net. So create one samle website with the name LocalWeatherReport. Design the aspx page like as below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="IndianWeatherReport._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>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 158px;
}
.style3
{
width: 158px;
height: 130px;
text-align: center;
}
.style4
{
height: 130px;
}
.style5
{
color: #FFFFFF;
font-weight: bold;
}
.style6
{
width: 103px;
}
.style7
{
width: 113px;
}
</style>
</head>
<body style="font-family: Tahoma; font-size: small">
<form id="form1" runat="server">
<table class="style1">
<tr>
<td>
<table class="style1">
<tr>
<td class="style6">
Select City Name</td>
<td class="style7">
<asp:DropDownList ID="cmbCity" runat="server">
<asp:ListItem>Nalgonda</asp:ListItem>
<asp:ListItem>Kammam</asp:ListItem>
<asp:ListItem>Bhopal</asp:ListItem>
<asp:ListItem>chennai</asp:ListItem>
<asp:ListItem>Karnataka</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>

<table class="style1"
style="background-image: url('background.JPG'); background-repeat: no-repeat; height: 239px;">
<tr>
<td class="style3">
<img alt="" src="weather_MyPic.gif"
style="width: 112px; height: 103px; border-left-style: inset; border-left-width: thin; border-right-style: outset; border-right-width: thin; border-top-style: inset; border-top-width: thin; border-bottom-style: outset; border-bottom-width: thin;" /></td>
<td class="style4" style="vertical-align: top;">
<table class="style1" style="vertical-align: top; height: 153px;">
<tr>
<td>
<asp:Label ID="lblState" runat="server" style="font-weight: 700" Text="State :"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblCity" runat="server" style="font-weight: 700" Text="City : "></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblDesc" runat="server" style="font-weight: 700"
Text="Description : "></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="style2">
<span class="style5">Maximum Temprature</span></td>
<td>

<asp:Label ID="lblMax" runat="server" Text="0"></asp:Label>
</td>
</tr>
<tr>
<td class="style2">
<span class="style5">Minimum Temprature</span></td>
<td>

<asp:Label ID="lblMin" runat="server" Text="0"></asp:Label>
</td>
</tr>
</table>

</td>
</tr>
</table>
</form>
</body>
</html>


Now write the below code in .cs file and compile it.

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

namespace LocalWeatherReport
{
public partial class _Default : System.Web.UI.Page
{
protected void btnShow_Click(object sender, EventArgs e)
{
WeatherReportClient rptClient = new WeatherReportClient();
WeatherData wd = rptClient. GetMyCityTemprature(cmbCity.Text);

lblState.Text = "State : " + wd.State;
lblCity.Text = "City : " + wd.City;
lblMax.Text = wd.MaxTemprature.ToString() + " C";
lblMin.Text = wd.MinTemprature.ToString() + " C";

}
}
}



Comments

Author: Siva Prasad06 Jul 2012 Member Level: Gold   Points : 0

Can you tell me where is interface called IWeatherReport 's code.

And in method,
public WeatherData GetCityTemprature(string city), Where is WeatherData class's code?

Author: Siva Prasad06 Jul 2012 Member Level: Gold   Points : 0

You missed using System.Xml.Linq; namespace for XElement in method implementation.

Author: Siva Prasad06 Jul 2012 Member Level: Gold   Points : 2

following code is missing


[DataContract]
public class WeatherData
{
private string city;
private string state;
private double maxTemprature;
private double minTemprature;

[DataMember]
public string City { get; set; }


[DataMember]
public string State { get; set; }

[DataMember]
public double MaxTemprature { get; set; }

[DataMember]
public double MinTemprature { get; set; }
}

Author: Siva Prasad06 Jul 2012 Member Level: Gold   Points : 0

GetCityTemprature is the method should be called in client , not GetMyCityTemprature() method.

Author: Siva Prasad06 Jul 2012 Member Level: Gold   Points : 0

Following code is missing.


[ServiceContract]
public interface IWeatherReport
{
[OperationContract]
WeatherData GetCityTemprature(string city);
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: