Create new account I forgot my password    

Windows Communication Foundation 4.0 - New REST Features
Rating: 2 user(s) have rated this article Average rating: 3.5
Posted by: Mahesh Sabnis , on 10/14/2009, in category ".NET 4.0"
Views: this article has been read 10059 times
Abstract: By now most of you might have started exploring the new features of .NET 4.0 and have probably read my previous articles on WCF 4.0.

Windows Communication Foundation 4.0 - New REST Features
 
By now most of you might have started exploring the new features of .NET 4.0 and have probably read my previous articles on WCF 4.0. In my previous articles, we have explored various new and useful features of WCF 4.0. All these features provided by WCF 4.0 are for developers who have been writing complex code to deal with operations like Discovery, Routing, etc.  
Note: This article is based on the Beta 1 release of .NET 4.0 and can change in future
RESTful WCF service is one of the nice features provided by WCF in version 3.5. RESTful services are actually web styles WCF services, to which client can directly make a request using the http protocol. Since REST/POX provides direct XML based data available with client application, it is not necessary for client application to add service reference to create proxy class.
WCF 4.0 has introduced a new feature called ‘Automatic Help Page for REST service’. This automatic help page describes WCF service to the client. This help page explains ‘OperationContracts’ of the WCF service to consumers.
In the following WCF service we will create a WCF 4.0 service and configure it for default help page.
Step 1: Open VS2010 and create a WCF Service application as below:
CreateWCFService
Step 2: In the project add a reference to the ‘System.ServiceModel.Web’ .
Step 3: Rename ‘Iservice1.cs’ to ‘IRESTHelp.cs’ and ‘Service1.svc’ to ‘CRESTHelp.svc’.
Step 4: Right click on ‘CRESTHelp.svc’ and select ‘View Markup’. Modify ‘CRESTHelp.svc’ as shown below:
<%@ServiceHost="" Language="C#" Debug="true" Service="WCF_RestCaching.CRESTCaching" CodeBehind="CRESTCaching.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
 %>
Step 5: Open ‘Web.Config’ file and modify it as below:
<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
 <services>
    <service
      name="WCF_RestCaching.CRESTCaching">
      <endpoint address="" binding="webHttpBinding" contract="WCF_RestCaching.IRESTCaching" behaviorConfiguration="EdpHelpBehavior">
      </endpoint>
    </service>
 </services>
 <behaviors>
    <endpointBehaviors>
      <behavior name="EdpHelpBehavior">
        <webHttp enableHelp="true"/>
      </behavior>
    </endpointBehaviors>
 </behaviors>
</system.serviceModel>
Step 6: Now modify ‘IRESTHelp.cs’ as shown below:
C#
[OperationContract]
        [WebGet] 
        string GetData(int value);
VB.NET
<OperationContract, WebGet> _
String GetData(Integer value)
Step 7: Now modify ‘CRESTHelp.cs’ as shown below:
C#
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CRESTCaching : IRESTCaching
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}
VB.NET
<AspNetCompatibilityRequirements(RequirementsMode := AspNetCompatibilityRequirementsMode.Required)> _
Public Class CRESTCaching
      Inherits IRESTCaching
      Public Function GetData(ByVal value As Integer) As String
            Return String.Format("You entered: {0}", value)
      End Function
End Class
The ‘ASPNetCompatibilityRequirements’ attribute here indicates that the service is running in compatibility mode.
Step 8: Run the application, type ‘/help’ with URe L and the following page will be displayed as below:
RuntimeApplication
If you see observe the page, it provides readable information for each operation contract in the WCF service. For each request, the help page will display the XML schema.
WCF 4.0 REST Http Caching Support
 
This is another other important feature of WCF 4.0. To use this feature, the client must use various caching headers in the HTTP request and response. WCF 4.0 has provided a new attribute class ‘ASPNetCacheProfile’. This attribute must be applied on the operation contract of the WCF service interface. To use this attribute, you must import ‘System.ServiceModel.Web.Caching’. To use the caching technique, the concerned caching settings must be applied in the web.config file.
Following changes must be incorporated in the WCF service:
Step 1: Change Service contract method as below:
C#
using System.ServiceModel.Web.Caching;
 
[OperationContract]
        [WebGet] 
        [AspNetCacheProfile("CacheResponse")]
        string GetData(int value);
VB.NET
Imports System.ServiceModel.Web.Caching
 
<OperationContract, WebGet, AspNetCacheProfile("CacheResponse")> _
String GetData(Integer value)
Step 2: Modify Web.Config file as below:
<caching>
 <outputCacheSettings>
    <outputCacheProfiles>
      <add name="CacheResponse" duration="20" varyByParam="fromat"/>
    </outputCacheProfiles>
 </outputCacheSettings>
</caching>
The above configuration clearly specifies that the cache profile is set for 20 seconds. The configuration also sets ‘varyByParam’ to ‘format’ query string. This is because WCF REST service supports both XML as well as JSON, and to send a request, query string parameter is used.
Conclusion: Using WCF REST improvements, now WCF service consumers can get information of the WCF service contract. The help page makes contract information available to consumers to make easy for them to request an operation.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Anil Pandey on Monday, December 14, 2009 3:11 AM
Very Nice Post.. Waiting for the other new features of 4.0.

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER