Today I was surfing Microsoft E-Learning, I learned WCF has 5 contracts as follows:
1. Service contract
A service contract defines the operations that a service supports, and maps to a portType in Web Service Description Language (WSDL). Service contracts are implemented as .NET Framework interfaces that are annotated with the ServiceContract attribute.
[ServiceContract]
public interface IMyContract { ...}
2. Operation contracts
Operation contracts define the individual operations that a service supports and map to operations in WSDL. Operations are defined by adding methods to a Service Contract interface that is annotated with the OperationContract attribute.
[OperationContract]
void SomeOperation();
3. Data contracts
Data contracts define how complex types are serialized when they are used in WCF service operations. They are defined by applying the DataContract and DataMember attributes to classes.
[DataContract]
public class SomeType{ [DataMember] public int ID;}
4. Message contracts
Message contracts describe the entire SOAP message format. They can use data contracts and serializable types to emit schema for complex types, and they also make it possible to control the SOAP message headers and body explicitly, by using a single type. Message contracts provide a simple method to add custom SOAP headers to incoming and outgoing messages.
[MessageContract]
public class MyRequest {
[MessageHeader] public string field1;
[MessageBody] public string field2;
}
5. FaultContract
WCF service reports errors by using Fault objects. Fault contracts document the errors that WCF code is likely to produce, and WCF maps Fault objects to SOAP faults. Note that the type specified in the FaultContract does not have to be an exception, although it often will be.
[OperationContract]
[FaultContract(typeof(DivideByZeroException))]
void SomeMethod();
A Fault is generated by throwing a FaultException:
throw new FaultException<DivideByZeroException>(
The Process of Implementing a Service Contract
The following code example shows how to define a WCF service contract by using a .NET Framework interface. The ServiceContract attribute is applied to the interface, and each of the methods defined in the interface has the OperationContract attribute applied to it. Note that any methods that are not marked with OperationContract are not visible to clients.
[ServiceContract]
public interface IOrderService
{
[OperationContract]
void CreateOrder(int orderNumber);
[OperationContract]
void AddItemToOrder(int orderNumber, Item itm);
[OperationContract]
Order GetOrderDetails(int orderNumber);
}
The following code example shows how to implement a WCF service contract by using a .NET Framework class that implements a service contract interface. Note that the implementing class does not require any special attributes.
public class OrderService : IOrderService
{
void CreateOrder(int orderNumber)
{
// implementation details
}
void AddItemToOrder(int orderNumber, Item itm)
{
// implementation details
}
Order GetOrderDetails(int orderNumber)
{
// implementation details
}
}
The following code example shows how to implement a WCF data contract. The DataContract attribute is used to define a data contract class and DataMember attributes define the class members that take part in the contract.
[DataContract]
public class Order
{
[DataMember]
public int OrderNumber;
[DataMember]
public String ClientName;
...
}
Do you think it is a good interview question ?
No comments:
Post a Comment