Click here to Skip to main content
Click here to Skip to main content

Introduction of Window Communication Foundation

, 4 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Explains basic information and steps for creating new WCF Service

About This

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”.

Why WCF?

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.

Term Definition
address 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.
application endpoint An endpoint exposed by the application and that corresponds to a service contract implemented by the application.
behavior 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.
binding 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.
contract 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.
data contract 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.
endpoint Consists of an address, a binding, and a contract used for communicating with a WCF service.
endpoint address Enables you to create unique endpoint addresses for each endpoint in a service, or under certain conditions share an address across endpoints.
operation contract 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.
service contract 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.

Step 1

Create a new WCF project using VS IDE.

Language Visual C# or Visual Basic
Target Framework .NET Framework 4
Installed Templates WCF
Template WCF Service Library
Name WCFPersonService

Step 2

Delete the existing .cs files (IService.cs & Service.cs) and Create new two files IPersonService & PersonService.

IPersonService.cs has the following method declarations:

Person CreatePerson(Person createPerson); //Method will create a new person
List <Person> GetAllPerson(); 	     //Method will display all the persons in the list
Person GetAPerson(string id); 	     //Method will display a person
Person UpdatePerson(string id, Person updatePerson); //Method will update a person
void DeletePerson(string id); 	     //Method will delete a person

These codes are similar to that of normal Web Services, in the WCF application IPersonService interface class and methods are decorated with [ServiceContract] and [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 [ServiceContract].

Sample template code created by the IDE is:

[ServiceContract]
public interface IPersonService
{
    [OperationContract]
    Person CreatePerson(Person createPerson);
    [OperationContract]
    List<Person> GetAllPerson();
    [OperationContract]
    Person GetAPerson(string id);
    [OperationContract]
    Person UpdatePerson(string id, Person updatePerson);
    [OperationContract]
    void DeletePerson(string id);
}

We can write class and its method as like Web Services, but we have to decorate with class & method using [ServiceContract] & [OperationContract]

How to Test WCF?

Step 1

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).

WCFTestClient.exe http://localhost/demo/mex

Step 2

Testing method CreatePerson()

  • 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 Address, a 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

Step 1

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:

  1. Mex
    • Address=”mex”
    • Binding=”mexHttpBinding”
    • Contract=”IMetadataExchange”
  2. Basic
    • Address=”basic”
    • Binding=”basicHttpBinding”
    • Contract=”WCFPersonService.IPersonService”
  3. WS
    • Address=”ws”
    • Binding=”wsHttpBinding”
    • Contract=”WCFPersonService.IPersonService”

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); // This line creates a person using
		// WCF application & returns that person to the client application

Please refer to the attached project for detailed code.

Conclusion

I hope this article helps you to understand the basics of WCF applications. Please feel free to comment.

References

  1. http://msdn.microsoft.com/en-us/library/dd560536.aspx
  2. http://msdn.microsoft.com/en-us/magazine/cc163647.aspx
  3. http://msdn.microsoft.com/en-us/library/ee958158.aspx

History

  • 2nd September, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arthanarieaswaran
Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.
 
Windows Phone
Microsoft Developer Network
 
Published Tools in Visual Studio Galleries
 
Published Source Codes in CodePlex
 
Microsoft Virtual Academy Profile
 
Published Articles in Code Project
 
Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

Comments and Discussions

 
QuestionQuestion PinmemberAnubhavSrivastavaScitm12-Oct-12 14:02 
AnswerRe: Question PinmemberArthanarieaswaran16-Oct-12 20:26 
GeneralMy vote of 3 PinmemberAbhay Malviya10-Apr-12 0:01 
GeneralRe: My vote of 3 PinmemberArthanarieaswaran17-Apr-12 18:56 
SuggestionReasonable "intro", needs some changes PinmemberGary Noter12-Sep-11 10:07 
GeneralMy vote of 3 Pinmembermaq_rohit4-Sep-11 7:20 
GeneralRe: My vote of 3 PinmemberArthanarieaswaran4-Sep-11 18:29 
Thanks maq_robit,
 
Sure I will write more on this and let you know.
GeneralRe: My vote of 3 PinmemberArthanarieaswaran18-Sep-11 18:49 
GeneralMy vote of 3 PinmemberSyed Wayez Ahmed4-Sep-11 2:37 
GeneralRe: My vote of 3 PinmemberArthanarieaswaran4-Sep-11 18:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 4 Sep 2011
Article Copyright 2011 by Arthanarieaswaran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid