This article will give you a brief description of WCF service contract attributes like namespace, callbackcontract, protectionlevel, configuration name, session mode. This will help you to design a WCF service contracts in a more efficient way. WCF service contract is included in System.Servicemodel namespace.
Service Contracts are behavioral contracts as defining the behavior of service. You will have to include the reference to System.Servicemodel and use [ServiceContract] with interface or class. It is a good practice to create a service contract with the interface as the interface creates a contract model for the client.
This is a string property of ServiceContract that allows you to set the name of the service contract. The default name of your service is your class or interface name. If you are using [ServiceContract] with the interface the prefix "I" also will include your service name. So It is always better to use some meaningful name for your service. The WSDL will include your service name as PortType.
[ServiceContract(Name="Product")]
public interface IProductService
{
}
Callback contracts of ServiceContract are used in the duplex message channel where clients and servers can call each other. (Read more on WCF MEP ) In the duplex model client send a request to the server it might be time consuming operation and then the service needs to provide notification to the client through callback contracts. If you use callbackcontract you will lose the service interoperability.
Also, you need to consider that not all bindings support callback contracts like the bindings which depend on HTTP protocols. HTTP protocols are stateless whereas the duplex channel requires a live connection to complete the operation.
[ServiceContract]
interface IOrdersCallback
{
[OperationContract(IsOneWay = true)]
void OnOrderPlaced(string message, DateTime timestamp);
}
[ServiceContract(CallbackContract = typeof(IOrdersCallback))]
public interface IOrders
{
[OperationContract]
bool OrderPlace(Order order);
}
By using ConfigurationName property of ServiceContract you can set the service name in the service config file. It specifies the name of the config element for service.
[ServiceContract(Name="Orders", ConfigurationName="OrderService")]
public interface IOrders
{
[OperationContract]
void PlaceOrder(Order order);
}
In the config file
<service name="OrderService">
<endpoint
address="http://localhost:8080/OrderService" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration" contract="IOrders">
</service>
It is a read only boolean property of ServiceContract. If the ProtectionLevel property is set to other than "None", HasProtectionLevel property will return true else return false.
ProtectionLevel of ServiceContract allows you to define the protection level for all messages on the wire. There are three different options for ProtectionLevel enum available.
[ServiceContract(ProtectionLevel = ProtectionLevel.Sign)]
public interface IOrderService
{
[OperationContract()]
bool PlaceOrder();
}
Whatever value you set for ServiceContract.ProtectionLevel same value will be applied to all OperationContract and FaultContract . If you explicitly set different values to operation contract an exception will be thrown.
If you are having multiple services with similar method signatures adding a unique namespace value to the service contract will help you as your methods will become unique by using this feature. This property is very important while serialization and deserialization as you might have similar class names on the client-side and service side.
[ServiceContract(Namespace="http://northwind.com/orderservice") ]
public interface IOrderService
{
[OperationContract(Name="GetOrder")]
Order GetOrder(int orderID);
}
You must set the same value to bindingNamespace in app.config for service.
This allows you to decide how the session should work for your service. Only bindings with WS-*, NetTcpBinding, and NetNamedPipeBinding allows sessions. There are three ways to handle sessions.
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IOrders
{
}
good to understand wcf service contract attribute