To enable rest feature in WCF service we must do couple of changes,
- Changes in applications main Interface.cs file
- Changes in Configuration file.
Changes that needs to be done in application config file:
Following steps needs to be added in App.config file of the hosting WCF application.
- Search for config tag <service> and under that tag add below <endpoint> tag as highlighted in below screenshot.
address = "rest" // this can be any meaning full name, this name will be refereed in API URL calls later.
binding = "webHttpBinding" // this must be webHttpBinding
contract= "xyz.Iservice" // must be valid contract address
behaviorConfiguration = "jsonBehavior" // this can be any meaning full name, this name will be refereed in this config file later.
2. Search for config tag <behaviors> and under that tag add below <endpointBehaviors> section
Add this full section under <behaviors> tag , As mentioned in above point, behavior name we used here is jsonBehavior, which is the same name we provided in above endpoint tag.
- Search for config tag <configuration> and add below section under this tag
4. Search for config tag <system.serviceModel> and add below section under this tag
Below is screenshot for reference which shows for all above changes,
Changes that needs to be done in Interface file:
Add WebInvoke attribute to the exposed method in interface as shown below,
Here GetBcmsData() method is WCF method which is decorated with WebInvoke attribute so that application can be used as REST service as well. Above example is for GET request, similar approach goes for POST, PUT and DELETE operations.
How to consume REST service?
Rest service methods can be accessed using URL, syntax / signature that needs to be followed while calling the REST URL is as follows,