Introduction:
Windows Communication Foundation (WCF) – is a framework for building service oriented applications. This service will continuously available for clients to consume, which hosted on the server.
WCF is huge framework to understand but with this basic introduction we will see how to create a simple service which returns a message ‘Hello WCF world’. In WCF world we will have basically attributes which needs to decorate to our classes and interfaces.
Basic requirements :
To understand this article, you should aware of OOPS concept and basic working idea on attributes and C# language.
WCF Basics:
Before jumping into actual creating WCF – you should aware of few basic concepts in WCF.
1. Interfaces – Interfaces are building blocks of WCF framework through which outer world will interact to our service. Mean while we don’t want to make outer world to see or understand our service implementation – that’s the reason why Interfaces we’re used.
2. Service class – It’s basically a class which implements our WCF interface and gives your required output, like it may interact with database, file operation etc.
3.Attributes – WCF attributes are available in System.ServiceModel assembly which contains all necessary information to create an service. I given brief information about basic attributes as mentioned below.
1. Service Contract – An attribute which makes normal interface to WCF interface by decorating on the interface.
Example – [ServiceContract] Public interface IHelloWorld { }
2. Operation Contract – An attribute which should be decorate on a methods of interfaces which are needs to show for outer world. Without this all methods will not exposed to the same. Example shown below.
[ServiceContract]
Public interface IHelloWorld
{
[OperationContract]
string GetGreetings();
}
Creating, Hosting and Consuming WCF Service:
With above basic introduction we will start with creating an WCF service, then hosting and consuming the same.
Step 1 – Open Visual studio – New Project -> WCF tab -> WCF library. Name it as HelloWCFWorld. Press OK. It gonna create a new project for WCF which will take cares of Hosting and Consuming.
Image – Adding WCF Service project.
Step 2 – Delete IService and Service class – we will start freshly with new classes. Add new class – name it HelloWorldService and Create new Interface and name it IHelloWorldService.
Image – Adding new class and interface.
Step 3 – Introduce a new method in IHelloWorldService name it as GetGreating (Which returns string). To make it as WCF service and as discussed above we should decorate the same with Service and Operation Contracts.
Image – WCF Interface implementation.
Step 4 – HelloWorldService will implements the interface IHelloWorldService and return greeting as ‘Hello WCF world.’. That’s it. we ready to go but one more step that is we removed IService and Service classes previously, so need to replace in App.Config with our classes.
Image – Implementing interface in Service class.
Image – App.Config replacing old class name to new one.
Step 5 – Now just run the application by pressing Ctr+F5 , now you can see WCF client test window, which has information on our interface and just double click on the IHelloWorldService and click on Invoke (If you get any prompts you can say OK). That should return your greeting as ‘Hello WCF world.’.
Image – testing WCF service.
Conclusion: With this we created, hosted and consumed a WCF service which was basic step to start any new framework or language. You can explore more on the WCF framework for creating huge service oriented application for real time use. Thanks.
Happy coding 🙂