Build Service Oriented Applications
The application in this scenario is connected. The leading approach to building connected applications is service orientation. No doubt, you have heard of the Service-Oriented Architecture (SOA), but what is a service-oriented application?
A service is a program that performs a task and that a client application can communicate with through well-defined messages. To call a Web or WCF service, the client es it a message, that consists of XML. The message will typically include a request for the service to do an action, such as retrieve or update the inventory information for a product. If the service returns data to the client then it es back a message, also consisting of XML.
The client and the service need to agree on what the XML will look like. The most common message format is Simple Object Access Protocol (SOAP). SOAP defines the format of a XML request and reply messages and how clients and services encode the data in the messages.
In object-oriented programming, objects are tightly coupled. To call the Inventory component, a client application creates an instance of the component and therefore, controls its lifetime. In a service-oriented application, services and clients are loosely coupled. To call the Inventory service, a client does not instantiate an instance of a component. It simply es a message to the service and may receive a message in return.
Service-oriented applications are loosely coupled. All communication occurs using messages. SOAP defines the format of messages. Contracts define the contents of messages. A service will publish a contract that specifies what methods clients can call, what arguments those methods take and what, if anything, the methods return. The contract can also specify security requirements and whether a transaction is required.
ABCs of WCF
Address: WCF services must have an address. The address specifies the location of the service that will be exposed for clients that will use it to communicate with the service. With the address, WCF can provided the protocols: HTTP, TCP, NamedPipe , Peer2Peer and MSMQ.
Binding: Specifies how a service is accessible. In other words, how the two parties will communicate in terms of transport (HTTP, TCP, NamedPipe, Peer2Peer, MSMQ or others) and the encoding (text, binary and so on).
Contract: List of methods that can be executed by a remote client.
Endpoints
A service created with WCF is hosted by the .NET framework. To expose services, we use configuration files and define Service-Endpoints. Service-Endpoints serve as a directive to the .NET framework specifying how a service should be exposed.
Address formats can be relative or absolute. There are ways to define default or base addresses in the configuration file. Am address will contain the protocol keyword in the beginning depending on the type of the Binding being used. For example, for bindings using the HTTP protocol, your address will start with "http://" and for binding using the TCP protocol, it will start with "net.tcp://".
Binding will typically start with ws or net. All bindings starting with ws will have a text encoded message format (incuding the basicHttpBinding) and the bindings starting with net will have binary encoded message formats.
Typical way to define Contracts
Contracts are usually defined via interfaces listing all the methods available to a remote client. However for distinguishing the interfaces meant to be contracts in WCF configuration, you must decorate the interface with the [ServiceContract] attribute.
A Service Contract Interface may contain the definitions of multiple methods and not all methods may be meant for a remote client in some scenarios (usually that will be rare though). So only those methods that have the [OperationContract] attribute on them will be available to the remote client.
Data is exchanged between a client and a server via Method arguments (from client to server) and returned data of a method (server to client). Most of the types in the .Net framework are serializable. However when exchanged data of a custom type (for example an object of the Employee class defined in your application), it must be serializable. So you must have the [DataContract] attribute on the class whose objects are transported between client and server. Again, like serialization, some data of your class is not meant to be serialized, so in that case, only the fields or automatic properties with the [DataMember] attribute on them will be serialized. For example, to provide a news Service where objects of NewsArticle may be exchanged between two applications, here is how to define it.
- [ServiceContract]
- public interface INewsService
- {
- [OperationContract]
- NewsArticle[] GetNewsForAllBrief();
- [OperationContract]
- string GetNewsDetails(int id);
- }
- [DataContract]
- public class NewsArticle
- {
- [DataMember]
- public int id { get; set; }
- [DataMember]
- public string HeadLine { get; set; }
- [DataMember]
- public string BodySummary { get; set; }
- [DataMember]
- public string Date { get; set; }
- public int AuthorId { get; set; }
- public bool Confidential { get; set; }
- public string BodyDetail { get; set; }
- }
The contract has two methods, one that gets all the news in brief (without the details and hence BodyDetail, AuthorId and Confidential properties in the NewsArticle class don't have the DataMember attribute on it) and the other that gets the details of a specific News identified via id by client.
The Service will be basically the class that provides the implementation of the preceding interface. That class may implement multiple contract interfaces. We will be seeing the class in our examples ahead.
HOSTING THE SERVICE
Let us create our first WCF example. In this example, we will host a service in IIS (but for testing purposes, we can do it under a location in the File System; that will work fine).
Create a new empty web site, named say "WCF-NewsService-IIS-Hosted". Add a new item of type "WCF Service". This will add IService.cs and Service.cs files to the "App_Code" folder. In the IService.cs file, delete the interface defined by default and replace it with the INewsService defined earlier in the document and also define the NewsArticle class in the same file. In the Service.cs file, replace the class with the following.

Pavel PoluninPosted Feb 17, 2022, 8:22 AM
I am very grateful to you for the experience! Thank you very much!
Shifa HazhifahPosted Jan 17, 2019, 4:07 AM
Thanks, very helpful
Pradeep YadavPosted Jul 4, 2017, 5:18 AM
Nice article Pramod!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Rohatash KumarPosted Aug 6, 2013, 3:49 AM
Great article Pramod. Can you define multiple endpoints in service.
Dinesh BeniwalPosted Aug 5, 2013, 10:38 PM
Appreciate your work, Welcome to the C# Corner.