Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Server

Windows Communication Foundation Primer : Defining the contract

3/22/2011 3:43:20 PM

What is WCF?

In a nutshell, WCF is a framework for building and hosting services. Hosted by the Microsoft platform, WCF services make use of standard technologies to offer a wide range of cross-platform security, transaction, and communication capabilities.

Before WCF came along, .NET developers, who built distributed applications had to choose between communication schemes such as ASP.NET web services, .NET remoting, and MSMQ. This choice carried with it implications for how the component was designed, developed, deployed, and consumed. If you went with ASP.NET web services, you were committing to XML message formats and were handcuffed by limitations of the HTTP transport protocol. If you chose .NET remoting, you were able to process messages in an efficient fashion, but immediately limited yourself to .NET-only service clients. MSMQ is wonderful for disconnected applications, but in choosing it, you've eliminated any chance at having a synchronous, request-response conversation with a software client.

The goal of WCF is to unify these many technologies and provide a single transport-neutral development paradigm with common aspects for security, transactions, and exception handling. The service is implemented independent of the communication protocol strategy. This is a fairly revolutionary concept that introduces immense flexibility to service designers. Instead of building services with tightly coupled and rigid endpoints that do not welcome change, we can design flexible services that are capable of supporting a wide range of current and future consumers.

The service endpoint is king, and endpoints in WCF are defined using the easy-to-remember ABC acronym. The letter A stands for addressing, which refers to the actual URL of the service. The letter B stands for binding, which describes how we communicate with the service. Finally, the letter C stands for contract, which defines the operations and data elements that this service exposes. Let's look at each of these in detail.

Defining the contract

Unlike ASP.NET web services, WCF truly promotes a "contract first" design style where developers need to thoughtfully consider how the outside world will interact with their service. There is a clean separation between the interface definition and the actual implementation of the service. When building ASP.NET services, the developer typically takes a code-first approach, where .NET classes are decorated with attributes and exposed as services. In the WCF model, we focus first on the data being shared and what our interface to the outside world should look like (i.e. the contract). Only after this critical step is complete does the WCF developer begin to design the actual service implementation logic.

There are actually three different contracts you may define for a WCF service. These are:

  • Service contract

  • Data contract

  • Fault contract

Service contracts

The service contract explains what your service can do. It's built using a .NET interface class and decorated with WCF attributes that identify it as a service contract. A basic service contract looks like this:

[ServiceContract()]
public interface IVendorContract
{
[OperationContract()]
void InsertVendor(string vendorId, string vendorName);
[OperationContract()]
bool DeleteVendor(string vendorId);
}

Notice that the interface has a ServiceContract attribute and each operation that we wish to expose publicly on our contract has an OperationContract attribute. Each of these metadata attributes has a series of optional parameters that let us explicitly define public characteristics of the service. For instance, we can add the Name and properties to the ServiceContract to better characterize this service in our environment. We can also add a series of properties to the OperationContract SOAPAction value is set to. Why give an alternate name to a service operation? Consider scenarios where you have an overloaded operation in your WCF service contract, and need each WSDL operation to have a unique public name. C# (and .NET) support overloading, but the WSDL standard no longer does. Namespace to control what the operation is named and the

[ServiceContract(Name="VendorService", Namespace="http://Seroter.BizTalkSOA/Contracts")]
public interface IVendorContract
{
[OperationContract(Name="InsertVendor")]

void InsertVendor(string vendorId, string vendorName);
[OperationContract(Name="InsertVendorWithContact")]

void InsertVendor(string vendorId, string vendorName, string vendorContactName);
[OperationContract(Name="DeleteVendor")]
bool DeleteVendor(string vendorId);
}


Data contracts

As you can probably imagine, services often need to accept and return comprehensive data entities in addition to simple type parameters. I might want to model a data entity such as a customer instead of having a service operation accept 15 individual string parameters. Complex data parameters are categorized as data contracts in WCF. A data contract is a .NET class object decorated with the DataContract attribute and whose public properties are flagged with DataMember attributes. Public service operation definitions can only include complex types identified as data contracts.

[DataContract()]
public class VendorType
{
private string vendorId;
private string vendorName;
private string vendorContactName;
[DataMember()]
public string VendorId
{
get { return vendorId; }
set { vendorId = value; }
}
[DataMember()]
public string VendorName
{
get { return vendorName; }
set { vendorName = value; }
}
[DataMember()]
public string VendorContactName
{
get { return vendorContactName; }
set { vendorContactName = value; }
}
}



