So, in today’s discussion, will move next step by writing a simple WCF program. So, let’s get started. Below in the screenshot, you can see I am in Visual Studio instance 2013.
Then,i’ll click on new project and select WCF tab and from there i will select WCF Service Library.
Now, this will create one simple class library solution as shown below in the screen shot.
Now, let’s understand what visual studio has given us by default. so, basically it gave me basic implementation of WCF service to get started. But, i don’t want these stuffs; rather i am quite interested in getting started right from the scratch. so, i’ll go ahead and delete these two files. Now, let’s look at the references which visual studio gave me with this template.
Now, these two namespaces are the basic things which required for building any WCF Service. Now, it also gave me app.config file which is pretty much required for all types of WCF configurations. Now, i’m going to write new class to it
Now, i am going to modify this, i’ll add one more class here itself and then annotate the same with [DataContract] as shown below so that this class will get exposed for WCF. Now, for each field within the class also needs to be annotated with [DataMember] as shown below in the screen shot.
Now, i am going to create one interface which will have one operation which will basically return one string with values which we have in the class. Also, if you have noticed that this service contract has taken Employee as an object. Now, in order to expose this as a service contract, i also need to annotate the same with [ServiceContract] attribute and the other attribute which i need to apply on the method is the [OperationContract] as shown below in the screen shot.
Also, one point to note here that [ServiceContract] is available under the namespace “System.ServiceModel”.
Now, to implement this WCF service contract, I will come to my base class and simply derive it from here as shown below in the screen shot.
So, above is my WCF Implementation. Notice that for WCF Implementation, I don’t need any kind of annotation. Now, we are good to host our service either in IIS, or we can write our own custom hosting mechanism also known as self hosting or we can test the same with WCF Test client. For now, i’ll be using WCF Test client. But, in either case, we need to update our App.Config with correct endpoints so that TestClient will come to know which service you are interested in hosting.
Now, i have completely modified the App.Config with my requirements. So, basically i have set configured three endpoints in it with service contract which i have written during implementation. i have also configured the base address of my service. Now, below is the modified app.config file.
Now, with the above changes in place, we are ready to host the service on WCF Test Client. Now, all i need is to save this file and press F5. Now, when i press F5, it will launch the below shown window which is basically a confirmation that your service has started.
Now, as you see in the screen shot, there are three endpoints hosting the same contract but different ways. So, now if click on any of these methods of any endpoint it will present me the same result. For example, I click on PrintEmployee() of BasicHttpBinding and it will present me the below UI.
Now here i can go ahead and mention the values in those text box and then click on invoke button, then it will return me expected result in string format.
With this i would like to wrap this session here. In the next section, will do more deep dive and see other important things related with WCF. Till then stay tuned and Happy Coding.
1,632 total views, 1 views today