McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 6
Services
McGraw-Hill 6-2
Objectives
• Discuss the concepts of Windows Communication Foundation (WCF) Services and understand the terminology
• Create a WCF Service
• Add a service reference to a Windows project
• Consume a WCF Service from a Windows project
• Perform calculations in a WCF Service
• Access data through a WCF Service
• Consume a third-party WCF Service
McGraw-Hill 6-3
Concepts and Terminology - 1
• Communication starts with a client application
• The service application waits for a client to send
a message and then responds
– A single application can be both a client and a service
• WCF uses Web Service specifications
– WS-Security and WS-ReliableMessaging for example
• WCF can communicate with other platforms that
support SOAP protocol
– Also can work with simple XML formats such as RSS
McGraw-Hill 6-4
Concepts and Terminology - 2
• An endpoint indicates where messages
can be sent (address), how sent (binding),
and a contract defining the service
provided
– Referred to as the “abc” of the service
• Address, binding, contract
– Endpoints are handled by VB when a service
is used by adding a service reference in the
client application
McGraw-Hill 6-5
Concepts and Terminology - 3
• The Web.config file indicates the appropriate address and binding – The address is localhost when using the Visual Studio web
server
• WCF Service has two files by default – IService.vb and Service.vb
McGraw-Hill 6-6
Concepts and Terminology - 4
• An operation contract shows a function or
sub procedure header listing parameters
and return type
– Each operation contract requires the method
to be written in the Service.vb file
• After a service is created, it may be used
or consumed from another application
McGraw-Hill 6-7
Windows Communication
Foundation (WCF)
• A WCF Service component may include
XML, SOAP, WSDL, and HTTP services
– VS handles most of the details
• Each of the technologies can be thought of
as protocols, or “rules” that both sides
understand
– Used to store, locate, and use WCF Services
McGraw-Hill 6-8
XML
• A standard method for storing data that
can be transferred easily from one
machine or platform to another
• Key advantage for Web services is that
data are transmitted in a text format rather
than in binary
– Allows data to pass through many firewalls
– .NET Framework takes care of the details
McGraw-Hill 6-9
SOAP
• Simple Object Access Protocol (SOAP) is the
popular protocol for sending data
• Includes a set of rules for handling requests and
responses
– Includes class names, method names, and
parameters
• SOAP works with XML but does not include a
specific protocol for transporting the response
and request packets
– The transport protocol is most frequently HTTP
McGraw-Hill 6-10
WSDL
• Web Services Description Language
(WSDL)
– Contains information about the names of
methods, the parameters that can be passed,
and the values that are returned from the
functions
McGraw-Hill 6-11
More Acronyms
• Uniform resource identifier (URI) uniquely
identifies a resource on the Web
– More generic than URL
• For technical specifications, industry standard term
URI is preferred to URL
– Always end URI or URL with a slash to avoid
an extra trip to the server
McGraw-Hill 6-12
Creating a WCF Service
• Create a solution and the consumer
application
• Add a WCF Service
• Rename the WCF Service
• Create the operation contract
• Code the WCF Service
• Test the Service
McGraw-Hill 6-13
Consuming a WCF Service
• Create a project with a Service reference
• Add a Service reference
• Write the code
• Run the completed application
– If there is an error while running the service,
change to debug = true in the Web.config file
for the service
McGraw-Hill 6-14
Performing Calculations
in a WCF Service
• Add the contract to the Service
– Add the method found on page 260 to the class
• Create a client or consumer application
• Add a service reference to the project and controls to the form
McGraw-Hill 6-15
Accessing Data
through a WCF Service
• Easily create a service that returns a
dataset
– Similar to the data tier in a multitier data
project
• The component fills a DataSet
– Returns the DataSet to the calling program
from a WCF Service function
McGraw-Hill 6-16
Creating a Data WCF Service - 1
• Divides the
data tier into
two separate
projects
– The data
access tier
and the
data entity
tier
McGraw-Hill 6-17
Creating a Data WCF Service - 2
• Create the solution
• Create the WCF Service
• Add the data-tier components
• Add the data source
• Configure the DataSource
• Code the WCF Service
• Set up the client user interface
• Write the client code
McGraw-Hill 6-18
The DataSet Merge Method
• Merge method of the DataSet class is
used to merge rows, tables, or datasets
that have similar schemas
• Create a service that defines a dataset
– Call the Fill or Get method from the table
adapter
• Merges the resulting data set into the data set
defined by the service
PubsDataSet.Merge(ADataService.GetAuthors)
McGraw-Hill 6-19
Placing Validation and Access
Code in the Data-Tier Projects
• Click any empty spot of the DataSet Designer to
view the dataset’s properties
– The DataSet Project property determines where the
Dataset.Designer.vb code file resides
• To write code for a data access tier, add a class
library item to the project
• Write Fill and Update methods for data access in
the Data Manager file of the data access tier
McGraw-Hill 6-20
Writing Validation Code
• In a service place the validation code for
updates in the DataEntity tier
– Double-click on the table in the DataSet
Designer
– The class is added to the DataEntity project
– Write dataset validation procedures in the file
McGraw-Hill 6-21
Setting Properties of
the Dataset Fields
• Double-click on the Dataset.xsd file in the
Solution Explorer
– Select each field name
• View or set its properties
– Set Caption, DefaultValue and other
properties
McGraw-Hill 6-22
Working with Related Tables
• Hands-on project uses related tables
– Similar to creating a single-table application
• Form must contain two dataset Merge
methods
– Specify the table and dataset names
• Add a query for each table to return in the
DataSet Designer window