Getting Started with WCF – Part 4

Hi Friends,

Now with previous theoretical concepts, let’s jump into some practical implementations for the same. So, what i do i simply go ahead and launch my service which i have written earlier for WCF Demo. So, below in the screen shot, test client gets popped from VS 2013. Notice that our Demo service is up and running now.

11th

Picture15

Now, from this Host, i can go ahead and copy metadata and paste the same in notepad for later reference, which i’ll be using for creating my client.

Picture16

Now, at this instant, i will return to my Visual Studio and add new project basically to build my client which an talk to the service. So, what i’ll do, i will simply go ahead and add simple console app to talk to this service as shown below.

Picture17

Picture18

Then, i can simply go ahead and add reference to my client project with the metadata which i copied initially. For doing the same, i’ll simply click on references and say add service reference as shown below

Picture19

Then, it will open dialog box, wherein i will simply paste the metadata and click on go.

Picture20

so, here it downloaded the metadata and showed me the service contract available for the service and also the operation. So, below in the dialog either i can change my service reference name or keep the same, but i am going to make it EmployeeServiceReference and press ok. So, as soon as i clicked ok, it downloaded the metadata and generated all required components for me. In the below screen shot, 1st thing which you will notice it generated all the assemblies required to run the WCF, then it created one App.config file which is nothing but the client side configuration file having the endpoints in it.

Picture21

It also created service reference in there. So, now if you right click the service reference, you can see all the object level details in the object browser like what properties we created while creating the class, methods etc.

Picture22

Now, the very 1st thing which we need to do here in our code is to include the client side reference as shown below in the screen shot.

Picture23

Then, i can go there and instantiate my employee client as shown below in the screen shot. Now, when i instantiate this, it has to be done against one end point

Picture24

Now, in order to get the endpoint details at the client side, you can open the app.config file at the client side and check what Visual Studio has created for you like shown below

Picture25

Now, from these options i will select any endpoint of my interest and paste in there. Now, it will construct the communication channel based on the endpoint. Then i need to generate the required information like properties which i used while writing the class then fill the same with data and then in the end i’ll just invoke the method and print the same on the console.

Picture30

Now, i need to do some setting in the project to make sure that it will run in my console client app. So, for that i’ll come to my service project and click on properties and then

Picture27

Now, here i need to change this name with our client app which is “ClientDemo.exe”. And then i also need to assign the working directory of my client till debug as “C:\Rahul\Books\WCF\WCFServiceDemo\ClientDemo\bin\Debug\” Now, with that change in place i am good to go.

Picture28

Picture29

So, with this output i would like to wrap this session here. Will see in another session more about WCF and it’s terminologies. Till then stay tuned and happy coding.

Thanks,
Rahul
Happy Coding

Getting Started with WCF – Part 3

Hi Friends,

In today’s post i will talk more about Endpoints and Services. So, basically in the last session we saw how to write a simple WCF program and host the same. So, basically with WCF we write services which expose end points to the outside world. Here, our service implementation will define the actual business logic and endpoints define the communication logic. Basically, endpoints serve various communication logic. We’ll see some of these in some time. Now, below is the diagram which explains the relationship between services and endpoints. So, basically services expose multiple endpoints to it’s consumers and consumers then utilize this service via endpoint.

Picture12

Now, the question arises here is that what is an Endpoint? well endpoint is basically an information that tells WCF how to setup communication channel. Now, each endpoint address consists of three Major things.
Now, below is the diagram which shows the actual meaning of endpoint in a nutshell.

Picture9
EndPoints consist three major things. These are

Address:- Basically tells where to send the messages.
Binding:- How to send them.
Contract:- What messages will contain.

Now, these are basically A,B,C of WCF. Address defines network address for sending and receiving messages. Here, WCF runtime will produce a communication channel which will be served as listener for these messages. Then comes the binding which defines the protocol for how to send messages like it specifies what kind of Transport Protocol needs to be used, what message format and what WS-* protocol you want to use. Then, we have Contract, which tells what the messages must contain. So, each endpoint contains three basic information. Hence, services exposes endpoints and client consume them.

Now, let’s discuss binding in detail. In the below table, you can see some of the built in bindings and it’s usage scenarios.

Picture10

So, as you see in the table 1st three bindings all use HTTP. So, here Transport mechanism is also associated with the binding name. The 1st one provides Restful style communication whereas other two provides SOAP style communication. We’ll understand all these styles of communication in detail in coming sessions. Now, one important point to note here that these 1st three bindings are meant for interoperability which means they will use XML message format for communication.Now, the remaining bindings shown here are basically not meant for interoperability. Basically, they assume that WCF is used on both sides as these bindings are .Net Specific. That is why their naming also begins with net which means .Net to .Net communication. Now, irrespective of these built in bindings, you can always write your custom binding which could be a mix of these built in bindings. By the way, before selecting any binding you should always know which one would be best suited in your case.

Now, let’s see the process of consuming WCF Service. Now, in order to consume the WCF service, client need to know several things like

Where to send the message
How to send the message
Then what the messages are going to contain

So, basically these are the endpoints of any WCF service. So, in order to consume any WCF service client need to know the endpoint of that particular service. Now, below is the most generalized description from the higher level which shows how communication happens.

Picture11

Now, with WCF we consume services via channels. Below, is the labeled diagram explaining how clients consume WCF service.
Picture14

Now, channels are basically built based on endpoints. Now, here, the client has to retrieve the endpoint definition from service metadata. Now, as you can see in the diagram we have a Service which is exposing multiple endpoints. I have shown in diagram two endpoints but it can be any number. Then, let’s suppose client wants to talk to this service via this endpoint. Then, in order to do so, client has to request metadata or WSDL definition from the service. Now, once metadata is available to the client, client will run one metadata import tool which will produce client side version of the endpoint. Now, once client has endpoints available; client can then go ahead and construct the channel based on one of the endpoints. And, once the channel has been constructed, client can send message through that channel to the service. So, basically WCF provides nice symmetrical architecture on both sides of the wire.

So, in this session we have seen closely about endpoints, services and then how to consume services theoretically. We will see all these practically in coming sessions. Till then stay tuned and Happy Coding.

Thanks,
Rahul