Understand the ABC of an endpoint in WCF

← PrevNext →

WCF offers its services to its client using an endpoint. An endpoint comprises of three key elements, the address, binding and contract. WCF endpoints provide the necessary resources and directions, which help clients to communicate with the various services WCF offers.

Recommended for you
A Comprehensive WCF Tutorial for Beginners - Introduction to WCF

An endpoint completes WCF. We commonly refer the three elements (attributes) as ABC. Where,

01) A stands for Address
02) B stands for Binding and
03) C stands for Contract

Practically, we define it as, what contracts (methods or services) are provided, how to bind it (using pre-defined binding methods for security etc.), and where to find it (its address or location).

You can find the endpoint ABCs in the web.config file in your project. Once you create a service, the endpoint is included in the web.config file.

I'll explain each element individually.

To understand each endpoint element, we will first create a WCF service, and try to follow the elements gradually.

Create a Service

Open visual studio and a new website using your preferred language, i.e. C# or Vb.Net. In Solution Explorer, right click the project, select Add New Item, and choose WCF Service. Name the service as Books.svc. This procedure will add two more files to your project namely, Books.cs and IBooks.cs. (For Vb.Net, it is Books.vb and IBooks.vb)

I am not adding any method, just creating a service and add a service reference to the project. This will configure the web.config file, which help us under the basics of endpoints, its features and elements.

Now, open the class files Books.cs (Books.vb) and press F5 button. It will create the service. Again, open Solution Explorer and right click the project. This time choose Add Service Reference and will open a window we can choose the address of the service, which you have just created.

Click Go button, which will show you list of available services and operations in the address you have mentioned. Finally, click the OK button.

It is time to check the web.config file, because the process has configured the file with the necessary information that we are going discuss now. Look for the <system.serviceModel> element, and binding element <bindings>, and the client element <client>. Keep the file open we'll to need it for reference.

Address

When a client tries to have access to a WCF service, it first needs to locate the actual location of the service. It is like, you have a shop in a city, and your clients need to know the address of the shop, in order to avail its products and services.

An address is the endpoint location that provides the service to the outer world especially its clients using a network protocol. The address is usually in the form of an URL.

WCF endpoint Adderss

A typical URL would have these elements defined with specific values.

a) Protocol: The transport protocol used to transfer data (messages) from one location to another. Protocols such as, HTTP or HTTPs (“s” stands for “secure”).
b) Location and Port: The location of the service and the port number where the service is running

Eg: "//localhost:1995" - A locally hosted service using “localhost” and a port number “1995”.

c) Path and Service: The actual path and name of service, which is running

Eg: "/csharp/Book.svc"

Binding

WCF bindings represent a collection of elements, which will describe the various aspects of communication between a service and its clients. Its three main characteristics are,

What protocol it shall use for the communication?

An optional feature, a protocol defines information related to security, reliability and transaction flow of the message.

How does it transport the message?

It will define the means for transporting message from service endpoint to a client endpoint. The transport you may use can either be an HTTP, HTTPs, TCP and MSMQ etc.

Do we need to encode the message?

This feature is required to specify the encoding of the message, which can be either a Text or Binary message. The default message encoder is XML or Text for HTTP and HTTPs transports, binary message encoder for other transports.

You will find this MSDN Binding Class and a list of Bindings useful, to understand the bindings in detail.

The pre-defined binding types

WCF provides many different types of pre-defined bindings, according to MSDN. We will discuss few binding types in this article.

01) basicHttpBinding: It is a basic communication binding which exposes its service endpoints to clients using ASMX services. Before WCF services took over, .Net developers commonly used ASMX services. Due to its interoperable features, basicHttpBinding will support clients (apps) using ASMX services.

HTTP is the transport protocol used for this type of binding. It typically uses an encoder to encode SOAP messages. By default, this binding does not provide any security, due to its interoperability feature, and hence disabled. We need to configure the security mode explicitly in the web.config file.

