Contracts in WCF Service
- Service contracts
- Data Contracts
- Message contracts
- Fault Contract
- Operation Contract
Service contracts
- [ServiceContract]
- public interface IService1
- {
- // TODO: Add your service operations here
- }
- Any WCF service can have more than one service contract.
- Declare at least one service contract in a service.
- A service contract can be declared using the [ServiceContract] attribute.
- It allows defining an Operation Contract under it to expose the service outside the world.
- It maps the interface and methods of your service to a platform-independent description.
- It describes message exchange patterns that the service can have with another party. Some service operations might be one-way, others might require a request-reply pattern.
- CallbackContract
- ConfigurationName
- HasProtectionLevel
- Name
- Namespace
- ProtectionLevel
- SessionMode
- TypeId
Operation Contract
- public interface IService1
- {
- [OperationContract]
- string GetData(int value);
- [OperationContract]
- CompositeType GetDataUsingDataContract(CompositeType composite);
- }
- CallbackContract: Gets or sets the type of callback contract when the contract is a duplex contract.
- ConfigurationName: Gets or sets the name used to locate the service in an application configuration file.
- HasProtectionLevel: Gets a value that indicates whether the member has a protection level assigned.
- Name: Gets or sets the name of the <portType> element in the Web Services Description Language (WSDL).
- Namespace: Gets or sets the namespace of the <portType> element in the Web Services Description Language (WSDL).
- ProtectionLevel: Specifies whether the binding for the contract must support the value of the ProtectionLevel property.
- SessionMode: Gets or sets whether sessions are allowed, not allowed or required.
- TypeId: When implemented in a derived class, gets a unique identifier for this Attribute.
Data Contract
- [DataContract]
- public class Student
- {
- private string _Name;
- private string _City;
- [DataMember]
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
- }
Fault Contract
- [ServiceContract]
- public interface IGetDetailsService
- {
- [OperationContract]
- [FaultContract(typeof(Student))]
- Student GetDetails(string Name);
- }
- [DataContract]
- public class Student
- {
- private string _Name;
- private string _City;
- [DataMember]
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
- [DataMember]
- public string City
- {
- get { return _City; }
- set { _City = value; }
- }
- }
Message contracts
- [MessageContract]
- public class Person
- {
- [MessageHeader] public Operation Name;
- [MessageHeader] public string city;
- [MessageBodyMember] private Home Address;
- [MessageBodyMember] private Home Streat;
- [MessageBodyMember] public int age;
- }
- Introduction to WCF Service
- Creating WCF Service
- End Points in WCF
- Ajax Enabled WCF Service
- Binding In WCF Service
- Consuming WCF Service in Web Application