Saturday, June 9, 2012

Developing WCF Service using VS.Net Support

Microsoft VisualStudio Net provides many templates for developing as well as consuming wcf services.It has to two important/core templates for developing wcf service.These templates not only make our work easier but also they help us in developing wcf service in standard approach.
Apart from these templates we have other concepts/Projects related wcf templates also means wcf azure template...wcf cloud based service,wcf ajax template...wcf ajax based service...many more like this.
It is highly recommended to use these templates and develop the require projects instead of manually handling them.
BasicHttpBinding => SOAP+HTTP
wsHttpBinding => SOAP+Security+Performance+HTTP

WCF Service Library
  •   A sample wcf interface and derived class.
  • It provides app config file,where all service endpoints and behavior are written as follows:


<system.serviceModel>
    <services>
      <service name="JobService.Jobs">
        <endpoint address="" binding="wsHttpBinding" contract="JobService.IJobs">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/JobService/Jobs/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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>
  </system.serviceModel>

 Devolping wcf Service Jobservice:
To develop jobservice first we have download pubs database .
  1. write the following code under IJobs.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;

namespace JobService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IJobs" in both code and config file together.
    [ServiceContract]
    public interface IJobs
    {
        [OperationContract]
        Job GetJobInfo(int jobid);
        [OperationContract]
        DataSet GetJobs();
        [OperationContract]
        List<Job> GetAllJobs();
        [OperationContract]
        void Dowoker();
    }
}
  2.write the following code under Jobs.cs

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace JobService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Jobs" in both code and config file together.
    public class Jobs : IJobs
    {
        string cnstr = @"Data Source=SUDHAKAR-PC;Initial Catalog=Pubs;User ID=sa;Password=#sa#";
        public void DoWork()
        {
        }

        public Job GetJobInfo(int jobid)
        {
            SqlConnection con = new SqlConnection(cnstr);
            SqlCommand cmd = new SqlCommand("select * from jobs where job_id="+jobid,con);
            Job jobdata = new Job();
            con.Open();
            SqlDataReader dr=cmd.ExecuteReader();
            if (dr.Read())
            {
                jobdata.Description = dr[1].ToString();
                jobdata.JobID = jobid;
                jobdata.MinLevel = Convert.ToInt32(dr[2]);
                jobdata.MaxLevel = Convert.ToInt32(dr[3]);
            }
            else {
                jobdata.JobID = -1;
            }
            con.Close();
            return jobdata;
        }

        public System.Data.DataSet GetJobs()
        {
            SqlConnection con = new SqlConnection(cnstr);
            SqlDataAdapter da = new SqlDataAdapter("select * from jobs",con);
            DataSet ds = new DataSet();
            da.Fill(ds,"jobs");
            return ds;
        }

        public List<Job> GetAllJobs()
        {
            //JobsDataDataContext dc=new JobsDataDataContext();

            //List<job> obj = new List<job>();
            //obj = dc.jobs.ToList();
            //return obj;
            throw new NotImplementedException();
        }

        public void Dowoker()
        {
            throw new NotImplementedException();
        }
    }
}
 3. write the following code under Job.cs
    

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

namespace JobService
{
    [DataContract]
    public class Job
    {
        [DataMember]
        public int JobID { get; set; }
        [DataMember]
        public string  Description { get; set; }
        [DataMember]
        public int MinLevel { get; set; }
        [DataMember]
        public int MaxLevel { get; set; }

    }
}

1 comment:

  1. I think you did a great job with this information. I have been searching this for a while for my Offshore .NET Development.

    .NET Development Company

    ReplyDelete