To see how to configure the security mode, open the web.config file. Find <basicHttpBinding> and add the security mode inside the binding element, as shown in the image below.

Binding Security Mode

02) wsHttpBinding: The wsHttpBinding support the WS-* specification, a standard that define its capabilities. Hence, it is limited to newer versions only, that is, services created using wsHttpBinding will not support clients using older .Net versions.

Due its support for WS-* standards, it is more secure and reliable. This ensures that the security mode is by default enabled. I have explained about basicHttpBinding before, and wsHttpBinding seems to have a better security aspect, since wsHttpBinding encrypts the message before sending it across the network.

03) wsDualHttpBinding: It provides similar support as the wsHttpBinding. Therefore, it confirms that it secure and reliable; accept that it supports duplex service. A duplex service contract is a two way process, where a service can behave like a client (asking for service) and a client can perform the role of a service.

To facilitate a two-way (duplex) communication, the client must create and host a callback endpoint, which will allow services to communicate with its client.

We can initiate a callback service by using a “CallBackContract” property in the Service Contract.

04) wsFederationHttpBinding: It is basically an architecture that provide federated security to web services. Just like wsHttpBinding, wsFederationHttpBinding too provides a secure, reliable and interoperable binding using http transport.

Put it simple, it is alliance between service and its clients where a service requires its clients to authenticate (a security measure) using a token (encrypted) issued by a service.

The default security mode is message, though it also supports “none” as a mode.

Sample

<system.serviceModel>
    <bindings>
        <wsFederationHttpBinding>
        <binding name="wsFederationHttpBinding_IBooks">
            <security mode="Message">
                <message algorithmSuite="Basic256">
                    <issuer address="https://localhost:1995/csharp/Books.svc"></issuer>
                </message>
            </security>
        </binding>
        </wsFederationHttpBinding>
    </bindings>
</system.serviceModel>

05) msmqIntegrationBinding: It is a communication mechanism that allows services to communicate directly with applications using MSMQ. It enables MSMQ applications to send and receive messages using either COM or MSMQ APIs.

This binding ensures authentication and protection to the messages.

06) netMsmqBing: Provides a reliable and secure communication between WCF applications by using Queuing. Queuing is provided using MSMQ transport and to application which are loosely coupled.

07) netTcpBinding: It provides a secure and reliable service between WCF applications (WCF to WCF) and it is much faster that “wsHttpBinding”. When configured using a protocol called “WS-ReliableMessaging“, it confirms its reliability over the network.

08) netNamedPipeBinding: Provides a secure and reliable binding for applications running on the same machine. It too supports duplex service like “wsDualHttpBinding” binding. Please refer the third binding in this list to understand the term duplex in this context.

Its configuration is similar as netTcpBinding as it generates a runtime for messages delivered using Named pipes and binary message encoding.

A named piped is a one-way or duplex communication protocol between a piped server and piped client. You can find more info on named pipes here.

Contract

WCF contracts define what service and operations would it provide to the clients to consume. A WCF service and its client exchange messages using the provided services, on a network. These contracts, not only defines but also decides if a transaction is at all required between the client and service.

There are three different Contracts defined in WCF.

The Service Contract

The Service Contract is an Interface that defines what operations the client may call and what the service can do in return. The service contract attribute must have a collection of Operation Contract attributes.

Read more about service contract and find out how service contracts are implement in a project with examples. We have written the codes in C# and Vb.Net.

The Data Contract

Data Contract defines the structure of data, using which the client and service will exchange data. The WCF runtime uses a method called the Serializer to serialize and de-serialize the data on the network.

Learn more about WCF Data Contract and its usage.

The Message Contract

A Message contract allows us to define (explicitly) the structure of messages (XML), shared between client and service over the network. Each message will have a Header and Body section defined individually by the message contract. I have dedicated an entire article on Message contract with codes for beginners only.

← PreviousNext →