Introduction to Windows Communication Foundation Services in Visual Studio

Visual Studio 2008 provides tools for working with Windows Communication Foundation (WCF), the Microsoft technology for creating distributed applications. This topic provides an introduction to WCF from a Visual Studio perspective.

What Is WCF?

Windows Communication Foundation (WCF) is a unified framework for creating secure, reliable, transacted, and interoperable distributed applications. In earlier versions of Visual Studio, there were several technologies that could be used for communicating between applications.

If you wanted to share information in a way that enabled it to be accessed from any platform, you would use a Web service (also known as an ASMX Web service). If you wanted to just move data between a client and server that was running on the Windows operating system, you would use .NET Remoting. If you wanted transacted communications, you would use Enterprise Services (DCOM), or if you wanted a queued model you would use Message Queuing (also known as MSMQ).

WCF brings together the functionality of all those technologies under a unified programming model. This simplifies the experience of developing distributed applications.

WCF Programming Model

The WCF Programming model is based on communication between two entities: a WCF service and a WCF client. The programming model is encapsulated in the System.ServiceModel namespace in the .NET Framework.

WCF Service

A WCF service is based on an interface that defines a contract between the service and the client. It is marked with a ServiceContractAttribute attribute, as shown in the following code:

<ServiceContract()> _
Public Interface IService1
    <OperationContract()> _
    Function GetData(ByVal Value As String) As String
End Interface

You define functions or methods that are exposed by a WCF service by marking them with a OperationContractAttribute attribute. In addition, you can expose serialized data by marking a composite type with a DataContractAttribute attribute. This enables data binding in a client.

After an interface and its methods are defined, they are encapsulated in a class that implements the interface. A single WCF service class can implement multiple service contracts.

A WCF service is exposed for consumption through what is known as an endpoint. The endpoint provides the only way to communicate with the service; you cannot access the service through a direct reference as you would with other classes.

An endpoint consists of an address, a binding, and a contract. The address defines where the service is located; this could be a URL, an FTP address, or a network or local path. A binding defines the way that you communicate with the service. WCF bindings provide a versatile model for specifying a protocol such as HTTP or FTP, a security mechanism such as Windows Authentication or user names and passwords, and much more. A contract includes the operations that are exposed by the WCF service class.

Multiple endpoints can be exposed for a single WCF service. This enables different clients to communicate with the same service in different ways. For example, a banking service might provide one endpoint for employees and another for external customers, each using a different address, binding, and/or contract.

WCF Client

A WCF client consists of a proxy that enables an application to communicate with a WCF service, and an endpoint that matches an endpoint defined for the service. The proxy is generated on the client side in the app.config file and includes information about the types and methods that are exposed by the service. For services that expose multiple endpoints, the client can select the one that best fits its needs, for example, to communicate over HTTP and use Windows Authentication.

After a WCF client has been created, you reference the service in your code just as you would any other object. For example, to call the GetData method shown earlier, you would write code that resembles the following:

Dim client As New ServiceReference.Service1Client
Dim returnString As String

returnString = client.GetData("Hello")
MsgBox(returnString)

WCF Tools in Visual Studio

Visual Studio 2008 provides tools to help you create both WCF services and WCF clients. For a walkthrough that demonstrates the tools, see Walkthrough: Creating and Accessing WCF Services.

Creating and Testing WCF Services

You can use the WCF Visual Studio templates as a foundation to quickly create your own service. You can then use WCF Service Auto Host and WCF Test Client to debug and test the service. These tools together provide a fast and convenient debug and testing cycle, and eliminate the requirement to commit to a hosting model at an early stage.

WCF Templates

WCF Visual Studio templates provide a basic class structure for service development. Several WCF templates are available in the Add New Project dialog box. These include WCF Service Library projects, WCF Service Web Sites, and WCF Service Item templates.

When you select a template, files are added for a service contract, a service implementation, and a service configuration. All necessary attributes are already added, creating a simple "Hello World" type of service, and you did not have to write any code. You will, of course, want to add code to provide functions and methods for your real world service, but the templates provide the basic foundation.

To learn more about WCF templates, see WCF Visual Studio Templates.

WCF Service Host

When you start the Visual Studio debugger (by pressing F5) for a WCF service project, the WCF Service Host tool is automatically started to host the service locally. WCF Service Host enumerates the services in a WCF service project, loads the project’s configuration, and instantiates a host for each service that it finds.

By using WCF Service Host, you can test a WCF service without writing extra code or committing to a specific host during development.

To learn more about WCF Service Host, see WCF Service Host (WcfSvcHost.exe).

WCF Test Client

The WCF Test Client tool enables you to input test parameters, submit that input to a WCF service, and view the response that the service sends back. It provides a convenient service testing experience when you combine it with WCF Service Host.

When you press F5 to debug a WCF service project, WCF Test Client opens and displays a list of service endpoints that are defined in the configuration file. You can test the parameters and start the service, and repeat this process to continuously test and validate your service.

To learn more about WCF Test Client, see WCF Test Client (WcfTestClient.exe).

Accessing WCF Services in Visual Studio

Visual Studio 2008 simplifies the task of creating WCF clients, automatically generating a proxy and an endpoint for services that you add by using the Add Service Reference dialog box. All necessary configuration information is added to the app.config file. Most of the time, all that you have to do is instantiate the service in order to use it.

The Add Service Reference dialog box enables you to enter the address for a service or to search for a service that is defined in your solution. The dialog box returns a list of services and the operations provided by those services. It also enables you to define the namespace by which you will reference the services in code.

The Configure Service References dialog box enables you to customize the configuration for a service. You can change the address for a service, specify access level, asynchronous behavior, and message contract types, and configure type reuse.

To learn more about how to use WCF services, see Using WCF Services in Visual Studio.

See Also

Tasks

Walkthrough: Creating and Accessing WCF Services

Other Resources

Using WCF Services in Visual Studio

Using the WCF Development Tools

Windows Communication Foundation Services and ADO.NET Data Services