Create:
Open Visual Studio, Go to the following menu, New -> Project -> WCF Service Application
Enter the application name and create the service
Delete the default Service and Interface(Contract)
Right Click the project and Go to Add -> New Item -> WCF Service.
Name the service and you will get a .svc file added along with the Interface file
There are three main things for a webservice as everyone tells,
1.Address
2.Binding
3.Contract
We will look into the Address and Binding laters, first we will discuss about the Contracts used for an WCF application There are types of contracts availables
DataContract
This will be usefult to handle your data, Each DataContract will be having a set of DataMembers
Example:
[DataContract]
Public class StudentContract
{
[DataMember]
public int StudentId {get; set;}
[DataMember]
public string StudentNamae {get; set;}
[DataMember]
public string CGPA {get; set;}
[DataMember]
public int Age {get; set;}
}
In this case we have defined Student as a Contract which is used to hold Student Data
Service Contract and OperationContract
This will define the operations or otherwise functionalities performed by that Webservice. This will be an Interface with method declarations, Interface is implied as Service Contract and methods/properties are implied as Operation Contracts.
Example:
[ServiceContract]
public interface IStudentService
{
[OperationContract]
StudentContract GetMarks(int studentId);
[OperationContract]
StudentContract GetStudent(int studentId);
}
Fault Contract and Message Contract
Fault Contract is used for error handling and defining custom errors. Message Contract is used for communicating directly with messages, they can be typed or untyped.
Writing the Service:
Once you finish defining Service Contract and DataContract, you can write your service in the .svc file
Service Class has to inherit from the SERVICE CONTRACT(in our case "IStudentService")
public class StudentService : IStudentService
{
public StudentContract GetMarks(int studentId);
{
//Your code to get Marks of the student by using StudentId
}
public StudentContract GetStudent(int studentId);
{
//Your code to get Student from StudentId
}
}
EndPoint:
After complete writing the service, we need to define the END POINT ( Where the service is going to be residing) For doing this, we need to change the web.config file and add the following values, Add the following Configuration settings to your web.config
"
This will act as service endpoint, there three main things you have to consider ServiceName -> You should give the name of the service with complete namespace
Contract -> You should give the name of the service contract with complete namespace
binding -> There are totally 9 different binding WCF Supports that includes WS Binding - wsHTTPBinding, BASIC Binding - basicHTTPBinding, TCP Binding, MSMQ Binding, IPC Binding and so on.
After completing the endpoint definitino, build the WCF Project and make sure that there is no build errors. AFter that WCF Service has to be hosted to use/consume it, there are three ways to host a WCF Service
1. IIS Hosted
2. Self Hosted
3. WAS(Windows Activation Service)
4. Windows Service Hosted
There is a clear information on hosting and consuming webservice is given in MSDN
CONSUMING WCF Services
You can create a Web/Windows/Console application to consume a WCF - webservice
1. Add the Service reference to the application by identifying the webservice using "Discover" in "Add Service Reference" dialog
2. This will automatically add the client service model entries in web.config/app.config,
make sure that you get something like this after adding the Service reference to the application.
Example:
"
this
If these entries are placed correctly in your web.config/app.config then you can go ahead use your service reference in code by adding "Using"
//Using entry for Service Reference using ServiceReference1;
//Instantiating the type from WCF Service and using it ServiceReference1.StudentServiceClient reference = new StudentServiceClient();