Definition of WCF
Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF we can build secure, reliable, transacted solutions that integrate across platforms.
Description
- WCF inter-operate between WCF-based applications and any other processes that communicate via SOAP (Simple Object Access Protocol) messages.
- So how can a WCF service be used to communicate using different protocols and from different kinds of applications, for this we want to use a WCF service from an application.
- If we want to use a WCF service, we have to get answer for the three questions given below
2. How to access the service i.e protocols and message formats
3. What functionality does the service provides to the clients
- The answers for the above questions are known as the ABC of a WCF service.
needs to access the WCF service. This URI is called the Address of
WCF service. So we got answer to the first question.
Binding : We got the address of WCF service, so we know how to connect
to it from a client. Now we should think about how to communicate
with the service (Protocol wise). So Binding is what defines how the
WCF service handles the communication.It could also define other
communication parameters like message encoding etc. So this clears
the second question.
Contract : It is what defines the public data and interfaces that WCF
service provides to the clients. So we have got answers to all the
3 questions asked above.
Now lets go for your First WCF Project
Steps
1. Launch Visual Studio 2008 or above.
2. Create New Project -> WCF Service Application
3. Now your project MyFirstWCF will have Iservice1 and Service1. Replace
the codes of both with the one given below
Iservice1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyfirstWCF
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string Name);
}
}
See the Screenshot
See the Screenshot
4. Now Rebuild MyFirstWCF and Debug. You may get an output page in your browser
as shown below.
Now lets go for your First WCF Project
Steps
1. Launch Visual Studio 2008 or above.
2. Create New Project -> WCF Service Application
3. Now your project MyFirstWCF will have Iservice1 and Service1. Replace
the codes of both with the one given below
Iservice1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyfirstWCF
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string Name);
}
}
Service1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace MyfirstWCF
{
public class Service1 : IService1
{
public string GetData(string Name)
{
return string.Format("Hello: {0}", Name);
}
}
}
See the Screenshot
4. Now Rebuild MyFirstWCF and Debug. You may get an output page in your browser
as shown below.
If you are not getting the above output page and getting the below one,
don't worry just click on Service1.svc, you will get the required output.
The url shown above i.e localhost:22186/Service1.svc is the address of your service.
Note down this address , it is required for further steps.
5. So we have created a Service and now we need to create a New Project from
which we can access the service. So Right click on Main Project file on
Solution Explorer and Add -> New Project as shown in the below screenshot.
6. Now you have to add a Windows Form Application as shown below.
7. Design the Form as shown in the below screenshot
8. Here you will have to Add a reference to the service you have created. So just Right
click on the project MyForm from Solution Explorer and select Add Service Reference.
a) Here you have to give the Address of the Service you got from Step 4.
(Here its localhost:22186/Service1.svc ). Now Click Go.
b) Now under Services, the service you have created will be listed.
c) Click OK
9. This is the last step and your first WCF Project will be ready with this step.
Double click on Say Hello button and copy the code given below.
Form 1
Code
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Myform.ServiceReference1;
namespace Myform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Service1Client client = new Service1Client();
MessageBox.Show(client.GetData(textBox1.Text));
client.Close();
}
}
}
10. Now Rebuild and Debug your MyForm Project. Enter your name and click on
Say Hello, you will get a Messagebox with Hello added as a prefix to your name.
Hello is added by the service. See the screenshot of the output.
Conclusion
You have done your First WCF Project. If you have any issues or doubts in getting the output for this project, please feel free to drop a comment.
Thank you Tijo...It helped me a lot :-)
ReplyDeleteAmazing article.loved reading every bit.
ReplyDeleteThanks a lot
ReplyDeleteNice Description
ReplyDeleteThanks.
ReplyDeleteThanks good one.
ReplyDeleteAwesome description....Thanks a lot
ReplyDeleteyou're my hero,been struggling with this for 3 days now!
ReplyDelete