Before I start with this article I’d like you to know certain things about WCF or the Windows Communication Foundation. We all have heard about servers and applications running at server end(the back end) like, a Java Servlet. The basic idea in the infrastructure is that certain parts of the applications are deployed as services across the hosts, which means, the application is not running in a native machine but as a service across several machines connected and sharing stuff between themselves through a network.
It is a system for creating connections between applications using services and endpoints. WCF is, more than anything, an infrastructure technology for messages. Just as roads support cars, and as electricity travels over wires and cables, and as pipes convey water, WCF exists to transfer messages between any two endpoints. And it does so securely as well. That is, you can create messages that are encrypted to keep your information safe from being tampered with. A standard example will be Data integration service for any Windows Forms Application that you develop or a WPF one, even a Silverlight based RIA (Rich Internet Application).
So, before we start, let’s get the basics clear :
- We have a DataContract where we add Data members.
- We have a ServiceContract where we mention about operations to be performed on the data, i.e, we declare methods.
- Finally, we have ServiceBehaviour where we assign how the service needs to be executed or how our WCF application should behave.
So, getting this correct we shall move on to our project.
- First create a WCF Service Library in Visual Studio :
3. Now add another class to the project named IDataService.cs, change it to an interface(instead of a class), and enter the code:
4. Now add another class named as DataService.cs and add the following code :
5. Now build the project.
6. Now that our model is ready we need to make certain changes to the App.Config file so that our application works as we need in the host.
First, we edit WCF configuration of app.config file by right clicking, check the service tab, and we have a browse window go to e:\programming\c#\marksheet\marksheet\bin\Debug (or project location) and choose the appropriate service dll.
Now, let’s select some end points by choosing empty endpoint name and in service endpoint window select contract browse to appropriate service like above.
Now, close, save all if prompted and deploy (Ctrl+F5).
Now there you have your methods, lets deploy the Submit_Data() method to add an entry to our Database and then invoke :
Continue, for other methods, and that’s it. Simple and easy.