A lot has changed in the world of web services. This series is to get you on the right track to building incredible web services using a ridiculously easy model in the .NET 3.0 Framework.
WCF Contracts
When we build web services, we are typically working with 3 types of 'contracts'. First, we have the services we talk to. Second we have operations on all of these services to which we use the objects and messages to communicate workflow in a data-driven way. WCF creates a great analogy for each of these with contracts.
Service Contracts
Service contracts are what we use to expose an actual web service to the public. The difference in WCF is that we define those services, first, through an interface.
using System.ServiceModel;
namespace WinOrFail
{
[ServiceContract]
public interface IWantToWin
{
}
}
There are plenty of ways to define out your ServiceContract just from the attribute itself. We'll work on those later though. Adding to the contract model, are operation contracts.
Operation Contracts
The operations are pretty easy to define, just like the service contract, we place them within the interface.
using System.ServiceModel;
namespace WinOrFail
{
[ServiceContract]
public interface IWantToWin
{
[OperationContract]
bool IsThisPossible();
}
}
The last of the contracts are the models we define, additionally, when working with the services - we call these
Data Contracts
Data contracts have additional parameters that we can add to further define the data within it. We use the DataContract attribute to expose it for those leveraging it, and the DataMember to expose individual properties and fields. If we have additional values, such as enumerations, we can use the EnumMember for those.
using System.Runtime.Serialization;
using System.ServiceModel;
namespace WinOrFail
{
[ServiceContract]
public interface IWantToWin
{
[OperationContract]
bool IsThisPossible();
}
[DataContract]
public class Money
{
[DataMember]
public double Amount { get; set; }
[DataMember]
public Currency Currency { get; set; }
}
[DataContract]
public enum Currency
{
[EnumMember]
US,
[EnumMember]
Euro,
[EnumMember]
Peso
}
}
There you have it! The contracts used for all WCF Services :) When building WCF services it is generally best to put these in a library that you can build just for the contract definitions. It helps you separate your code concerns.
Enjoy :)