Get Familiar With Windows Communication Application Development
By Certification Magazine —
1 | 2 | 3 | 4 | 5 |
These questions are based on 70-503 TS: Microsoft .NET Framework 3.5 - Windows Communication Foundation Application Development (C#)
A Self Test Software Practice Test
Sub-objective: Define service contracts.
Single answer, multiple-choice
You are using version 3.5 of the Microsoft .NET Framework to develop a Windows Communication Foundation (WCF) service. You have defined the following class for employees to handle purchase orders:
public class OrderService {
public void SubmitOrder ( int OrderID ) {
//Implementation omitted
}
public void ApproveOrder ( int OrderID, bool Approved ) {
//Implementation omitted
}
public string RetrieveOrder ( int OrderID ) {
//Implementation omitted
}
}
You want to expose the OrderService class as a WCF service. The OrderService class must support multiple inheritance and built-in contract versioning and backward compatibility features. What should you do?
A. Apply the ServiceContract attribute to the OrderService class, and apply the OperationContract attribute on each method.
B. Have the OrderService class inherit from the ServiceContract class, and add the static keyword to each method declaration.
C. Have the OrderService class implement the IServiceContract interface, and add the static keyword to each method declaration.
D. Create a new interface, apply the ServiceContract attribute to the interface and the OperationContract attribute on each of its methods, and have the OrderService class implement the interface.
Answer:
D. Create a new interface, apply the ServiceContract attribute to the interface and the OperationContract attribute on each of its methods, and have the OrderService class implement the interface.
Tutorial:
To expose the OrderService class as a WCF service, you should use the ServiceContract and OperationContract attributes in a new interface and have the OrderService class implement that interface. The following code demonstrates this technique:
public class OrderService : IOrderService {
public void SubmitOrder ( int OrderID ) {
//Implementation omitted
}
public void ApproveOrder ( int OrderID, bool Approved ) {
//Implementation omitted
}
public string RetrieveOrder ( int OrderID ) {
//Implementation omitted
}
}
[ServiceContract]
public interface IOrderService {
[OperationContract]
void SubmitOrder ( int OrderID );
[OperationContract]
void ApproveOrder ( int OrderID, bool Approved );
[OperationContract]
string RetrieveOrder ( int OrderID );
}
In this scenario, you want to ensure the OrderService class supports multiple inheritance, built-in contract versioning and backward compatibility features. Therefore, you should create an interface and then have the OrderService class implement that interface.
You can create a service using any of the following methods:
- Define the contract as a public interface and then create a class that implements the interface. You need to apply the ServiceContract attribute on the interface and the OperationContract attribute on the methods in the interface.
- Create a class and apply the ServiceContract attribute on the class and the OperationContract attribute on the methods that should be made available to WCF clients.
There are many benefits of using an interface to implement the contract:
- A service contract interface can extend multiple service contract or other interfaces.
- Multiple service contracts can be fulfilled by a single class by implementing multiple service contract interfaces.
- The implementation of a service contract can be changed without impacting the service contract.
- Versioning of service can be done by implementing an old interface and a new interface. The old clients will connect to the original version; whereas the newer clients can connect to the newer version. Both contract versions could be implemented by same service class.
Using an interface to create a service contract is the preferred technique in most cases. However, you can mark the class directly with the ServiceContract attribute to create a service contract when you want to create it quickly and keep it simple. But the major disadvantage of this technique is you can only use a class to implement one service contract at a time.
A service is a program that receives messages and responds to these messages by executing code to perform predefined actions. The actions performed by a service are based on the contents of the message. A WCF service is a unit of functionality available to provide operations to clients. You create a WCF service by designing a class that implements WCF service contract. You should use the OperationContract, ServiceContract and DataContract attributes to create a service contract. A group of operations make up a service contract.
You can create a service operation by creating a public method and marking it with the OperationContract attribute. You can specify the unique action performed by the operation using the Action property of the OperationContract attribute. You can then group the operations to create a service contract.
The ServiceContract attribute is used to mark a service contract. You can specify the name of the service element in the configuration file to be used by using the ConfigurationName property of the ServiceContract attribute. You can also specify the name and namespace of the service contract using the Name and Namespace properties of the ServiceContract attribute respectively.
The DataContract attribute is used to serialize data that will be exchanged by service operations. You should explicitly apply the DataContract attribute on a type if it is not an intrinsic .NET type. By default, when a DataContract attribute is applied on a class, the class name is used as the local name of the contract. You can use the Name property of the DataContract attribute to override the default local name of the contract.




