Monday, May 7, 2012

WCF Basics :


IService.cs

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

namespace sampleWCF
{
    // 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(Namespace = "")]
    public interface IAddService
    {
        [OperationContract(Name = "IGetdata")]
        CaseResponse IGetdata(CaseRequest getdata);

    }

    [MessageContract]
    public class userRequest
    {
        [MessageHeader(Name="hai")]
        public int  intNo1;
        [MessageHeader]
        public int  intNo2;
       
    }

   
    [MessageContract]
    public class CaseRequest
    {
        [MessageHeader]
        public int no1;
        [MessageHeader]
        public int no2;
       
    }

    [MessageContract]
    public class CaseResponse
    {
        [MessageBodyMember]
        public int result;
    }


}




Service1.svc.cs


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

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

        #region IAddService Members

       
        public CaseResponse IGetdata(CaseRequest getdata)
        {

            int noc = 0;
            CaseResponse result = new CaseResponse();

                    try
            {

                int noa = getdata.no1;
                int nob = getdata.no2;

                userRequest obj = new userRequest();
                obj.intNo1 = noa;
                obj.intNo2 = nob;
                noc = IAdd(obj);       
                result.result = noc;
               
            }
            catch (Exception ex)
            {

                return result;
 
            }

            return result;

        }


        public int IAdd(userRequest AddData)
        {

            return AddData.intNo1 + AddData.intNo2;

        }

        #endregion
    }
}



Webform1.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sampleWCF.ServiceReference1;

namespace sampleWCF
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            int result = 0;

         //   ServiceReference1.IAddService obj = new ServiceReference1.IAddService();
           
            Service1 obj = new Service1();
           
          //  result=obj.

            CaseRequest  objCaseRequest = new CaseRequest();

            objCaseRequest.no1 = 7;
            objCaseRequest.no2 = 9;
        
            result =Convert.ToInt16(obj.IGetdata(objCaseRequest).result);            


        }
    }
}

No comments:

Post a Comment