Now continuing from the last section. Let’s look a quick demo of writing a simple WCF service. Below, in the screen shot.
Now, i am going to delete the default implementation presented by visual studio and new class EmployeeService. So, below implementation has got one concrete class which acts DataContract, means composed of properties supposed to sent across wire, then one interface which is having two operations. 1st one to Submit the employee and 2nd one to Print the employee.
Now, if you see in the above implementation you will come to know it has got all the required attributes which is needed for WCF service implementation say
A simple Data Contract class An Interface which is basically a set of operations And then one concrete class which is basically implementing these operations.
Now, one point to note here, is above class implementation is the default interface implementation. Now, I am going to modify the same to look like as shown below. Also, at this point now i can go ahead and add the service behavior on our service class by specifying InstanceContextMode to single which means within the service host, we will be having single instance of service running in memory and it will be used to serve all incoming messages. Now, the one advantage associated with this is now, i can have a list of employees in memory and then whenever Submit employee is called, it will simply add the employee into that list and in case of Print employee, it will simply return the result from the memory.
So, now at this point i can go ahead and build the project. And, once the build is succeeded i am ready to host this. But, before that i need to modify my App.config file so that it has the correct contract and service name. Below is the modified app.config file.
Now, with this change in place i can go ahead and host the service in Visual Studio Test tool. So, in order to do that i just need to click F5 and then little window 1st appear in the tray icon saying that your service is hosted.
Now, from WCF test client i can go ahead and test my operations.
so, in the above screen shot i have specified the required parameters and then clicked on invoke button to add the same in memory. Now, in order to retrieve the same i can just go ahead invoke print employee as shown below in the screen shot.
Now, with this i would like to wrap this session here, in the next segment will delve further into WCF configurations. Till then stay tuned and Happy Coding.
1,596 total views, 1 views today