Last updated: December 12, 2018
WCF Programming: Create Service-based Windows Applications
Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
Windows Communication Foundation (WCF) is a runtime environment and a set of APIs in the .NET Framework used in the creation of service-oriented, connected applications. WCF is Microsoft’s unified programming model for building service-oriented applications.
The increased popularity of service-oriented communication and applications over the past decade had a profound influence on software development. Applications rely on various services for interaction have become the norm. On Microsoft’s Windows development platform, these changes were made possible by the Windows Communication Platform (WCF).
WCF was first introduced as a part of the .NET Framework 3.0 in 2006, and further updated in .NET Framework versions 3.5 and 4.0. The latest version of the WCF is available as a part of .NET Framework version 4.5.
Windows Communication Foundation is used in much of the software based on the .NET Framework.
WCF is a tool for developers creating service-oriented applications, relying on web services to send and receive data. Web services have a general advantage of being loosely-coupled, instead of being specifically coded for each application. In other words, any application created on any platform can connect to any service.
WCF implements modern industry standards for web service interoperability, such as HTTP, SOAP, XML, WS-Addressing, W3C Web Services Addressing, and many more. Messages (or should we say data) can be exchanged in several different patterns.
The most common communication pattern is request-reply, but WCF supports other patterns as well, like one-way messages or the duplex exchange pattern. Messages can be encrypted for data protection, and user authentication can be enforced. Security is implemented using industry standard solutions like SSL or WS-SecureConversation. Messages can be transported using different protocols and encodings, most commonly as text encoded SOAP messages over the HTTP. WCF also supports sending messages over TCP, MSMQ or named pipes, encoded as text or using an optimized binary using the MTOM standard. Custom transport protocols and encodings can also be created.
WCF supports reliable message exchange using sessions implemented over WS-ReliableMessaging and queued messages using MSMQ. Support for durable messaging is also built into WCF, preventing data loss caused by communication problems.
Extensive transaction support is available in WCF, using one of three transaction models: WS-AtomicTransactions, the APIs from the System.Transaction namespace, or the Microsoft Distributed Transaction Coordinator.
WCF is a very flexible platform, allowing for easy extensibility and integration with other Microsoft technologies, for example the Windows Workflow Foundation (WF), Microsoft BizTalk, and Silverlight.
WCF is, in fact, a generic communication mechanism used for setting up client-host communications between two parties. That is what makes it so useful, as you can configure service parameters such as transport protocol, security, encoding, and so on — without any modification to the client application code. You can also set up communication between services and clients written in different programming languages running on different platforms.
Creating a WCF service and a client in the Visual Studio IDE requires six steps:
- Defining a WCF Service Contract specifies the operations supported by a service. Contracts are created by defining a C++, C#, or Visual Basic interface, and each method in the interface must correspond to a specific service operation. Each interface must have the ServiceContractAttribute applied to it, and each operation must have the OperationContractAttribute applied, otherwise they will not be exposed.
- Implementing a Service Contract is done by creating a class that implements the user-defined interface for the service.
- Hosting and Running a WCF service consists of the following tasks: creating an URI instance for the base address of the service, hosting the service, adding an endpoint that exposes the service, enabling the metadata exchange, and opening the service host.
- Creating a Client includes generating a proxy to the service and a configuration file using the command-line Service Model Metadata Utility Tool.
- Configuring a client consists of specifying the endpoint that the client uses to access the service. An endpoint has an address, a binding and a contract, and each of these must be specified in the process of configuring the client.
- Using a client actually calls the service from the generated proxy and closes the client once the operation call is completed.
Using the WCF Service Library template, or the WCF Service Application template in Visual Studio can significantly simplify the steps mentioned above, as most of the tasks described will be done automatically.
Should I Learn and Use WCF?
In most cases it comes down to a specific task at hand: how it can be resolved, and what is the most efficient and future-proof solution? Maybe a simple example is the best answer to this question.
You are developing a point of sale software suite for a client with many dispersed sales locations, and you have to connect to the client’s existing core business SQL database. There are two likely solutions. The first is to set up a WAN and connect all of the client’s sales locations to the core database using routers or VPN connections.
The second solution is to simply implement a WCF client-server solution and access the database over the internet. In many scenarios, using WCF can be very helpful and economical.
If you work on developing client-server applications for the Microsoft platform, you should take a look at the following resources on WCF:
- The Microsoft Developer Network (MSDN) Windows Communication Foundation main page provides a full overview of WCF — including documentation, reference resources, guides, and tutorials.
- The Microsoft Developer Network (MSDN) WCF in Visual Studio section describes the WCF tools available in Visual Studio and provides short how-tos.
- The Microsoft Developer Network (MSDN) WCF Application Deployment Scenarios is a nice article showcasing different WCF usage scenarios and use-cases.
We selected a couple of widely most respected books about WCF:
- Programming WCF Services: Design and Build Maintainable Service-Oriented Systems (2015) by Juval Lowy and Michael Montgomery: this book provides unique insight, rather than documentation, to help you learn the topics and skills you need for building maintainable, extensible, and reusable WCF-based applications.
- WCF Multi-Layer Services Development with Entity Framework (2014) by Mike Liu: if you are a C#, VB.NET, or C++ developer and want to get started with WCF and Entity Framework, then this book is for you. This book will teach you all the essentials to develop WCF services using Entity Framework as the backend.
Since WCF is a Microsoft product, relying on the .NET Framework for creation of service-oriented software solutions, it has staying power. It’s not new, but it’s still relevant, and probably will be for years to come.
We already outlined a few instances in which WCF can make a big difference from a technical perspective. While it’s a Microsoft technology, WCF supports interoperability with most non-Windows systems, as long as they run Java.
Further Reading and Resources
We have more guides, tutorials, and infographics related to coding and development:
- Microsoft Visual Basic / Visual Studio: this is our basic primer on Visual Studio with a focus on Visual Basic.
- Introduction to ADO.NET: the main .NET system for interacting with databases.
- C# Resources: as one of the most popular languages in the .NET firmament, C# is very helpful to know.
What Code Should You Learn?
Confused about what programming language you should learn to code in? Check out our infographic, What Code Should You Learn? It not only discusses different aspects of the languages, it answers important questions such as, “How much money will I make programming Java for a living?”