• “Web Services 101,” a step-by-step tutorial for using web services • Creating a report using web services • Reporting with mobile devices What Is a Web Service?. Web Services 101 Creat
Trang 1Reporting with Web Services
and Mobile Devices
Chapters 4 and 5 discussed using the Windows Forms and the Web Forms clients to host our
reports Starting with this chapter, we’ll look at the rest of the clients that can host reports
First in line are web services and mobile devices In later chapters, you’ll see console
applica-tions, Windows services, and web parts in action as delivery vehicles for reports
Both the web services and the mobile device clients are uniquely positioned to deliverthe reports By exposing your report with a web service, you are reaching a far greater user
base, because a web service can be consumed by a variety of clients and is accessible across
platforms Similarly, providing report access with mobile devices can help users to get the
information on demand More and more organizations are moving toward service-oriented
architecture (SOA) to manage information, and web services are an important part of SOA
This chapter will cover
• What is a web service?
• “Web Services 101,” a step-by-step tutorial for using web services
• Creating a report using web services
• Reporting with mobile devices
What Is a Web Service?
A web service is a technology designed to enable machine-to-machine interoperability over
any given network Web services are popularly used as web APIs and accessed over a network
such as the Internet and intranets Web services can also help to eliminate duplicate business
functions by allowing them to be shared by more than one application For example, a credit
check web service can serve both billing and web enrollment applications Web services can
also be executed from a remotely hosted system
In simple words, the common use of a web service is to communicate information usingXML messages The SOAP standard is used to communicate these messages Functionality
built with web services is ready to use by any potential client For example, a financial
institu-tion can build a web service to provide currency exchange rates in which a client can consume
this service to execute a web method to get the necessary exchange rate
227
Trang 2functionality? Well, this information is provided by the service as a Web Services DescriptionLanguage (WSDL) file A WSDL file contains interface information that is needed for commu-nication among web service producers and consumers It allows a client to utilize a webservice’s capabilities without knowledge of the implementation details of the web service.You can get more information on WSDL here:
http://msdn2.microsoft.com/en-us/library/ms996486.aspx
Once you develop a web service, it is available to consume by a variety of clients Theseservices are also shared across platforms; that means that a service hosted in the UNIX envi-ronment can be consumed using a NET-based Windows client Covering all the aspects ofweb services is beyond the reach of this book I would suggest you to go through the MicrosoftHelp to know more; you can start here:
http://msdn2.microsoft.com/en-ca/webservices/default.aspx
In the Windows environment, web services need Internet Information Services (IIS) asthe host, either locally or on a remote machine For our reporting project, we’ll host the reportgeneration service with local IIS and consume it with the Windows Forms application Before
we go on and develop the web service to produce a report in PDF format, let’s learn to create asimple web service
Web Services 101
Creating a web service is somewhat similar to creating an ASP.NET web site Both the web siteand the web service need IIS for the hosting However, when it comes to behavior, they differ:web sites serve HTML pages to the user, and web services send XML messages In this tutorial,we’ll create a function to send a “Welcome to Web Service 101” message back to the client
Creating a Web Service Project
Please open Visual Studio, and use the following steps to create an ASP.NET web service ect; see Figure 6-1 for a graphical representation of these steps:
proj-1. Click File ➤New ➤Web Site
2. In the Templates pane of the New Web Site dialog box, select ASP.NET Web Service
3. From the Language drop-down menu, select Visual C#
4. Please give the application a name; I’ve called the web service RSWebService101 Youmay choose a different location for storing the application files according to yourpreference
Trang 35. Click the OK button to finish the process VS 2005 will create a new ASP.NET web ice Figure 6-2 shows the code that’s produced and the files inside Solution Explorer.
serv-Figure 6-1.Creating a new ASP.NET web service
Figure 6-2.The newly created ASP.NET web service
Trang 4important things:
• The namespace
• The Service.asmx file
• The web method
A namespace is needed to uniquely identify the web service As you can see in Figure 6-2,
VS gives the default name http://tempuri.org/ It is your responsibility to change it; you canuse any name as long as you feel it is unique The best practice here is to use a conventionlike http://ServiceName.CompanyName.org In practice, this could translate to
http://CalculateAnnualSales.BestHomeConstructions.org/, where ServiceName is
CalculateAnnualSales and CompanyName is BestHomeConstructions
Now, you know about the namespace, so what about the Service.asmx file? The main use
of this file is to display the information about the various methods that are part of this webservice It also provides the WSDL information You might be wondering if all this information
is stored inside the file Well, no If you open the Service.asmx file, you’ll notice that it onlycontains the following text:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %>
As you can see, all it has is the scripting language information and location of the codebehind the file When you request the ASMX page through IIS, ASP.NET uses this information
to generate the content displayed in the web browser (the web methods and their descriptions).Let’s move on to learn about web methods I’m sure most of you have already guessedthat this is the function part of the web service As you can see in Figure 6-2, Visual Studio gen-erates a default web method called HelloWorld You can add as many web methods as you like
to your web service
Adding a Web Method to Service.cs
Let’s add our own web method to send the “Welcome to Web Service 101” message to the sumer client Adding the web method is simple; it is similar to writing your own C# method.The only difference here is that each web method you write must have the tag [WebMethod].Please make sure that code behind the Service.cs file looks like the following after adding ourweb method:
con-using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
Trang 5public string HelloWorld() {return "Hello World";
}[WebMethod]
public string MyHelloWorld(){
return "Welcome to Web Service 101";
}}
You can see that the new web method is called MyHelloWorld, and its scope is public
You’ll also notice that this method returns our message as a string and that I changed the
default namespace from http://tempuri.org/ to http://MyHelloWorld.Apress.com/ All right,
now we’re ready to build our web service and host it with the local IIS
Building the Project
I’m sure you are ready to breathe life into your web service to see your hard work in action
Building a web service is like building any other project with the VS IDE As you know, you can
click the small, green Play button in the main toolbox or press F5 on the keyboard to start the
application in run-time mode
■ Note If you’re building the project for the first time in debug mode, click OK at the Debugging Not
Trang 6Figure 6-3.The web service is hosted with the internal web server of the VS IDE.
■ Note You may get a different port number from the internal web server As you can see in Figure 6-3, Igot 3002 as the port number
After the web service is successfully launched, a browser window will appear with our twoweb methods inside Take a look at Figure 6-4 Does the URL in the address bar seem familiar
to you? Well, as I mentioned earlier in this chapter, the browser window is launched with ourASMX file, which shows the functions of the web service to the user
Figure 6-4.Web service with default generated and newly created web methods
Trang 7How Does the Web Service Work?
As you can see in Figure 6-4, the web methods appear as links So, what happens when we
click them? Simply put, think of the web service as a sort of server waiting to provide the
busi-ness function needed by the client So far, we know we have the web service, ready to serve! As
all web methods in a web service perform some business function, we need a client request to
invoke them So, where is the client?
It is common practice to build a test client in the same solution as the web service fortesting However, it is possible to test a web method without creating a test client As you can
see in Figure 6-4, the browser window acts like a client to let you invoke the web method and
do some basic functionality testing A user can make this browser window the client and
exe-cute a web method by clicking the link So, if you click MyHelloWorld, you should see the
browser window content change to include an Invoke button (see Figure 6-5)
Figure 6-5.The web method in action after clicking the MyHelloWorld link
Now your browser window acts like a consumer client What do you think the result will
be if you click the Invoke button? As the button’s name suggests, the MyHelloWorld web
method will be invoked and return the greeting message as XML, as shown in Figure 6-6
Trang 8Figure 6-6.The XML message is displayed after invoking the web method.
All right, now it’s time to move on to our reporting project Let’s generate a report usingthe web service and consume it with the Windows Forms client
Creating a Travel Itinerary Report
Assume you’re working for Happy Landings, Incorporated as a developer You have the task ofdeveloping a web service to generate an on-demand travel itinerary The itinerary should besent across to the client in PDF format To test the web site, you’ll also create a Windows Formsclient as the consumer of this web service The report should meet all the characteristicsdescribed in Table 6-1, and the PDF report output should match Figure 6-7
Table 6-1.Report Characteristics
Characteristics Value
Report title Travel Itinerary
Company title Happy Landings, Inc
Data source Itinerary
Page size Letter
Page orientation Portrait
Layout design All information should be in the body section (no header or footer)
Trang 9Figure 6-7.Solution of the reporting travel itinerary report
Business Case
When did you last have an itinerary? We know the importance of travel, especially air travel
Air travel has made this world a global village; we can quickly reach one part of the world from
another So, as you can see, an itinerary is a common form of report
In this case, Happy Landings Incorporated wants to provide this report as a web service toempower its business partners This web service can be used by individual travelers or by
agents Because this is a web service, all sorts of clients can benefit
Getting the Web Service Ready
You learned how to create an ASP.NET web service earlier in this chapter; now it’s your turn to
practice getting the web service ready You may make use of the solution RSWebService101 as
a template for this project or create it from scratch Creating the new project from scratch is
good practice; you can always refer to the steps mentioned in the tutorial if you get stuck
Please use the following steps to create an ASP.NET web service project (see Figure 6-1 for
an illustration of the steps):
1. Click File ➤New ➤Web Site
2. In the Templates pane of the New Web Site dialog box, select ASP.NET Web Service
3. From the Language drop-down menu, select Visual C#
Trang 10remove this web method:
[WebMethod]
public string HelloWorld() {return "Hello World";
}Let’s add a new dataset to the project and name it dsItinerary You’ll notice that VS willask you to put the dataset inside the App_Code folder; go ahead and click the Yes button SelectCancel in the Table Adapter wizard dialog box; we’ll create the data table later Before continu-ing, please make sure your solution looks similar to the one shown in Figure 6-8
Figure 6-8.Web Service–generated code with default WebMethod removed
Step 1: Creating a Data Table
We’ve already added the dataset to the project, so now its time to add the data table to it.Please use the following steps to add the data table inside the dataset:
Trang 111. You can go to the dataset designer in two ways: double-click the dsItinerary nodeinside Solution Explorer, or right-click the dsItinerary node and select View Designer.
2. Start adding the data table by right-clicking the design surface and selecting Add ➤DataTable
3. Click the header of the newly created data table, and name it dtItinerary Add thecolumns to dtItinerary by right-clicking DataTable and selecting Add ➤Column
4. Please add the following columns to the Data Table; your data table should look likeFigure 6-9:
Trang 12Figure 6-9.The final look of the dtItinerary data table
Step 2: Designing the Report Layout
All right, we have our dataset in place with the data table and all the necessary columns, sowe’re all set to start working on designing the report layout Add the report by selecting theproject in Solution Explorer, right-clicking it, and selecting Add ➤New Item; select Reportfrom the Add New Item dialog box Please name the report rptItinerary.rdlc Click the Addbutton to complete the process
Once you click the Add button, a new report is added to the project and opened in thereport designer If you look at the report output in Figure 6-7, you’ll see that we don’t need aheader and footer here All information present in the itinerary can be easily placed inside thebody section We’ll make use of two rectangle report items: the first rectangle will have thelogo and customer name, and the second will have the travel details
Setting Up the Page
Let’s set up the page to meet our needs We need to make sure the report is letter-size and has
a portrait page orientation Right-click the open area on the design surface, and select ties; you may wish to put your name in the Author field and add information about the report
Proper-to the Description field I’d advise you Proper-to let all other choices stay at their defaults
Designing the Body Section
For this report, we need two rectangle items, one image item, and a total of forty text boxes.Let’s start the design by dragging the two rectangle items from the Report Items toolbox anddropping them on to the report design surface In the first rectangle, drag and drop the imageand four text boxes
We’re going to embed the company logo in the report Embedding an image is a two stepprocess First, you need to embed the logo in the report by setting the EmbeddedImages
Trang 13property Next, you need to browse to the physical path of the image and embed it within the
report After you embed the image in the report, just set the Value property of the image reportitem to the image name
Figure 6-10.The first rectangle report item with its content: the logo image, company name,
“Itinerary for” label, customer name, and reservation code
■ Note If you move a rectangle report item, all content inside moves with it, because all report items
con-tained inside the rectangle act as a group Therefore, when the rectangle is moved, the entire group of items
moves with it
The report title and company name font sizes are set to 14 pt You’ll also notice that
“Reservation Code” is concatenated with the information from the ReservationCode field of
the dataset
As usual, drag and drop the rest of the text box report items from the Report Items box Some text boxes are used as labels, and some are used to display values from the dataset
tool-Please set up the text boxes as explained in Table 6-2
Table 6-2.Text Box Values, Labels, and Expressions
From: =First(Fields!TravelFrom.Value)
To: =First(Fields!TravelTo.Value)
Departure Terminal: =First(Fields!DepartureTerminal.Value)
Arrival Terminal: =First(Fields!ArrivalTerminal.Value)