Saturday, July 4, 2009

Windows Communication Framework (WCF) Introduction

Windows Communication Framework in the Application Programming Interface in .Net 3.x for building Service Oriented Applications.

Create a new project by select WCF Service Application in the New Project Dialog.




















The project comprises of an Interface and a Class File.

















In the interface add Operation Contracts.



















Implement the interface in the Class Service1.cs.




public string Blog()
{
return "ShalvinPD.blogspot.com";
}

public int Add(int i, int j)
{
return i + j;
}







Now run the application.






Consuming the Service

We can consume a WCF service from any UI technologies say Windows Forms, Web Forms, WPF etc.
Go to Project, Add Service Reference.
In the Address copy paste the url from WCF Test Page. Give an appropriate name in the Namespace TextBox.






















Now your are ready to use the service just like consuming a dll.


Shalvin.Service1Client sc = new Shalvin.Service1Client();

private void btnBlog_Click(object sender, EventArgs e)
{
MessageBox.Show(sc.Blog());
}

private void button1_Click(object sender, EventArgs e)
{
int intSum = sc.Add(456, 345);
MessageBox.Show(intSum.ToString());
}









WCFwithout Interface

It is possible to create WCF service without an associated interface file.

[ServiceContract]
public class Service
{
[OperationContract]
public string Blog()
{
    return "ShalvinPD.blogspot.com";
}
[OperationContract]
public int Add(int i, int j)
{
    return i + j;
}
[OperationContract]
public List<string> Students()
{
    List<string> glsStudents = new List<string> { "Shalvin P D", "Praseed Pai", "Mathew K J", "Ashok Shenoy" };
    return glsStudents;
}
}


Silverlight Enabled WCF Service service for example comes without Interface.

No comments:

Post a Comment