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

Sunday, January 6, 2013

Create And test WCF Service

1>Create a simple WCF service

 
2>Now Change the service name to the desired service name[Find and replace old service Name with new one]

3>Add the Endpoint of REST & SOAP like following

4>Add the host address.
Host address should be=> http://Domain Name/Service Virtual Directory\n EX:http://localhost/MyService

5>Now addd the EndPoint behaviors for webHttp

6>Now add a Service Method in the interface
Note:Add the "System.ServiceModel.Web" As reference

7>Inherit the service method and defined it in the following way


8>Create a virtual directory say "MyService" and specify the virtual directory path to the WCF Service path

9>Right Click on the WCF Service and Click on Properties. Here Start URL Should be :
http://domain name/Service Virtual Directory/SVC file name/EndPoint Name/URITemplate Name
EX:http://localhost/MyService/MyService.svc/REST/MyAction

9>Specify IIS Home directory path to the WCF Service path

10>So WCF service is now hosted in IIS


11>Now to test the service we need to create a console application
We can call the service asynchronously as well as synchronously
To call the service Synchronously we need to do the following things

To Call the service Asynchronously we need to do the following things

How to Host A WCF Service in IIS server
1>Create A WCF service and set the IIS home directory as the service path

2>Specify the start up url in the IIS location as
http://domain name/Service Virtual Directory/SVC file name/EndPoint Name/URITemplate Name
EX:http://localhost/MyService/MyService.svc/REST/MyAction

3>Create virtual directory and specify the service path as it's path

4>Add the host location in the config file
Note:Here MyService is the created service virtual directory

Service Tag Under <system.serviceModel>

Behavior Tag Under <system.serviceModel>

REST SERVICE ERROR
1>The remote server return an error method not allowed
Sol:This error is generated because of wrong way of method request.Suppose WCF service accept POST request but client request the service method with GET
2>The remote server returned an error: (400) Bad Request
Sol:This error is generated because of wrong type/parameter request.Also if from service end if any error occur then also we get this type of error

How to debugg WCF Service
i)TO debug the WCF service first, we need to host it in IIS
ii)Run the application from where WCF service is consumed.
iii)From Editor navigate to Debugg=>Attach The Process=>Attach the WCF service Process or attach the aspnet_wp.exe
iv)Keep the break point in appropriate position and to go throught the break point press F5