Friday, February 11, 2011

Step by Step tutorial of REST Enabled Service in WCF

, wOutput 100 is returned in the browser by the service.
It is because, below service method. URI of below GET method is mapped to root of the URI.


http://www.dotnetspark.com/kb/1373-step-by-step-tutorial-rest-enabled-service.aspx

<%@ ServiceHost Language="C#" Debug="true"
Service="RestServicePublishing.RestService"
CodeBehind="RestService.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"%>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;


namespace RestServicePublishing
{

    [Serializable]
    public class NumberService
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }

    [ServiceContract]
    public interface IRestService
    {
        [OperationContract(Name="AddParameter")]
        [WebInvoke(UriTemplate = "/",Method="POST")]
        int  Add(NumberService n1);
        [OperationContract(Name="Add")]
        [WebGet(UriTemplate = "/")]
        int Add();

    
    }


    public class RestService : IRestService
    {
        public int res = 100;

        public int Add(NumberService n1)
        {

            res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2);
            return res;
        }
        public int Add()
        {
            return res;
        }

    }
}





No comments:

Post a Comment