Creating “Hello World” WCF Application
In this article we will learn that how to create a simple WCF application which
is Hello World application.
Use .NET framework 4.0 and Visual Studio 2010 to create a WCF service
application
1)
Start Visual Studio 2010.
2)
On the File Menu, point to new and then click on
Project.
3)
In the New
Project dialog box expand visual c# node and then select WCF template.
4)
From Project type window select WCF service
application.
5)
Type Name and Location of project and then click
on OK button.
By performing these steps you can create a first WCF application with default
template.
Now rename name of the file IService.svc to HelloWorld.svc and IService.cs to IHelloWorld.cs. This is
not mandatory as I had done it on my sample you can put any name you want.
Creating ServiceContract
[ServiceContract]
attribute is used to create a contract. For creating WCF application you need to
create a ServiceContract. We use following codes to create a ServiceContract.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace HelloWorldService
{
[ServiceContract]
//This attribute is used to create a
ServiceContract.
public interface
IHelloWorld
//Create a interface which defines contract
{
[OperationContract] //Use
[OperationContract] attribute to declare a method which will be accessiable via
client.
string GetMessage();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace HelloWorldService
{
//Implement IHelloWorld interface and override
GetMessage() method displlay a message.
public class
HelloWorld :
IHelloWorld
{
public string
GetMessage()
{
return string.Format("Wow! You have successfully created first WCF
application.");
}
}
}
Now build application and then execute it by pressing F5 key. By performing
these steps you will see following dialog box which is known as WCF Test Client
window.
Now double click on getMessage() label followed by click on Invoke button and
you get desired result.
|