Creating the web service

The web service that supplies the data for the dashboard is hosted on the Internet Information Server (IIS) on a remote server. This means that it will be developed as an ASP.NET Web Service Application project. As ASP.NET web services are not the focus of this book, you will not need to retrieve the data from an actual database. Rather, you can write some code to return hardcoded dummy data.

Create a new ASP.NET Web Service Application project. Rename the main service from Service1.asmx to DataService.asmx. Take note that you might also need to reflect this new name in the Class attribute of the web service declaration in the DataService.asmx mark-up file. You can view the mark-up file by right-clicking the DataService.asmx file and choosing View Markup from the pop-up menu.

The DataService.asmx file will only contain one web method, named GetChartData, which takes in two arguments—the year and month of the requested sales data. This method will return the data for all three charts in a single XML block. The code to generate this XML follows:

[WebMethod()]public string GetChartData(int Year, int Month)
{
XmlDocument _xml = new XmlDocument();
XmlElement _xmlRoot = _xml.CreateElement("ChartData");
XmlElement _xmlDay;
XmlElement _xmlRoadShowRoot;
XmlElement _xmlTotalSalesRoot;
_xml.AppendChild(_xmlRoot);
VBMath.Randomize();
//======================================================
//Generate road show sales data.
//You will use the Rnd() function to generate random
//sales figures for each day of the month.
//======================================================
_xmlRoadShowRoot = _xml.CreateElement("RoadShowSales");
_xmlRoot.AppendChild(_xmlRoadShowRoot);
for (var _counter = 1; _counter <=
DateTime.DaysInMonth(Year, Month); _counter++)
{
_xmlDay = _xml.CreateElement("Day" + _counter);
_xmlDay.SetAttribute("Value",
System.Convert.ToString(Math.Round(VBMath.Rnd() * 50000)));
_xmlRoadShowRoot.AppendChild(_xmlDay);
}
//======================================================
//Generate total sales for the month
//There are two values that need to be returned:
//a) Actual sales achieved for the month
//b) The sales target for the month
//======================================================
_xmlTotalSalesRoot = _xml.CreateElement("TotalSales");
_xmlRoot.AppendChild(_xmlTotalSalesRoot);
XmlElement _xmlSalesAmount = _xml.CreateElement("TotalSalesAmount");
_xmlSalesAmount.SetAttribute("Value", "800000");
_xmlSalesAmount.SetAttribute("MonthlySalesTarget", "1000000");
_xmlTotalSalesRoot.AppendChild(_xmlSalesAmount);
//======================================================
//Generate total sales for the last three months
//======================================================
XmlElement _xmlLastThreeMonthSalesRoot;
_xmlLastThreeMonthSalesRoot =
_xml.CreateElement("LastThreeMonthSales");
_xmlRoot.AppendChild(_xmlLastThreeMonthSalesRoot);
XmlElement _xmlMonth1 = _xml.CreateElement("Month1");
_xmlMonth1.SetAttribute("Value", "1840000");
_xmlLastThreeMonthSalesRoot.AppendChild(_xmlMonth1);
XmlElement _xmlMonth2 = _xml.CreateElement("Month2");
_xmlMonth2.SetAttribute("Value", "1200000");
_xmlLastThreeMonthSalesRoot.AppendChild(_xmlMonth2);
XmlElement _xmlMonth3 = _xml.CreateElement("Month3");
_xmlMonth3.SetAttribute("Value", "800000");
_xmlLastThreeMonthSalesRoot.AppendChild(_xmlMonth3);
//Write the XML block into a string
StringWriter _stringWriter = new StringWriter();
XmlTextWriter _xmlWriter = new
XmlTextWriter(_stringWriter);
_xml.WriteTo(_xmlWriter);
return _stringWriter.ToString();
}

After creating this web service, you will need to host it in IIS so that it can be consumed remotely. Add a new application under the Default Web Site via IIS Manager.

Creating the web service

To test if your web service was set up successfully, open your browser, and navigate to the following address:

http://localhost/DashboardService/DataService.asmx

You should be able to see the following page. Notice that your web method is displayed on this page. You can even try invoking this web method by clicking on the GetChartData link, filling in the year and month values and pressing the Invoke button (in the page that follows).

Creating the web service

The XML data that is retrieved from this web method will have the following structure:

Creating the web service
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.2.23