Windows Communication Foundation 4.0 - New REST Features

Posted by: Mahesh Sabnis , on 10/14/2009, in Category Windows Communication Foundation (WCF)
Views: 57987
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.
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.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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.
Comment posted by Nasser on Monday, November 22, 2010 1:16 PM
Maybe copy/paste got the best of you...

Two projects seem to have gotten mixed up.

In steps 4 and 5, CRESTCaching must be CRESTHelp and IRESTCaching must read IRESTHelp.

Then just right click the RestHelp.svc and click "View in Browser". You can then add /help to end of the URL.

It worked for me.
Comment posted by Ragan on Friday, January 21, 2011 1:31 PM
Really waste of time this example, you need to learn to copy and paste and how to explain. I wasted my time trying to follow this example.
Comment posted by James Bristow on Wednesday, November 9, 2011 2:11 PM
Great article for the first time creation of a RESTful service. Minor spelling error in "<webHttp enableHelp="true"/>." The actual attribute is "helpEnabled".
Comment posted by Rakesh Pandian on Friday, December 20, 2013 2:42 AM
Please Give me the Source Code of the Solution.
also Give me some idea about Routing and Remoting in WCF

Thanks in Advance