Você está na página 1de 34

Visual Studio® 2008:

Windows®
Communication
Foundation
Module 1: Getting Started with Windows
Communication Foundation
• Designing an Application to Be Part of a Service Oriented
Architecture (SOA)
• Overview of WCF Architecture

• Using a Language-Level Interface As a Service Contract

• Implementing a Simple WCF Service in Visual Studio 2008

• Consuming a Simple WCF Service in Visual Studio 2008


Lesson: Designing an Application to Be Part of a
Service Oriented Architecture (SOA)
• Benefits of Service Oriented Architecture

• Designing an SOA Application

• Services, Components, and Objects

• WCF and SOA

• WCF in an SOA Context


Benefits of Service Oriented Architecture
Primary objective:
• Deliver more business benefit faster

• Independent services act as building blocks for new or


improved business processes

• Applications become:
 User interface flows that make use of services
 Longer-running workflows that make use of services
 Portals and mash-ups that make use of services

• Success depends more on business practice than technology

• SOA enables you to build services much faster once the


business has made its mind up
Designing an SOA Application

1
Get commitment for the effort required

2
Gather the services

3
Group related business functions together
Services, Components, and Objects

Service Components

Objects
WCF and SOA

WCF supports SOA:

• Developers do not have to learn WSDL


• Functionality is handled by the runtime
• Tools to enable easy creation and consumption of services

However:
• Just because you have exposed an interface over a Web service
protocol stack does not mean that you have created part of an SOA
• SOA helps you design services and WCF enables you to implement
these services
WCF in an SOA Context

Example:

• Customer management service application

Language level:
• Objects for customers persisted into a database
• Business rules (some may be in stored procedures)
• Business interface to embody small amounts of business process
• Standard .NET Framework objects

Service level:
• Similar business facades but with potentially different data types
that are more service friendly
• These are, and use, WCF-aware data types
Lesson: Overview of WCF Architecture
• Service-Oriented Development with WCF

• Sending a WCF Message

• The ABC of Endpoints

• Structure of a Service

• A Unified Programming Model

• Communicating with Non-WCF Services


Service-Oriented Development with WCF
Create a service:
• Over a network protocol

Use a service:
• Contact the service to access one part of that functionality

• Clients and services communicate by passing messages

• Not limited to one particular set of protocols

• Aimed at clients and services that do not share the same


address space

• Service-oriented architecture is a concept related to


the style of the application and granularity of the services
Sending a WCF Message
WCF
Client Proxy Service
dispatcher

Channel Channel
Channel Channel
Channel Channel Endpoints

Binding Encoding Message Encoding

Transport Transport
channel channel
The ABC of Endpoints

Address

Where to find the service

Example: http://localhost:8001/MathService Service

Binding

How to communicate with the service

Example: BasicHttpBinding

Contract
What the service can do for you

Example: [OperationContract]
int Add(int num1, int num2);
Structure of a Service
.NET Classes
and Interface

Service

Service
Service Contracts for Host
Domain Functionality
or Metadata Exchange
A Unified Programming Model

Many confusing and complicated options


Remoting COM
DCOM
WSE
MSMQ ASMX COM+

Windows
Communication
Foundation

A unified development model


Communicating With Non-WCF Services

• Web services
 Older Web services such as ASP.NET .ASMX files
 Other Web service stacks that support different WS-*
protocols
 Plain Old XML services (POX)

Internal protocols
 MSMQ (there are several specific bindings)
 .NET Remoting
 COM+
Lesson: Using a Language-Level Interface As a
Service Contract
• Example of a Simple Contract

• The ServiceContract Attribute

• The OperationContract Attribute

• Data and Messages

• Contracts, Metadata, and Artifacts


Example of a Simple Contract

using System;
Principal
using System.ServiceModel;
namespace
namespace ConnectedWCF for WCF
{
[ServiceContract(Namespace="http://myuri.org/Simple") ]
public interface IBank
{
[OperationContract]
decimal GetBalance(string account);

[OperationContract]
void Withdraw(string account, decimal amount);

[OperationContract]
void Deposit(string account, decimal amount);
}
}
Attributes control exposure of types and methods
The ServiceContract Attribute
The ServiceContract attribute:
• Can be used on interfaces or classes
• Is identified by tools and environments designed for WCF

Service contracts:
• Support services
• Are analogous to interfaces on a polymorphic object – caller may
not care about other contracts
• Are only visible to clients if exported on an endpoint
• Contains one or more operations

Namespace serves to disambiguate types and operations in


service metadata:
• Defaults to http://tempuri.org
• Not to be confused with language-level namespace
The OperationContract Attribute

• Attribute can be used on methods but not other class


elements such as properties or indexers
• Identified by tools and environments designed for WCF

• Methods are only visible in service contract if marked with

OperationContract
Data and Messages
• Parameters and return values must be marshaled between
client and server
• CLR types are mapped to XML Infoset by serialization

• WCF enables you to define custom serialization

• Serialized data is packed into the message

• Contents and structure of message must be understood by


client or service at the other end
• Data and message contracts provide control over
these areas
Contracts, Metadata, and Artifacts
Running
Service
Implements Service
Assembly
Service
Class
Service
Metadata
Generate Generate
Service
Interface

Generate

Service
Proxy
ServiceModel Metadata
Utility Tool (Svcutil.exe)
Implements Proxy
generates metadata
Artifacts
and artifacts
Lesson: Implementing a Simple WCF Service in
Visual Studio 2008
• Defining the Service Contract and Service Class

• Hosting the Service

• Configuring the Service

• Demonstration: Creating a Simple Bank Service


Defining the Service Contract and Service Class
Hosting the Service
Configuring the Service
Demonstration: Creating a Simple Bank Service
In this demonstration, you will see how to:
• Create a simple WCF service in Visual Studio 2008

• Host the service in IIS by using the facilities of Visual


Studio 2008
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Consuming a Simple WCF Service in
Visual Studio 2008
• Importing the Metadata

• Calling the Service by Using the Proxy

• Demonstration: Calling the Simple Bank Service


Importing the Metadata
Calling the Service by Using the Proxy

Metadata import generates client-side classes that represents


the service and service contract:
• <ServiceName> Client is the service proxy type
• <ServiceContractName> is the contract representation type

Client-side class created in the current project’s namespace


when you add the service reference

using BankServiceClient.BankServiceReference;
...
IBank proxy = new BankClient("WSHttpBinding_IBank");
...
double balance = proxy.GetBalance(1234);
Demonstration: Calling the Simple Bank Service
In this demonstration, you will see how to invoke
operations on a simple WCF service by using the facilities of
Visual Studio 2008
Lab: Creating a Simple Service
• Exercise 1: Creating a Simple WCF Service

• Exercise 2: Calling the Simple WCF Service

Logon information

Virtual machine 6461A-LON-DEV-01

User name Student

Password Pa$$w0rd

Estimated time: 60 minutes


Lab Review
• What must you do to the ClinicAdminClient application if
you change the AppointmentServiceContract?
• What would be the consequences to the solution if you
removed the default metadata endpoint from the
AppointmentServiceIISHost project?
Module Review and Takeaways
• Review Questions

• Best Practices

• Tools

Você também pode gostar