WCF part3 Restful

WCF REST

REST is a stateless, sessionless, resource-based web service. It's good if your service is designed to access data and perform simple CRUD operations on it. SOAP and REST are mutually exclusive.SOAP addapt better for bussiness applications for Security reasons and can be better configured.

WCF
1. It is also based on SOAP and return data in XML form.
2. It is the evolution of the web service(ASMX) and support various protocols like 3. TCP, HTTP, HTTPS, Named Pipes, MSMQ.
4. The main issue with WCF is, its tedious and extensive configuration.
5. It is not open source but can be consumed by any client that understands xml.
6. It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest
1. To use WCF as WCF Rest service you have to enable webHttpBindings.
2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
3. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
4. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
5. It support XML, JSON and ATOM data format.

Where to use:-

1. SOAP services with SOA architecture are well suited in enterprise applications. SOA architecture includes several key factors like service versioning, scalability, deployment and maintaining of services.
2. REST services are best suited in Internet-facing applications, which can take advantage of HTTP caching and other features.

There are some scenario’s where we can use RESTful service like
1) Less Overhead because there is no use of SOAP object
2) Testing is very easy
3) Standardization is very nice

REST uses4 HTTP methods to insert/delete/update/retrieve information which is below:
GET - Retrive a specific representation of a resource
PUT - Creates or updates a resource with the supplied representation
DELETE - Deletes the specified resource
POST - Submits data to be processed by the identified resource


WEBGET  & WEBINVOKE:

WCF WEB HTTP services make use of retrieval verbs (for example HTTP GET) in addition to various invoke verbs (for example HTTP POST, PUT, and DELETE). The WCF WEB HTTP programming model allows service developers to control the both the URI template and verb associated with their service operations with the WebGetAttribute and WebInvokeAttribute. The WebGetAttribute and the WebInvokeAttribute allow you to control how individual operations get bound to URIs and the HTTP methods associated with those URIs. For example, adding WebGetAttribute and WebInvokeAttribute in the following code.

 

[ServiceContract] interface ICustomer { //"View It" [WebGet] Customer GetCustomer(): //"Do It" [WebInvoke] Customer UpdateCustomerName( string id, string newName ); }

The preceding code allows you to make the following HTTP requests.

GET /GetCustomer

POST /UpdateCustomerName

 


 

Creating a simple WCF Rest Proyect:

  1. Create a blank solution
  2. add WCF  ApplicationProyect
  3. Delete .svc and class that appears
  4. Add new ítem WCF Service

rest2 rest3 rest4 rest1

  • Now  lets decorate Service contract :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace WcfService2

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.

[ServiceContract]

public interface IService1

{

[OperationContract]

[WebInvoke(Method ="GET", UriTemplate ="/dansau/{value}",RequestFormat =WebMessageFormat.Json,ResponseFormat =WebMessageFormat.Json,BodyStyle =WebMessageBodyStyle.Wrapped)]

string GetData(string value);

}

}

 

  • let us implement that Service contract in the Service class and return a simple value:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

namespace WcfService2

{

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.

// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.

public class Service1 : IService1

{

public string GetData(string value)

{

return "my value "+value+"% ";

}

}

}

  • in the webconfig file you will have to  type the right binding configuration and the webhttp enambled.

i leave you here a copy of mine:

<?xml version="1.0"?>

<configuration>

<appSettings>

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>

</appSettings>

<system.web>

<compilation debug="true" targetFramework="4.5.2"/>

<httpRuntime targetFramework="4.5.2"/>

<httpModules>

<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>

</httpModules>

</system.web>

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behaviorname="serviceBehaviors">

<!-- To avoid disclosing metadata information, set the values below to false before deployment -->

<serviceMetadatahttpGetEnabled="true"/>

<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->

<serviceDebug includeExceptionDetailInFaults="false"/>

</behavior>

</serviceBehaviors>

<endpointBehaviors>

<behaviorname="web">

<webHttp/>

</behavior>

</endpointBehaviors>

</behaviors>

<services>

<servicebehaviorConfiguration="serviceBehaviors"name="WcfService2.Service1">

<endpointaddress=""contract="WcfService2.IService1"binding="webHttpBinding"behaviorConfiguration="web" >

</endpoint>

</service>

</services>

<protocolMapping>

<add binding="basicHttpsBinding" scheme="http"/>

</protocolMapping>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

</system.serviceModel>

<system.webServer>

<modules runAllManagedModulesForAllRequests="true">

<remove name="ApplicationInsightsWebTracking"/>

<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"

preCondition="managedHandler"/>

</modules>

<directoryBrowse enabled="true"/>

<validation validateIntegratedModeConfiguration="false"/>

</system.webServer>

</configuration>

 

 

 

  • lets run Service  pointing to https://localhost:1350/Service1.svc

you will see Service respond:

Service1 Service

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

 svcutil.exe https://localhost:1350/Service1.svc?wsdl

You can also access the service description as a single file:

 https://localhost:1350/Service1.svc?singleWsdl

 

  • and then point to URItemplate decorated in the Service contract:

for example place : https://localhost:1350/Service1.svc/dansau/hola3

the result is in json format as stated down below :

  {"GetDataResult":"ishola3 more"}<br>------------<br>Thanks.:)