Hi there,
Last week, I took the two hour clinic: Clinic 6264: Introducing Windows Communication Foundation using .Net Framework 3.5 & Visual Studio 2008 from the Microsoft eLearning site. It’s a free clinic, so if your new to WCF, it’s well worth a study. It’s a self-paced course and took me actually some more time than two
hours, since I had to make notes for this review on things I found
interesting to talk about. And a couple of hours for me writing this
post last weekend
Anyway, you will learn about Windows Communication Foundation, the next generation technology for developing service oriented applications. Within the clinic you will learn about WCF Architecture, how to create WCF Services and Clients, and enhancing WCF services with security and reliability.
In this post I’ll review the clinic:
Introduction to WCF:
Gerry O'Brien, technical product planner, gives us an introduction to WCF. He tells us that WCF was built up from the ground on the principals of Service Orientation. It is a short introduction.
Overview of WCF:
First we get an explanation of what Windows Communication Foundation is. It is THE technology for building distributed applications. The whole model is based upon schemas and contracts, is platform, programming language and implementation independant. It supports, SOAP, WS-* and also RSS protocols for communication. It supports also, untyped primitive messaging, typed messages, secure message exchange, reliable messaging, transacted messaging and queued messaging.
We then get a video where 'What is the WCF Service model' is explained. It's all about endpoints. We then get the WCF ABC explained (Address, Binding, and Contract). The address shows us where the service can be located, in an illustration we get to see three bindings: BasicHttpBinding, WSHttpBinding and NetMsmqBinding. Bindings are all about how the client needs to communicate with the service. And in the contract you define what the service does. WCF services can be hosted in IIS, and in addition to that, you can host your webservice in any .NET framework application by using the ServiceHost class. Metadata can be used to generate the WSDL (Web Service Description Language).
After this video we get some scenarios for building WCF applications. We get an example of a Peer channel application in which a duplex contract is used to send and receive messages. Second to that we get an Interoperability scenario of a Web service communication to a WCF service. A Messaging scenario in a Publisher-Subscriber setting and a Router example. At last there's the scenario of how to use WCF to create infrastructure components. We get an example of Federated Security. After all of this there is a self-test, you get three questions which are very easy if you read and watched the Overview of WCF part.
WCF Features for Developers of Service-Oriented Applications:
First we'll learn the productivity enhancements in WCF. WCF uses a Unified Programming Model so as a programmer you don't have to have the knowledge anymore of COM+, MSMQ etc... WCF makes it possible to declaratively specify features (through the use of attributes) or by specifying them in a configuration file. So one productivity enhancement is the reduction in code. There's a lot of infrastructure code built-in, as said before, by using attributes and/or declaratively configuration, a lot of code is reducted. And then there's the feature of Message Logging and Tracing, WCF provides a comprehensive set of logging and tracing features which can log at various points (during transport, at service level). There's a lot of tooling support also: COM+ Service Model Configuration Tool, Configuration Editor, Find Private Key Tool, ServiceModel Metadata Utility, ServiceModel Registration Tool, TraceViewer Tool and the WS_AtomicTransaction Configuration Utility.
After all this, we'll get an illustration video on Service-Oriented Development in WCF. WCF supports three message exchange patterns: one-way messaging, request-response messaging and duplex messaging. WCF Interoperability and Integration: Web Service Interoperability, Integration with MSMQ and COM+ and Support for Extensibility
Creating WCF Services and Clients:
Gerry O'Brien, technical product planner, gives us an introduction and talks about Datacontracts, faultcontracts and the out-of-the-box bindings. Short briefly introduction.
Creating a WCF Service:
WCF Contracts: There are 5 types of contracts: ServiceContracts, OperationContracts, DataContracts, MessageContracts, FaultContracts. Contracts are created in code by service developers and are exposed to clients in the service metadata.
In the part of 'The Process of Implementing a Service Contract' we'll be learned how to define a service contract by specifying the ServiceContract attribute to the interface of the Service. OperationContract attributes can be applied to the Services operations/methods. By implementing one or more ServiceContract a Service can be created. If you want to be able to send custom object types over the wire, you have to mark those classes with the DataContract attribute and the fields and properties with the DataMember attribute. The information that you provide in the service and data contract definitions is used to build a WSDL file, which clients can then use to access the service.
There are four options for Hosting a WCF Service: IIS, Windows Activation Service (an IIS 7.0 feature), Self-Hosting (by making use of the ServiceHost class) and as Managed Windows Service.
WCF Bindings: A binding is an object that specifies how to connect to the endpoint of a WCF service. A binding contains three categories of information: protocol information, the underlying transport protocol and the message encoding. There are a lot of built-in bindings available in WCF: BasicHttpBinding, WSHttpBinding, NetMsmqBinding and MsmqIntegrationBinding.
Service Configuration in WCF: WCF services can be configured in code or by using configuration files. To define an endpoint in code, use the AddEndpoint method on the ServiceHost object to pass in an address, a binding object, and a service contract in the form of an interface. To set up endpoints in configuration files, define <endpoint> elements within a parent <service> element inside the <system.ServiceModel> section of your configuration file.
We'll also get an video on 'How to Create a Basic WCF Service'. In Visual Studio 2005 a WCF Service is made (and I thought the title of this elearning was 'Introducing Windows Communication Foundation using .Net Framework 3.5 & Visual Studio 2008', I guess they didn't update the video's
. All the WCF stuff is inside the System.ServiceModel assembly. We'll also make use of the WCF Configuration Editor for defining our endpoints.
Creating and Invoking a WCF Client:
It's all about proxies! Proxies can be created in one of two ways: at design time by using the svcutil.exe tool or dynamically at runtime by using a ChannelFactory. The last I didn't know. A ChannelFactory provides a flexible way to create proxies at runtime, by creating a factory from the address, binding details, and contract details, and then calling CreateChannel on the factory.
We then get a demo video of how to create a WCF Client by making use of a proxy. Also the svcutil.exe command line tool is shown.
And then, for me something completely new: 'Creating a Client by Using a Channel Factory'. A channel always starts in the Created state; which means that the channel can be configured but it cannot used to send messages. After you open it, you can send and receive messages. The ChannelFactory has access to the underlying channel and by using the Credentials property you'll be able to retrieve the credentials that are associated with the channel. We'll then learn about asynchronous invocation in WCF and about implementing a Duplex Contract in WCF. To implement a duplex contract, we'll have to define a service contract in the usual way, and use the CallbackContract named parameter to refer to a callback interface.
Customizing WCF with Behaviors:
Behaviors enable you to modify how service and clients operate. They can be applied at many places in WCF: client channels, services, endpoints, contracts, and operations. What I didn't know about was that developers can create custom behaviors by using IServiceBehavior and IEndpointBehavior. Any class that implements these interfaces can be used as a behavior and applied as an attribute or in a configuration file. That's really cool stuff!
We'll also get a video demonstration on this subject: 'Configure behaviors in a WCF service'. A video that goes really fast. First we start with using the service behaviour InstanceContextMode.PerSession, and is worth watching multiple times so you'll really get the benefit out of it. We then start using the InstanceContextMode.PerCall behaviour and finally the Singleton instance context mode. The example shows exactly what the differences are when each mode is used
Exploring WCF Features:
First Gerry O'Brien gives us an introduction and he talks heavilly about bindings, stacks and security, whooooo, the heavy part will start!
We'll learn about the predefined bindings, which are: BasicHttpBinding, WSHttpBinding, WSDualHttpBinding, WSFederationHttpBinding, MsmqIntegrationBinding, NetMsmqBinding, NetNamedPipeBinding, NetPeerTcpBinding and NetTcpBinding. We then learn how to make our own custom bindings. A binding must specify at least a transport and an encoder. WCF defines three predefined transports: HTTP, TCP en Named Pipes. And uses three types of encoding: text, binary and MTOM data. And then there are additional features e.g a ReliableSession binding element specifies the type of session to use and the Security binding element specifies the type of security required.
We then are learned the 'Guidelines for Configuring the Transport Layer'. Be aware of NATs and Firewalls, Streaming Message Transfer, Configuring HTTP and Transport Quotas.
Introduction to WCF Security:
WCF provides comprehensive security features in three main areas: transfer security, authentication and authorization of clients, and auditing. Transfer security supports two main mechanisms: Transport mode (which uses the security features of a transport layer such as HTTPS) and Message mode (which protects the message itself by using a protocol such as WS-Security). WCF also supports a mixed mode, where integrity and privacy are provided by the transport, while authentication is handled by using credentials in the message.
WCF uses claims for authorization. Claims are extracted from credentials and used to provide sophisticated access control. By default, WCF security events are written to the Windows event log, so that administrators can see who has accessed a particular service. You can configure auditing in code or in the service configuration file.
Authentication and Authorization in WCF: If MessageMode is used as a transport security, you can use Message Credentials for security. A transport may support more than one method for obtaining authentication information. You can use the .NET PrincipalPermission attribute to restrict access to an operation based on name, role, or authentication status. At the end we'll get to see a demo in which basicHttpBinding and MessageMode Security are being used. Also worth to watch more than once!
Reliability in WCF Applications:
WCF supports two types of transactions: shared transactions and transacted messaging. WCF supports two transaction protocols: WS-AtomicTransaction protocol and OleTransaction protocol. On the client use a TransactionScope object to group operations into transactions. The client can control the isolation level and timeout for the transaction by using a TransactionOptions object.
We then get an illustration of 'demo Queuing in WCF'. Service contracts that use queuing must be defined as one-way. The class that implements the contract can specify queued delivery. WCF provides two error-handling mechanisms, called time-to-live and poison messages that are unique to queuing. WCF supports two bindings that use queuing: NetMsmqBinding and MsmqIntegrationBinding.
Well that was about it. The last parts about security, authorization etc...was really heavy stuff, but gave me a good introduction about what WCF has to offer.
Oh yeah...here's my certificate I got:
Hope this was usefull!
gr,
Robbert