Windows Communication Foundation

  1. What are the the primary library references required for WCF services?
    • System.ServiceModel
    • System.Runtime.Serialization
  2. Why do we need System.ServiceModel namespace?
    System.ServiceModel namespace contains the classes that WCF needs to define services and their operations.
  3. Why do we need System.Runtime.Serialization namespace?
    WCF needs classes defined in System.Runtime.Serialization namespace to convert objects into a stream of data suitable for transmitting over the network (serialization) and to convert incoming stream of data into objects (deserialization).
  4. What kind of classes will you find in System.ServiceModel namespace?
    • You will find the following types of classes in System.ServiceModel namespace
    • 1. Basic binding classes (BasicHttpBinding, NetTcpBinding)
    • 2. Basic exception classes related to WCF
    • 3. Fault classes
    • 4. Base interfaces for WCF like IClientChannel 
    • 5. ServiceContractAttribute, OperationContractAttribute, MessageContractAttribute, DataContractFormatAttribute etc.
  5. What of Classes will you find in System.Runtime.Serialization namespace?
    You will find basic classes for serializing and deserializing objects.

    • 1. DataContractAttribute, DataContractSerializer
    • 2. Formatter
    • 3. Basic interface
    • 4. Serialization manager classes
  6. What is "Contract-First" approach?
    The structure of WCF service enables you to adopt a "contract-first" approach to development. When performing contract first approach you define the contract, or interface, that the service will implement and then you build your service that confirms to these contracts. 

    The point behind contract-first development is that you should focus on the design of your service before implementation.
  7. What is a "Data Contract"?
    A Data Contract is used to define the data that  will be communicated (sent and received) from the service. 

    A Data Contract is defined by using the "System.Runtime.Serialization.DataContractAttribute" on a class. The [DataContract] attribute specifies that the class can be serialized and deserialized by WCF.

    You can use the [DataContract] attribute on classes, structures, and enumerations.
  8. What is [DataMember] attribute?
    [DataMember] attribute is applied on each member of the [DataContract] to mark those data members that will be serialized. This can include complex data member i.e. objects based on custom defined classes. In this case, [DataContract] attribute must also be defined on each complex type.
  9. What is [ServiceContract] attribute?
    [ServiceContract] attribute is defined in System.ServiceModel.ServiceContractAttribute class. It is used to mark interfaces that are supposed to be used in WCF service implementations. 

    WCF runtime relies on the interface being tagged with this attribute when it is generating metadata for client applications that wish to use this service.
  10. What is [OperationContract] attribute?
    [OperationContract] attribute is defined in System.ServiceModel.OperationContractAttribute class. This attribute is used to mark methods of the service that will be exposed to client applications. 

    Any method marked with [OperationContract] attribute must only take parameters or return values that are marked with [DataContract] attribute.
  11. An example of [DataContract] and [DataMember] attributes:
    • namespace MyTypes
    • {
    •     [DataContract]
    •     public class PurchaseOrder
    •     {
    •         private int poId_value;

            // Apply the DataMemberAttribute to the property.

    •         [DataMember]
    •         public int PurchaseOrderId
    •         {

    •             get { return poId_value; }
    •             set { poId_value = value; }
    •         }
    •     }
    • }
  12. An example of [ServiceContract] and [OperationContract] attribute:
    • [ServiceContract]
    • public interface ISampleInterface
    • {
    •     // No data contract is requred since both the parameter
    •     // and return types are primitive types.
    •     [OperationContract]
    •     double SquareRoot(int root);

    •     // No Data Contract required because both parameter and return
    •     // types are marked with the                SerializableAttribute attribute.
    •     [OperationContract]
    •     System.Drawing.Bitmap GetPicture(System.Uri pictureUri);

    •     // The MyTypes.PurchaseOrder is a complex type, and thus
    •     // requires a data contract.
    •     [OperationContract]
    •     bool ApprovePurchaseOrder(MyTypes.PurchaseOrder po);
    • }
  13. Things to remember while implementing a service contract.
    When you have defined the shape of your service and the data that will be communicated then remember these points

    • 1. Service implementation must implement all methods defined in the service contract.
    • 2. Any additional method defined in the implementation will not be exposed by WCF.
    • 3. All complex types being taken or returned should be marked as [DataContract].
  14. How can WCF services be exposed to the clients?
    We can host a WCF service in a number of environments including the following

    • 1. ASP.Net Development Server (during development)
    • 2. IIS
    • 3. Custom stand-alone application
    • 4. Windows Service Application
    • 5. Windows Azure

    When hosting a service in WCF you also need to provide information on the location where the service will listen for incoming requests.
  15. What is the .svc file?
    A .svc file specifies the the name and location of the class that implements the service contract. Every service is exposed as a .svc resource to the clients (This is specific to IIS and ASP.NET Development Server, other environments have different means). This .svc file can either be physical or it can be defined in the configuration.
  16. What is the purpose and structure of <system.serviceModel> section in the configuration file?
    • the <system.serviceModel> configuration section is used to define the following
    • 1. Endpoints to which the service listens for incoming client requests
    • 2. Security requirements of the service
    • 3. Policy requirements of the service
    • 4. Behavior of the service
  17. What is Configuration based activation?
    The .svc file is a special content file that IIS uses to recognize a WCF service. It provides the information that the hosting environment in IIS uses to activate the WCF runtime and start the service. The .svc file always forms part of the address of a WCF service hosted by using IIS (this is not the case with other hosting environments).

    WCF 4.0 provides a feature called Configuration-Based Activation, with which you can combine the information normally included in the .svc file directly into the Web.configfile for a WCF service hosted by IIS. To do this, you add a <serviceActivations> section to the <serviceHostingEnvironment> part of the configuration file and provide values for the relative Address and service elements. The relative Address element should be a string that looks like a file name with the .svc extension, and the service element should specify the fully qualified type that implements the WCF service. The following code fragment shows an example that configures the ProductsService WCF service:

    • <?xml version="1.0"?>
    •   <configuration>...  
    •     <system.serviceModel>...    
    •       <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    •       <serviceActivations>
    •         <add relativeAddress="NoServiceFile.svc" service="Products.ProductsServiceImpl" /> <
    •         /servicaActivations>
    •       </serviceHostingEnvironment>  
    •     </system.serviceModel>...</configuration>
  18. Can you use the LINQ to SQL entities as DataContract in your WCF application? How come?
    Yes you can use LINQ to SQL entities as DataContract. This is possible because by default these entities are Serializable. However, since persistent objects created based on these classes contain proxies in the form of "EntitySet<T>" and "EntityRef<T>". You need to either increase the response payload enormously or you need to set these references to null when sending this data back.

    [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]


    • <behaviors>
    •   <serviceBehaviors>
    •     <behavior name="LargeServiceBehavior">     
    •       <dataContractSerializer maxItemsInObjectGraph="100000"/>
    •     </behavior>
    •   </serviceBehaviors>
    • </behaviors>
  19. What is a Host Application?
    A service is simply an object that exposes its operations to consumers of that service. But it needs a Hosting environment that will do the plumbing work of

    • 1. Starting and stopping the service
    • 2. Listening for incoming client requests and directing them to the service
    • 3. Sending response back to clients
  20. What are Service Endpoints?
    A host application makes a service available to client applications by providing one or more endpoints to which clients can send requests. An endpoint contains three pieces of information:

    • 1. The address of the service
    • 2. The binding supported by the service
    • 3. The contract implemented by the service
  21. What does the address part of the Service Endpoint define?
    The form of a service address depends on several factors,including the transport protocol being used. Different transport mechanisms use different address spaces. For example, in Chapter 1, “Introducing Windows Communication Foundation,” you deployed a service to IIS using the address http://localhost/ProductsService/ProductsService.svc. This address specifies the virtual directory and the service definition (.svc) file. If you build your own custom host application, you can use a different transport mechanism, and you must specify an address that is appropriate to your chosen transport mechanism.
  22. What does the binding part of the Service Endpoint define?
    The binding for a service describes how a client can connect to the service and the format of the data expected by the service. A binding can include the following information:

    • 1. The transport protocol
    • 2. The encoding format of messages
    • 3. The security requirements of the service
    • 4. The transcactional requirements of the service
    • 5. The reliability of communication with the service
  23. What is the relationship between the bindings of a service and the channel stack?
    The host application must ensure that messages being sent between the client and service conform to the security, reliability, and transactional requirements of the binding being used. The WCF runtime environment for a client application and a service provides a collection of channel objects that can perform this processing.

    A channel is responsible for handling one aspect of message processing, as specified by the bindings of a service. For example, a transport channel manages communications by using a specific transport protocol, and a transaction channel controls the transactional integrity of a conversation. The WCF runtime provides built-in channels for each of the supported transport protocols. The WCF runtime also provides channels that handle the different ways that WCF can encode data, manage security, implement reliability, and perform transactions. The WCF runtime composes channels into a channel stack. All messages passing between the client and the service go through each channel in the channel stack. Each channel in the channel stack transforms the message in some way, and the output from one channel is passed as input to the next. The channel stack operates in two directions: messages received from clients across the network proceed up the channel stack to the service, and response messages sent back from the service traverse the channel stack in the opposite direction back to the network and then to the client. If a channel cannot process a message, it reports an error, an error message is sent back to the client, and the message is not processed any further.When you start a service running, the WCF runtime uses the endpoint information specified as part of the service configuration and creates a listener object for each address specified for the service. When an incoming request is received, the WCF runtime constructs a channel stack by using the binding information specified for the address and routes the incoming data from the client through the stack. If a message successfully traverses all the channels in the channel stack, the transformed request is passed to an instance of the service for processing.
  24. What is the normal order of channel objects in the channel stack?
    A transport channel always resides at the bottom of the stack and is the first channel to receive data from the network. On top of the transport channel will be an encoding channel. These two channels are mandatory; the remaining channels in a stack are optional.
  25. What is InstanceContext object?
    a WCF service must be able to handle requests from multiple client applications simultaneously. To do this, the WCF runtime can create multiple concurrent instances of a service. The WCF runtime creates an InstanceContext object to control the interactionbetween the channel stack and a service instance. You can modify the way in which the WCF runtime instantiates a service instance through the Instance Context object by specifying the ServiceBehavior attribute of the class that is implementing the service contract.

    • 1. InstanceContextMode.PerCall
    • 2. InstanceContextMode.PerSession
    • 3. InstanceContextMode.Single
  26. What is the use of Windows Process Activation Service (WAS)?
    The Windows Process Activation Service (WAS) extends the functionality of IIS by removing the dependency on the HTTP protocol. Using WAS, you can host services that make use of other protocols, such as TCP, named pipes, and Microsoft Message Queues. WAS can listen for requests and activate a service that is waiting on an address that is based on any of these protocols. The important point to understand as far as a WCF service is concerned is that, for the most part, the protocol and the address are merely configuration details. The service contract, data contract, and service implementation are largely independent of the protocol and the host environment.
  27. What is the procedure of Installing WAS?
    • 1. Control Panel > Programs and Features > Turn Windows Features on / off
    • 2. Check on the Windows Process Activation Service List
    • 3. Check on all features in .Net Framework 3.5 features

    There is a chance that you might have to reinstall again using aspnet_regiis -i.
  28. How to programmatic host a WCF service in a Custom Host Application?
    ServiceHost host = new ServiceHost(typeof(ServiceImplementationClass), new Uri("http://localhost:8080/Service/"), new Uri("net.tcp://localhost:8081/Service/"));


    System.Threading.Thread.Sleep(60 * 60 * 1000);


    You can provide endpoint information programmatically but it is better to do this in  the configuration file.
  29. What are the addresses/Uri that you specify in ServiceHost constructor?
    These are the base addresses for the service and the WCF runtime combines these base addresses and the relative address of the service to form the complete address of the endpoint.
  30. Important tools of the trade
    • 1. WcfTestClient.exe (Test Client)
    • 2. SvcConfigEditor.exe (Configuration Helper)
    • 3. WcfSvcHost.exe (Test Hosting Environment)
Card Set
Windows Communication Foundation
This card set is about Windows Communication Foundation