I am writing this article to explain the fundamentals of WCF application. Here I have explained basic template provided by the VS 2010 IDE.
What is WCF?
Windows Communication Foundation (WCF) is a framework for building service-oriented applications (SOA). As its name suggests, WCF provides the .NET Framework with a basis for writing code to communicate across components, applications, and systems. It formerly named as “Indigo”.
WCF was designed according to the tenets of service orientation. A service is a piece of code you interact with through messages. Services are passive. They wait for incoming messages before doing any work. Clients are the initiators. Clients send messages to services to request work. Using WCF, we can send data as asynchronous messages from one application to other applications using below .NET Framework technologies.
How to Create WCF?
We will see how to create basic WCF applications using Visual Studio IDE. Before going into the application, see some of the basic terms used in the application.
|Specifies the location where messages are received. It is specified as a Uniform Resource Identifier (URI). The URI schema part names the transport mechanism to use to reach the address, such as HTTP and TCP. The hierarchical part of the URI contains a unique location whose format is dependent on the transport mechanism.|
|An endpoint exposed by the application and that corresponds to a service contract implemented by the application.|
|A behavior is a component that controls various run-time aspects of a service, an endpoint, a particular operation, or a client. Behaviors are grouped according to scope: common behaviors affect all endpoints globally, service behaviors affect only service-related aspects, endpoint behaviors affect only endpoint-related properties, and operation-level behaviors affect particular operations.|
|Defines which communication protocols are used to communicate with WCF services. It is constructed of a set of components called binding elements that stack one on top of the other to create the communication infrastructure. See endpoint.|
|A contract is a specification of support for the particular type of contract that it is. A service contract, for example, is a specification for a group of operations. In WCF, contracts have a hierarchy that is mirrored in the description objects located in the |
System.ServiceModel.Description namespace. A service contract is the largest contract scope in WCF. Each service operation in a service contract has an operation contract, which specifies the messages -- including fault messages -- the operation can exchange, and in which direction. Each message in an operation has a message contract, a specification for the structure of the SOAP message envelope, and each message contract has a data contract, which specifies the data structures contained in the messages.
|The data types a service uses must be described in metadata to enable others to interoperate with the service. The descriptions of the data types are known as the data contract, and the types can be used in any part of a message, for example, as parameters or return types. If the service is using only simple types, there is no need to explicitly use data contracts.|
|Consists of an address, a binding, and a contract used for communicating with a WCF service.|
|Enables you to create unique endpoint addresses for each endpoint in a service, or under certain conditions share an address across endpoints.|
|An operation contract defines the parameters and return type of an operation. When creating an interface that defines the service contract, you signify an operation contract by applying the |
T:System.ServiceModel.OperationContractAttribute attribute to each method definition that is part of the contract. The operations can be modeled as taking a single message and returning a single message, or as taking a set of types and returning a type. In the latter case, the system determines the format for the messages that are exchanged for that operation.
|The service contract ties together multiple related operations into a single functional unit. The contract can define service-level settings, such as the namespace of the service, a corresponding callback contract, and other such settings. In most cases, the contract is defined by creating an interface in the programming language of your choice and applying the |
T:System.ServiceModel.ServiceContractAttribute attribute to the interface. The actual service code results by implementing the interface.
Create a new WCF project using VS IDE.
|Language||Visual C# or Visual Basic|
|Target Framework||.NET Framework 4|
|Template||WCF Service Library|
Delete the existing .cs files (IService.cs & Service.cs) and Create new two files
IPersonService.cs has the following method declarations:
Person CreatePerson(Person createPerson); List <Person> GetAllPerson(); Person GetAPerson(string id); Person UpdatePerson(string id, Person updatePerson); void DeletePerson(string id);
These codes are similar to that of normal Web Services, in the WCF application
IPersonService interface class and methods are decorated with
[OperationContract] using System.ServiceModel namespace.
[ServiceContract] - A service contract specifies what an endpoint communicates to the outside world, i.e., only the class decorated with
[ServiceContract] is visible for WCF client application.
[OperationContract] - Indicates that a method defines an operation that is part of a service contract in an application, i.e., only the method decorated with
[OperationContract] is visible for WCF client application and class of this method must be decorated with
Sample template code created by the IDE is:
public interface IPersonService
Person CreatePerson(Person createPerson);
Person GetAPerson(string id);
Person UpdatePerson(string id, Person updatePerson);
void DeletePerson(string id);
We can write class and its method as like Web Services, but we have to decorate with class & method using
How to Test WCF?
You can run the application; it will run the “WCF Service Host” and run the WCF Test Client application.
Or you can run & test the application using template created by the IDE. To test the WCF application, you have to run WCFTestClient.exe which is in C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE with the application URL (like http://localhost/demo/mex).
- Double click the
“CreatePerson()” method in the WCT Test Client window
- Enter the values in the “Request” section
- Click “Invoke” button.
- Answer will populate in the “Responses” Section
Similarly, you can Invoke the methods
GetAllPersion(), GetAPerson(), UpdatePerson() & DeletePerson().
What is Endpoints in WCF?
An address associated with WCF service which is used to locate and identify the WCF service is known as endpoint.
Client applications (like Remoting, WSE, ASMX, MSMQ, and Enterprise Services) can communicate with WCF Services using these Endpoints.
Endpoint Address - All communication with a Windows Communication Foundation (WCF) service occurs through its endpoints. Each “ServiceEndpoint“ contains an
Binding, and a
Contract. It usually known as ABC.
Address – Specifies where to find the service
Binding – Specifies how to communicate with the service
Contact – Specifies which operations are available
Creating Service Endpoint
In the application Solution Explorer, right click the App.config file in the popup menu click “Edit WCF Configuration” item.
It pops up the WCF Configuration window.
There are three Endpoints in the configuration file, by save & close, these changes will reflect in the app.config file.
App.Cofig file has three Endpoints:
In these Endpoints, Mex endpoint is mandatory; it must have all the WCF application. Mex is exposing the Metadata to the client application.
Client can communicate using any of other two endpoints (Basic, WS).
How to Consume the WCF Service?
Add the WCF Service reference using Add Reference... menu in the console application, see the below code to consume the WCF services.
ServiceReference1.Service1Client sc= new ServiceReference1.Service1Client("basic");
Person varPerson = sc.CreatePerson(objPerson);
Please refer to the attached project for detailed code.
I hope this article helps you to understand the basics of WCF applications. Please feel free to comment.
- 2nd September, 2011: Initial version