WCF Services—Contract First or Code First
|Visual C# Tutorials|
|.NET Framework Tutorials|
|© 2007 Chris Peiris, Dennis Mulder|
Contract First or Code First?
There have been lots of discussions in SOA communities regarding best practices for either "code first" or "contract first" service development. We won’t say one is better than the other. However, in a true "contract-first" paradigm, you’ll spend all the up-front time generating the schema (WSDL) in XML that supports your service contract implementation. Frankly, we’d rather listen to someone scratch their nails on a blackboard than start with a whole bunch of XML. Although WCF can support a contract- and schema-driven starting point, for the most part you’ll spend time adorning your types with attributes in a declarative model and allowing the WCF framework to generate the necessary schema and contract metadata.
The WCF programming model of "code first and then apply attributes to the interface" allows the WCF framework to do all the work of providing a standardized schema to publish to consumers of the service. This model works best in industries or organizations where you’re the only provider of the service or where you’re just working on internal solutions that require cross-application integration and you have full control over the interfaces.
|.NET ATTRIBUTES BACKGROUND|
| .NET uses attributes throughout the framework. Attributes permeate many aspects of how your types are hosted by the .NET CLR. Things such as Code Access Security (CAS) and general assembly metadata all depend upon attributes that are part of the generated MSIL. This is one of the declarative aspects of .NET.
The ASP.NET 2.0 web service model relies as well on class- and method-level attributes that control how the .NET Framework manages the runtime support of web services through the request-reply cycle. For the most part, the declarative model remains the same when moving to WCF.
According to the WCF product team, this was somewhat intentional to help the migration and transition of both existing ASP.NET 2.0 web services and developers to the WCF model. That initial transition started with the introduction of Web Services Enhancements (WSE) from version 1.0 up through version 3.0. In fact, the transition from out-of-the-box ASP.NET 2.0 web services to WSE web services required no change from a coding perspective. All WSE required was the modification of the application (Web.config) file that the site was hosted on, in addition to a recompile to bind to the updated assemblies that were part of the WSE distributions.
WCF supports schema-first and contract-first development as well. This might sound like a new model in the .NET world, but it was possible under ASP.NET 1.1 web services as well; it just required some discipline to follow and implement. The paradigm also existed in the COM world; but again, discipline was required and tool support was limited, and we all need to get things done, right?
WCF offers the tools and framework support to provide the malleability of an implementation and definition that allows service architects to view what they implement from either the outside in or the inside out. The primary tool you’ll use in WCF is
SvcUtil.exe. We’ll introduce how to use
SvcUtil.exe in this chapter.