C# – Simple WCF project

Standard

.Net remoting [pluggable objects used to communicate between different application domains within the same process or different computers within the network or one process to another process in the same computer using either Simple Object Access Protocol or Hyper Text Transfer Protocol using either RPC or Doc style.] is now legacy technology, not recommended by Microsoft after .Net 2.0. Recent versions .Net framework supports and comes with WCF (Windows Communication Foundation). This technology replacing .Net remoting.

I have used this SOAP protocol in my earlier projects connecting with Java Web Services from .Net integration using WSE 3.0 (Web Services Enhancement – I have got the issue during this period [HP – SABA interfaces] when setup the IDE for development; Web Service Enhancement not integrated with Visual Studio that means corresponding WSE menus to change the configuration setting and policy file was not appeared in VS. After uninstall both VS and WSE 3.0 and change the installation order it was integrated properly.).

Simple WCF Service application for demo

A) Develop the Database components:

Connect student database to get student data using stored procedure ‘getStudents’.

Stored procedure:

CREATE PROCEDURE dbo.getStudents
AS
/* SET NOCOUNT ON */

Select st.Student_id as StudentId, st.Student_Name as StudentName,
st.Class_id as ClassId, sb.Subject_Name from Student st inner join Student_Subject ss
on st.Student_id = ss.Student_id inner join Subjects sb on ss.Subject_Id = sb.Subject_Id

RETURN

B) Develop Service Component:

Using System.ServiceModel to create the service and operation contract with [ServiceContract] and [OperationContract] attribute respectively. Then create data contract using System.Runtime.Serialization with [DataContract] attribute.

1. Service Contract –  Contract to define service definition using with [ServiceContract] attribute with interface or class.

 

[ServiceContract]

public interface IstudentDataService

{

[OperationContract]

Student[] getStudentData();

// TODO: Add your service operations here

}

2. Operation Contract – Contract to operate the service method using with [OperationContract] attribute within the service contract.

3. Data Contract – contract to define the data received by and returned from the service.

[DataContract]

public class Student

{

public string studentId { get; set; }

public string studentName { get; set; }

public string classId { get; set; }

public string subjectName { get; set; }

}

C) Implement the service component:

Implement the interface will invoke the ‘getStudentData’ method from ‘studentData’ class.

public class studentDetailsService : IstudentDataService

{

public Student[] getStudentData()

{

studentData sd = new studentData();

return sd.getStudentData();

}

}

public class studentData

{

public Student[] getStudentData()

{

List<Student> students = new List<Student>();

string strConnectionString = @”Data Source=THOSTHISINS02\SQLEXPRESS;Initial Catalog=test;Integrated Security=True;Pooling=False”;

SqlConnection sqlConnection = new SqlConnection(strConnectionString);

sqlConnection.Open();

SqlCommand sqlCommand = new SqlCommand(“getStudents”, sqlConnection);

sqlCommand.CommandType = CommandType.StoredProcedure;

SqlDataReader sdReader = sqlCommand.ExecuteReader();

while (sdReader.Read())

{

Student st = newStudent();

st.studentId = sdReader.GetValue(0).ToString();

st.studentName = sdReader.GetValue(1).ToString();

st.classId = sdReader.GetValue(2).ToString();

st.subjectName = sdReader.GetValue(3).ToString();

students.Add(st);

}

return students.ToArray();

}

}

Compile the solution bring to the browser directory listing and click on the service file will show the sample testing script.

                 

Browse the service wsdl (web service definition language – http://localhost:1237/studentService.svc?wsdl) in the web browser will shows the structure of the service in XML

                      

I have tried this feature with Visual Studio 2010 beta version WCF testing client windows opened automatically ‘start debug’ with service file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s