Thursday, 12 September 2013

creatting wcf service with two end points

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


namespace WcfService2
{
    // 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]
    public interface IService1
    {
        [OperationContract]
        List<empdata> getemp();            
    }
    [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        List<empdata> getemp1();
        [OperationContract(IsOneWay=true)]
        void display();    

    }
    [DataContract]
    public class empdata
    {
        private int id;
        private string name;
        private int age;

        [DataMember]
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        [DataMember]
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
}
-------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
   [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class Service1 : IService1, IService2
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        DataTable dt = new DataTable();
        SqlDataReader dr;
        public List<empdata> getemp()
        {
            List<empdata> emplist = new List<empdata>();
            try
            {            
                empdata data = null;
                if (con.State == ConnectionState.Open)
                    con.Close();
                SqlCommand cmd = new SqlCommand("sp_fetchdata", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                dr = cmd.ExecuteReader();
                dt.Load(dr);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    data = new empdata();
                    data.ID = Convert.ToInt32(dt.Rows[i]["id"].ToString());
                    data.Name = Convert.ToString(dt.Rows[i]["name"].ToString());
                    data.Age = Convert.ToInt32(dt.Rows[i]["age"].ToString());
                    emplist.Add(data);
                }
            }
            catch (Exception ex)
            {

            }
            return emplist;        
        }
        public List<empdata> getemp1()
        {
            List<empdata> emplist = new List<empdata>();
            empdata data = null;
            if (con.State == ConnectionState.Open)
                con.Close();
            SqlCommand cmd = new SqlCommand("sp_fetchdata", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            dr = cmd.ExecuteReader();
            dt.Load(dr);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data = new empdata();
                data.ID = Convert.ToInt32(dt.Rows[i]["id"].ToString());
                data.Name = Convert.ToString(dt.Rows[i]["name"].ToString());
                data.Age = Convert.ToInt32(dt.Rows[i]["age"].ToString());
                emplist.Add(data);
            }
            return emplist;
        }
 public void display()
      {
          string str = "method";

      }

  }
}
--------------------------------------------------------------------------------------------
web.config

  <services>
      <service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address=" " binding="wsHttpBinding" contract="WcfService2.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="WcfService2.IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService2.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

No comments:

Post a Comment