Much like the service contract, the attributes of the data contract allow for more fine-grained control of the entity definition. For instance, we may provide a Name and Namespace to the DataContract, while also adding some useful node ordering and existence attributes to the member elements.

 [DataContract(Name="Vendor" Namespace = "http://Seroter.BizTalkSOA/Types")]

public class VendorType
{
private string vendorId;
private string vendorName;
private string vendorContactName;
[DataMember(IsRequired=true, Order=0)]

public string VendorId
{
get { return vendorId; }
set { vendorId = value; }
}
[DataMember(IsRequired=true, Order=1)]
public string VendorName
{
get { return vendorName; }
set { vendorName = value; }
}
[DataMember(IsRequired=false, Order=2)]
public string VendorContactName
{
get { return vendorContactName; }
set { vendorContactName = value; }
}
}


If you omit the Order property from the DataMember attribute, then the nodes are ordered alphabetically, which may not be how you wish to organize your public schema.

Other -----------------
- Implementing Edge Services for an Exchange 2010 Environment : Implementing Safelist Aggregation for Outlook 2003 and Outlook 2007
- Implementing Edge Services for an Exchange 2010 Environment : Using EdgeSync to Synchronize Active Directory Information to the Edge Transport Server
- Implementing Edge Services for an Exchange 2010 Environment : Using Address Rewriting to Standardize on Domain Address Naming for an Organization
- Application Development with SharePoint Designer 2010 and Visual Studio 2010 : Developing a Visual Web Part
- Using Visual Studio 2010 with SharePoint 2010
- Creating a Workflow-Based Application in SharePoint Designer 2010
- Windows Server 2003 : Authorizing Remote Access Connections (part 3) - Configuring Access Beyond the Remote Access Server
- Windows Server 2003 : Authorizing Remote Access Connections (part 2) - Understanding Remote Access Policies
- Windows Server 2003 : Authorizing Remote Access Connections (part 1) - Configuring Dial-In Properties of the User Account
- Windows Server 2008 R2 : Understanding Remote Desktop Services (part 5) - Single Sign-On & Remote Desktop Connection Display
- Windows Server 2008 R2 : Understanding Remote Desktop Services (part 4)
- Windows Server 2008 R2 : Understanding Remote Desktop Services (part 3) - RD Connection Broker & RD Licensing
- Windows Server 2008 R2 : Understanding Remote Desktop Services (part 2) - RD Gateway & RD Web Access
- Windows Server 2008 R2 : Understanding Remote Desktop Services (part 1) - RD Session Host & RD Virtualization Host
- Windows Server 2008 R2 : How Remote Desktop Works
- Installing Microsoft SharePoint Server 2010 and Configuring PerformancePoint Services : Installing SharePoint (part 2) - Running the Server Farm Installation for SharePoint
- Installing Microsoft SharePoint Server 2010 and Configuring PerformancePoint Services : Installing SharePoint (part 1)
- Installing Microsoft SharePoint Server 2010 and Configuring PerformancePoint Services : Examining PPS Installation Prerequisites
- PerformancePoint Services 2010 Architecture
- Building BizTalk Server 2009 Applications : Working with BizTalk orchestration
 
 
Most view of day
- SQL Server 2008 R2 : Configuring Resource Governor (part 1) - Enabling Resource Governor, Defining Resource Pools
- Evaluating Applications for Windows 7 Compatibility : Deploying XP Mode
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
- Windows Phone 8 : Configuring Basic Device Settings - Passwords and Screen Timeouts (part 4) - Disabling a Password
- Windows Server 2008 R2 : Creating and Administering Hyper-V Virtual Machines (part 2) - Installing the guest operating system
- Exchange Server 2007 : Migrating from Windows 2000 Server to Windows Server 2003 (part 4) - Replacing Existing Domain Controllers
- Windows Server 2012 Group Policies and Policy Management - Group Policy Processing: How Does It Work?
- Windows Phone 7 : The Silverlight Controls (part 3) - Line, Polyline, and Polygon Controls
- Repairing and Removing Programs : Changing and Repairing Programs
- Creating a Home Network : Setting Up a Wired Network with an ICS Host
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro