Visual Studio ® 2008: Windows ® Communication Foundation
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
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
Benefits of Service Oriented Architecture
Designing an SOA Application
Get commitment for the effort required
1
Gather the services
2
Group related business functions together
3
Services, Components, and Objects
ComponentsService
Objects
WCF and SOA
WCF supports SOA:
However:
• Developers do not have to learn WSDL
• Functionality is handled by the runtime
• Tools to enable easy creation and consumption of services
• 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:
Language level:
Service level:
• Customer management service application
• 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
• 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
• 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
• Not limited to one particular set of protocols
Sending a WCF Message
Client ServiceWCF
dispatcherProxy
Message EncodingEncoding
Transportchannel
Transportchannel
ChannelChannelChannelChannel
ChannelChannel
Transportchannel
Transportchannel
EncodingEncodingBindingBinding
ChannelChannelChannelChannel
ChannelChannel EndpointsEndpoints
The ABC of Endpoints
Service
Where to find the service
Example: http://localhost:8001/MathService
Where to find the service
Example: http://localhost:8001/MathService
AddressAddress
How to communicate with the service
Example: BasicHttpBinding
How to communicate with the service
Example: BasicHttpBinding
BindingBinding
What the service can do for you
Example: [OperationContract]int Add(int num1, int num2);
What the service can do for you
Example: [OperationContract]int Add(int num1, int num2);
ContractContract
Structure of a Service
Service
.NET Classesand Interface
ServiceHostService Contracts for
Domain Functionalityor Metadata Exchange
A Unified Programming Model
A unified development model
WindowsCommunication
Foundation
Many confusing and complicated options
Remoting COMDCOM
COM+MSMQ
WSEASMX
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;using System.ServiceModel;
namespace ConnectedWCF{ [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); }}
using System;using System.ServiceModel;
namespace ConnectedWCF{ [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); }}
Principal namespace for WCF
Attributes control exposure of types and methods
The ServiceContract Attribute
Service contracts:
The ServiceContract attribute:
Namespace serves to disambiguate types and operations in service metadata:
• Can be used on interfaces or classes
• Is identified by tools and environments designed for WCF
• 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
• 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 overthese areas
Contracts, Metadata, and Artifacts
ServiceClass
ServiceInterface
ServiceMetadata
Implements
RunningService
ServiceProxy
Implements
ServiceAssembly
ProxyArtifacts
Generate
Generate
Generate
Generate
Generate
Generate
ServiceModel Metadata Utility Tool (Svcutil.exe) generates metadataand artifacts
ServiceModel Metadata Utility Tool (Svcutil.exe) generates metadataand 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
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