Exploring the WCF Programming Model
•
A single API for building connected systems. (Unification)
•
Referring to technologies such as ASP.NET web services (ASMX), .NET Remoting, Enterprise Services, Web Services Enhancements, and the System.Messaging namespace.WCF is meant to provide an easier-tounderstand and unified programming model compared to the previous set of technologies.
•
The heart of WCF lives inside the System.ServiceModel namespace and the messaging system underneath it.
Introducing the Technical Architecture WCF is part of the .NET Framework 3.0 that comes with Windows Vista and, among others, contains the following technologies: • • • •
Windows Windows Windows Windows
Communication Foundation (WCF) Presentation Foundation (WPF) Workflow Foundation (WF) Card Services (WCS)
Learning the ABCs of WCF
• The address specifies where the messages can be sent (or where the service lives). • The binding describes how to send the messages. • The contract describes what the messages should contain.
WCF ProgrammingModel
WCF Programming Model
clients need to know the ABCs of a service to be able to use the service. The Web Services Description Language (WSDL) is meant to describe service endpoints in a standardized way. A WSDL file describes what a service can do, how a service can be accessed, and where the service can be found.
Can see how the service model influences the messaging layer
WCF Programming Model
•The service side can have multiple endpoints. A service just sits around and waits for messages to come in on its endpoints. •For client it is possible to have your client connect to multiple services by using multiple proxies with different endpoints (that is, connected to different services). •The client / Service side can have a specific behavior to do local configuration, such as concurrency, instancing, throttling, error handling, transaction control, security, and so on.
What are Addresses
So, the format of a service address is as follows: scheme://<machinename>[:port]/path1/path2
What are Base Addresses • WCF supports base addresses, which enables you to host multiple endpoints under the same base address
What Are Bindings • A binding defines how you can communicate with the service and as such has the biggest impact in the programming model of WCF. • It is the primary extension point of the ABCs of WCF.
• WCF provides a default set of bindings that should cover most of your requirements. If the default bindings don’t cover your requirements, you can build your own binding by extending from CustomBinding.
The binding controls the following: • The transport (HTTP, MSMQ, Named Pipes, TCP) • The channels (one-way, duplex, request-reply) • The encoding (XML, binary, MTOM…) • The supported WS-* protocols (WS-Security, WSFederation, WS-Reliability, WS-Transactions)
Predefined WCF Bindings
What are contracts ?
What are contracts ? • One of the core principles of service orientation is explicit boundaries. • Contracts are the way service orientation achieves true interoperability between different platforms. • Service orientation draws a distinct line between the “external interface” and the “internal implementation.” Contracts are not connected to the .NET type system but rather to an implementation of several standards by using the .NET type system.
Service Contracts To influence the service contract, the [ServiceContract] attribute has several parameters that have their own functions:
Service Contracts To influence the service contract, the [ServiceContract] attribute has several parameters that have their own functions:
Operation Contract To influence the service contract, the [OperationContract] attribute has several parameters that have their own functions:
Data Contract •
To influence the data contract, the [DataContract] attribute has several parameters that have their own functions:
Data Member •
To influence the data members and to make versioning possible, you need to be aware of several parameters for the [DataMember] attribute:
Message Contracts
•
Sometimes you require more control over the SOAP envelope that WCF generates.
•
The [MessageContract] attribute allows you to map fields into either the SOAP body or the SOAP headers by means of the [MessageBody] and [MessageHeader] attributes,
Looking at the WCF Layers “Inside”
• Protocol: Defines information to be used in the binding such as security, transaction capability, or reliable messaging capability. This information is provided by the protocol stack. •
Transport: Defines the base protocol to be used in communication.
• Encoding: Defines the encoding to be used by messages during communication. such as Message Transmission Optimization Mechanism (MTOM) or are optimized for reading (binary) or readable (text).
WCf Layers • The service model layer is the layer you will use primarily to program against. The service model layer influences the layer underneath it, which is called the messaging layer. • The messaging layer is the layer where all the actual transportation of messages across the channels on the network becomes reality. • The reason for the separation is an architectural concept. It allows you to separate the actual messaging from the programming model.
WCf Layers
• Inside the Message Layer. A channel allows you to clearly separate the combination of the transport and the messaging exchange pattern.
Channels
•
The address and binding together manifest themselves in the messaging layer of WCF.
•
The address expresses where the message should go, and the binding is the model you use to manipulate the message.
The binding is the mechanism to control the channel stack. When you pick one of the predefined bindings mentioned earlier, you are picking, in essence, specific channels The channels are responsible for transferring a message from the client side to the service side, and vice versa.
Shapes of Channels
•
Channels come in three shapes that correspond to the messaging exchange patterns
•
Channels are a way for WCF to separate these messaging exchange patterns from the transport.
•
WCF implements only the messaging exchange patterns that are natural for the transport. This is so that when using the WCF programming model, you don’t need to bother about the transports directly; rather, you can think about the messaging exchange pattern or channel shape.
What Is the Service Model Layer?
•
The service model layer offers an easy-to-use API with classes, methods, attributes, and configuration to build connected applications.
•
You can apply multiple behaviors on the client and service sides. Behaviors don’t influence the contract in any way; in other words, consumers of services don’t know the details about the behavior of the service.
•
Behaviors influence the conversion from messages to .NET types, instancing, throttling, concurrency, and error handling.
Using ServiceHost and ChannelFactory •
ServiceHost gives you access to the WCF hosting infrastructure on the server side whereas ChannelFactory gives you access to the WCF hosting infrastructure on the client side.
Service Host: • When a ServiceHost instance is available, you can do anything you like programmatically in regards to the ABCs and behavior
Just for information for Customization if any?
Using ChannelFactory •
The client knows only about the exposed contract of the service, not about its implementation. Therefore, in this case, the generic that is passed to ChannelFactory is the interface of the contract.
Using ChannelFactory
•
There is an easier way to consume services without using the ChannelFactory. You can generate the proxies using the SvcUtil.exe utility. The SvcUtil.exe utility retrieves the metadata (WSDL) of the service, and based on that it will generate the proxy classes that can be used to call the service.
Comes from service Model layer
Service Description and Service Runtime •
ServiceDescription is an in-memory representation of the environment where your service lives. It is either based on initialization code or based on a configuration file according to your wishes.
•
The service runtime is an abstraction layer on top of the messaging layer. This layer is the bridge between your application code and the channels in the messaging layer.
Service model runtime
Service Runtime
•
The service runtime has typed proxies on the client side and dispatchers on the service side. Typed proxies and dispatchers are responsible for handing over messages from the service runtime to the messaging layer and the other way around.
The typed proxy offers methods that are useful to your applications, and it transforms the method calls into WCF messages and hands them over to the messaging layer to transmit them to the service.
The dispatcher is used on the service side to handle the messages coming in on the channels and is responsible for sending them over to your application code.
For every method in your service contract of your service, you have one proxy operation. The operations share one proxy behavior.
The proxy behavior deals with all the messages flowing between the channel layer and your application.
On the proxy behavior level, you can make interceptions to deal with parameter interception, serialization, formatting, the mapping to real methods, and so on.
Applying Behaviors •
Influencing the internals of a service, behaviors are applied at the servicelevel, operation level, contract level, and endpoint level. WCF comes with several behaviors out of the box.
•
With behaviors, it is possible to modify the runtime and eventually influence the internals of each type of concept where it applies (for example, endpoint, operation, contract, and service).
•
From a programming model perspective, you can apply behaviors through configuration or by using the attributes available to you.
•
Behaviors are something consumers of services have no direct notion of, and they are not expressed in the contract.
Behavior Interface
The end