Tải bản đầy đủ (.pdf) (26 trang)

Reporting with Web Services and Mobile Devices.

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (870.81 KB, 26 trang )

Reporting with Web Services
and Mobile Devices
C
hapters 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 deliver
the 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 ser
vice
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 b
y 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.
I
n simple words, the common use of a web service is to communicate information using


XML 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
CHAPTER 6
8547ch06final.qxd 8/30/07 3:50 PM Page 227

Note
Simple Object Access Protocol (SOAP) is a protocol for exchanging XML-based messages over
computer networks, normally using the HTTP protocol. You can find more information on SOAP here:
/>.
So, how does the client (consumer) requesting the service know about the available service
functionality? Well, this information is provided by the service as a Web Services Description
Language (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 web
service’s capabilities without knowledge of the implementation details of the web service.
You can get more information on WSDL here:
/>Once you develop a web service, it is available to consume by a variety of clients. These
services 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 of
web services is beyond the reach of this book. I would suggest you to go through the Microsoft
Help to know more; you can start here:
/>In the Windows environment, web services need Internet Information Services (IIS) as
the host, either locally or on a remote machine. For our reporting project, we’ll host the report
generation 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 a
simple web service.
Web Services 101
Creating a web service is somewhat similar to creating an ASP.NET web site. Both the web site

and the web service need IIS for the hosting. However, when it comes to behavior, they differ:
w
eb sites ser
v
e 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 proj-
ect; see Figure 6-1 for a graphical representation of these steps:
1. Click File

New

Web Site.
2. I
n the T
emplates pane of the
N
ew Web Site dialog box, select ASP.NET Web Service.
3. F
r
om the Language drop-down menu, select Visual C#.
4. Please give the application a name; I’ve called the web service RSWebService101. You
may choose a different location for storing the application files according to your
preference.
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES228
8547ch06final.qxd 8/30/07 3:50 PM Page 228
5. Click the OK button to finish the process. VS 2005 will create a new ASP.NET web serv-

i
ce. Figure 6-2 shows the code that’s produced and the files inside Solution Explorer.
Figure 6-1. Creating a new ASP.NET web service
Figure 6-2. The newly created ASP.NET web service
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES 229
8547ch06final.qxd 8/30/07 3:50 PM Page 229
As you can see in Figure 6-2, the project contains the App_Code folder containing the C#
code and the
Service.asmx file as part of the project. The generated code is a complete web
service, ready to host with IIS.
Let’s assume we hosted this service with local IIS. Now, what do you think this service is
supposed to do? This service will simply return a “Hello World” message to the client applica-
tion that consumes it.
Before we build and host this web service with local IIS, let’s pay attention to the following
important 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
It is your responsibility to change it; you can
use any name as long as you feel it is unique. The best practice here is to use a convention
like
. In practice, this could translate to
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 web
service. 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 only
contains 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 code
behind 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 guessed
that 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 con-
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].
P
lease make sure that code behind the
Service.cs file looks like the follo
wing after adding our
web method:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES230
8547ch06final.qxd 8/30/07 3:50 PM Page 230
[WebService(Namespace = " />[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public 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
to 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
Enabled prompt.
Assuming that you don’t encounter any errors during the build, the web service will be
automatically hosted with the VS IDE’
s internal web server. Please see Figure 6-3 for the URL
and port on which our web ser
vice is hosted.
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES 231
8547ch06final.qxd 8/30/07 3:50 PM Page 231
Figure 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, I
got 3002 as the port number.
After the web service is successfully launched, a browser window will appear with our two
web 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 our
ASMX file, which shows the functions of the web service to the user.
Figure 6-4. W
eb ser
vice with default generated and newly created web methods
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES232

8547ch06final.qxd 8/30/07 3:50 PM Page 232
How 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-
n
ess 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 for
testing. 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 inv
oked and r
etur
n the greeting message as XML, as shown in Figure 6-6.
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES 233
8547ch06final.qxd 8/30/07 3:50 PM Page 233
Figure 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 using
the 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 of
developing a web service to generate an on-demand travel itinerary. The itinerary should be
sent across to the client in PDF format. To test the web site, you’ll also create a Windows Forms
client as the consumer of this web service. The report should meet all the characteristics
described 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.
Logo Yes
Data source
Itinerary
Page size Letter
P
age orientation Portrait
Layout design All information should be in the body section (no header or footer).
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES234
8547ch06final.qxd 8/30/07 3:50 PM Page 234
Figure 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 to
empower 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 pr
oject 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#.
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES 235
8547ch06final.qxd 8/30/07 3:50 PM Page 235
4. Please give the application a name; I’ve called the web service Itinerary. You may
c
hoose a different location for storing the application files according to your
preference.
5. Click the OK button to finish the process. VS will create a new ASP.NET web service
project named Itinerary.
VS will create a default
HelloWorld web method. Please delete the following code to
remove 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 will
ask you to put the dataset inside the
App_Code folder; go ahead and click the Yes button. Select
Cancel 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:
CHAPTER 6

REPORTING WITH WEB SERVICES AND MOBILE DEVICES236
8547ch06final.qxd 8/30/07 3:50 PM Page 236

×