soap_csharp_labstats_5.

Transcription

soap_csharp_labstats_5.
Computer Lab Solutions
255 B St, Suite 207
Idaho Falls, ID 83402
Tel: 801.939.3312
Fax: 801.823.2210
SOAP and LabStats 5.0
What is SOAP?
SOAP is a technology that allows programs to communicate with each other using
XML based languages over the HTTP protocol. SOAP allows programs written in
many different languages to have a common form of communication.
SOAP and LabStats 5.0
LabStats 5.0 introduces a SOAP based API. You can use this API to access real-time
information about your groups and machines.
SOAP overview
The following web page is a great resource for learning all about what SOAP has to
offer:
http://www.w3schools.com/xml/xml_soap.asp
SOAP tutorial using C#
This tutorial will show you how you can use an ASP.net web site to pull your own
custom stats. This tutorial doesn’t assume that you are familiar with ASP.net. It does
assume that you have a general understanding of HTML and Object Oriented
Programming.
Step 1: Download and Install Visual Studio Express
You will need Visual Web Developer Express 2010. You can download it at
http://www.microsoft.com/Express/
The installation process is fairly straight forward.
Step 2: Create a new C# web service application
1.
2.
3.
4.
5.
Open Visual Web Developer Express
Click File -> New Project
Click to expand the section labels Visual C# and click Web
Click on ASP.NET Empty Web Application: Visual C#
Name your project (in this example we use MyStats) and click OK
Step 3: Add Default.aspx file.
1. Click Project -> Add New Item
2. Click Web Form, give it a name of Default.aspx and click Add
This will add two files to your web project (Default.aspx and Default.aspx.cs)
Step 4: Understanding relationship between C# and HTML
The Default.aspx page contains the general HTML code that will be displayed in web
browsers. There is also another page called Default.aspx.cs. This page contains C#
code that will be used to create dynamic HTML output.
Step 5: Adding Service Reference
C# needs to have a reference to the web service.
1. Click on Project -> Add Service Reference
2. An Add Service Reference Box will open, input the following address into the
address text box and click GO. You will need to edit this URL to match your
LabStats URL.
a. http://75.101.144.117/LabStats/WebServices/Statistics.asmx 3.
Change the Namespace to SoapStats and click OK
Step 6: Adding GridView control to Default.aspx
1. Double click on “Default.aspx” in solution explorer. This will open the
Default.aspx page in the text editor.
2. Add the following lines of code in between the div tags so that is looks like
below.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="groupName" HeaderText="Group Name" />
<asp:BoundField DataField="offCount" HeaderText="Offline" />
<asp:BoundField DataField="availableCount" HeaderText="Available" />
<asp:BoundField DataField="inUseCount" HeaderText="InUse" />
<asp:BoundField DataField="totalCount" HeaderText="Total" />
<asp:BoundField DataField="percentInUse" HeaderText="Usage Percentage"
DataFormatString="{0:P0}" />
</Columns>
</asp:GridView>
Example:
<asp:BoundField tells the GridView which columns to display. We are displaying
GroupName, Offline Count, Available Count, InUse Count, Total Count, and
Percentage of Use. You can remove columns as you see fit. Changing the
AutoGenerateColumns from “false” to “true” will display all columns in the DataSet.
Step 7: Adding the necessary code to the Default.aspx.cs page
1. In “Solution Explorer,” click on the arrow next to “Default.aspx”.
2. Double-click on “Default.aspx.cs.” You can also right click on your
default.aspx code and click View Code.
3. Add your web reference namespace
a. Add the following code to the bottom of your using texts. See below.
using MyStats.SoapStats
The using part tells C# to include code from another source. The MyStats says look
within this project and the SoapStats part further narrows it down to the web
reference you created earlier. If you have chosen different names for the project and
web reference, then you will need to make the appropriate adjustments in this
statement.
4. Add the following code to inside the Page_Load to retrieve Current Group
Statistics from LabStats. See below.
The code will look as follows:
StatisticsSoapClient proxy = new StatisticsSoapClient();
GroupStat[] station = proxy.GetGroupedCurrentStats();
GridView1.DataSource = station;
GridView1.DataBind();
Let’s break this down line by line:
1. StatisticsSoapClient proxy = new StatisticsSoapClient();
This tells C# to create a new StatisticsSoapClient object with the name
“proxy.”
2. GroupStat[]
station
=
proxy.GetGroupedCurrentStats();
GroupStat is a type of web service object that containts pertanent
information about your groups. We use the array notation since the
function GetGroupCurrentStats() returns an array of GroupStat
objects. 3. GridView1.DataSource = station;
In the previous step of this tutorial, you added a GridView control to the
default.aspx page. This line of code references the station array as the data
source for our GridView.
4. GridView1.DataBind();
This line binds the data to the GridView.
With everything put together the final code for the Default.aspx.cs file should like
this:
using System;
using
System.Collections.Generic;
using System.Linq; using
System.Web; using System.Web.UI;
using System.Web.UI.WebControls;
using MyStats.SoapStats;
namespace MyStats
{
public partial class Stats : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StatisticsSoapClient proxy = new StatisticsSoapClient();
GroupStat[] station = proxy.GetGroupedCurrentStats();
GridView1.DataSource = station;
GridView1.DataBind();
}
}
}
Step 8: Launching your sample web site
1. Click the green arrow button to launch your newly created web site.
2. The result will look something like this. If it doesn’t, make sure your groups
are published on the Public Page Settings page.
Aux: Changing things up a bit to make the GridView more appealing
1. Click to open Default.aspx in your project
2. Change the following to change font, header background and to alternate the
background of your rows.
BEFORE: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
AFTER: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="AliceBlue" HeaderStyleBackColor="GradientActiveCaption" Font-Names="Verdana">
Results:
AUX 2: Learning the API
The same URL you added as a web reference in step 4 can also be used to help you
learn all of the SOAP functions available. The URL I used is:
LABMAPS API: http://75.101.144.117/LabStats/WebServices/HierarchyAPI.asmx
CURRENT STATS API: http://75.101.144.117/LabStats/WebServices/Statistics.asmx
You will need to replace the IP address with the IP address of your LabStats 5 server.
When you access the URL in a web browser, you should see the following:
Every function available is listed on this page. If you click on the various function
names you’ll be taken to pages describing the services. Let’s take a look at the
“GetGroupedCurrentStats” page.
On this page you’ll see a description of what the function does. Then you’ll see the
XML code format necessary to communicate with the server. C# takes care of
wrapping your requests into XML format. However, we can derive clues of how to
use C# based on the XML code.
Take a look at the Statistics XML request block of code:
POST /WebServices/Statistics.asmx HTTP/1.1
Host: 75.101.144.117
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetGroupedCurrentStats"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetGroupedCurrentStats xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
Under the body I see the bracket “<GetGroupedCurrentStats
xmlns="http://tempuri.org/">.” This bracket reiterates the name of the function.
The lines in-between the <GetGroupedCurrentStats> brackets tell me what
parameters I need to pass to the function. In this case there aren’t any so no
parameters are accepted. Let’s take a look at another function this time in the
Hierarchy API called GetMap.
Take a look at the HierarchyAPI XML request block of code:
POST /WebServices/HierarchyAPI.asmx HTTP/1.1
Host: 75.101.144.117
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetMap"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetMap xmlns="http://tempuri.org/">
<mapID>int</mapID>
</GetMap>
</soap:Body>
</soap:Envelope>
In this example GetMap has a parameter of MapID in which you pass in an integer of
the MAPID you are referencing.