1. Open Visual Studio and Select WCF service application.
Step 2: Expose Endpoints with Metadata
In this step you need to expose Endpoints and the Metadata of the service. To do this open the Web.config file. We are going to create one Endpoint with basicHttpBinding. We are adding a metadata Endpoint also to expose the metadata of the service. We need metadata at the client side to create a proxy.
2.delete Service.svc file from Sloution Explorer
3. right click on solution -> Add-> Add new Item and Select Wcf service File
Give File Name calculator.svc
4. Add Following Code In
ICalculator.cs
using System.ServiceModel;
namespace fourstepblogdemo
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Calculate (int NoOfDays, int SalPerDay);
}
}
5.
You have created a Service Contract above with four Operation Contracts. These contracts will be part of the Service Contract and exposed to clients. By this step you have created the ICalculator Service Contract.
Step 2: Expose Endpoints with Metadata
In this step you need to expose Endpoints and the Metadata of the service. To do this open the Web.config file. We are going to create one Endpoint with basicHttpBinding. We are adding a metadata Endpoint also to expose the metadata of the service. We need metadata at the client side to create a proxy.
<services>
<service name="Calculator.Calculator">
<endpoint address="" contract="Calculator.ICalculator" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</service>
</services>
6. Implement Service
namespace Calculator
{
public class Calculator : ICalculator
{
public int CalcSal(int NoOfDays, int SalPerDay)
{
int Salary = NoOfDays * SalPerDay;
return Salary;
}
}
}
As of now you have created the Service and configured the Endpoint. Now you need to host the service. There are many processes in which a WCF Service can be hosted. Some processes are:
- Managed Application
- IIS
- ASP.Net Web Server
- Windows Service
- App Fabric
In this article we are not going into the details of the WCF Service hosting and we will consider the simplest hosting option. Let us host the service in an ASP.Net Web Server. To host it press F5 in Visual Studio.
In the browser you will see the Service as in the following.
In the browser you will see the Service as in the following.
7: Consume Service
There are various ways a WCF SOAP Service can be consumed in various kinds of clients. In this article we will consume the service in a Console Application. Launch Visual Studio and create a Console Application project.
Now there are two ways you can create a proxy at the client side.
There are various ways a WCF SOAP Service can be consumed in various kinds of clients. In this article we will consume the service in a Console Application. Launch Visual Studio and create a Console Application project.
Now there are two ways you can create a proxy at the client side.
- Using svcuitl.exe at command prompt
- By adding Service Reference
In this article we will create a proxy at the client side using Add Service Reference. In the Console Project right-click on References and select the "Add Service Reference" option. In the Add Service Reference dialog copy and paste the address of the Service or if the WCF Service and Console Client project is in the same solution then click on Discover. If there is no error in the Service then you will find the Service Reference added as given in the following image. If you want you can change the name of the Reference. I am leaving here the default name "ServiceReference1".
after adding Service reference You can consume the service by the client as in the following:
- Create instance of proxy class
- Call different operations from service
You can create an instance of the proxy class as in the following:
and finally check your sevice.
I hope you find this article useful. Thanks for reading. :)







No comments:
Post a Comment