Making the service RESTful

To make the SecurityService RESTful, you would need to specify the [WebGet] or [WebInvoke] attributes in your service contract. Although the former indicates that the WCF Service can respond to HTTP Get requests, the latter is used to indicate that the WCF Service can respond to HTTP Post requests. Both belong to the System.ServiceModel.Web namespace, and are actually part of the HTTP programming model for WCF.

The following code snippet illustrates how a typical [WebGet] attribute is defined:

[WebGet(UriTemplate = 
"/sales/getsales.xml",
  BodyStyle = WebMessageBodyStyle.Bare,
  RequestFormat = WebMessageFormat.Xml,
  ResponseFormat = WebMessageFormat.Xml)]

The [WebInvoke] attribute can be defined so that your WCF Service can respond to HTTP Post operations:

[GetOperationContract]
[WebInvoke(UriTemplate = 
"/sales/updatesales.xml?
productcode={code}",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]

To make the service we created earlier in this article RESTful, just change the contract ISecurityService, and specify the WebGet attribute, as in the following code:

[ServiceContract]
    public interface ISecurityService
    {
        [OperationContract]
        [WebGet]
        User GetUserData(Int32 userID);
        [WebInvoke]
        User PostUserData(Int32 userID);
    }

To enable the service to run in the ASP.NET compatibility mode, you should decorate your service class with the AspNetCompatibilityRequirements attribute, as shown in the following code:

[AspNetCompatibilityRequirements
    (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class PackRESTSerivce : IRestSerivce
{
    //Code implementation goes here
}

Hosting Security Service

You would now need to host your service. To do this, you would use the WebServiceHostfactory class derived from the ServiceHost class. You should choose the WebServiceHostFactory class if the service uses WebHttpBinding. If your service uses other types of bindings, you can simply use ServiceHostFactory to host the service.

To host Security Service, create a Console application, and add references to the following assemblies:

  • System.ServiceModel
  • System.ServiceModel.Description
  • System.ServiceModel.Web

Next, create an instance of the WebServiceHostfactory class, and pass the base address and the service type name as parameters:

Uri baseAddress = new Uri("http://localhost/SecurityService");
WebServiceHost host = new WebServiceHost(typeof(SecurityService), baseAddress);

You would now need to specify the service endpoint and service debug behavior for your RESTful service. The following is the complete source code:

using System;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.ServiceModel;
using Packt.Services.SecurityService;

namespace Packt.ServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost/SecurityService");
            using (WebServiceHost host = new WebServiceHost(typeof(SecurityService), baseAddress))
            {
                ServiceEndpoint serviceEndpoint = host.AddServiceEndpoint(typeof(ITestService), new WebHttpBinding(), "http://localhost/PacktServices/SecurityService.svc");
                ServiceDebugBehavior serviceDebugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();
                serviceDebugBehavior.HttpHelpPageEnabled = false;
                host.Open();
                Console.WriteLine("The SecurityService is ready...");
                Console.WriteLine("Press enter to terminate...");
                Console.ReadLine();
                host.Close();
            }
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset