Thursday, September 19, 2013

Self Hosting WCF Service

For WCF Self hosting we need to do the following things
1>Create a WCF Service Library
2>Delete the service class and service interface[IService.cs,Service.cs]
3>Delete the service tag from the App.config file.
4>Right click on ServiceApp=>Add New Item=>From the item panel choose Wcf Service
5>Now run the services
5>Create a Console Application to Host the services
6>Add the following code for hosting the services
7>Add the references as service project and System.Servicemodel.
8>Now set the Host application as "Set As Start Up Project".
9>Now create a client to consume the services
10>Add the Service References
11>Now run the client application

How to check Session in WCF
To check the session in WCF we need to do the following things
1>Add the following line in the service class
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
2>Expose a method which increment a value to check the session
3>Now consume the expose method from client

Generated Output

Conclusion
Here for different client proxy instances, value is incremented by one.So from output we can understand at service end for different client proxy new service instance is created but for same client proxy the same service instance is used.

Note:If service is not running properly then we can do one thing. First we need to open the project in two visual studio. In one editor set the Host application as Start Up Project and run it, in other editor set client as Set Up Project and run it.

How to implement Fault Exception in WCF
To implement Fault Exception in WCF we need to first define the fault contract in service contract
Secondly define the service class with Fault Exception