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

1,393 total views, 1 views today