This
article is for beginners who are starting in WCF. I will focus on
helping you to create and consume your first WCF Service in the simplest
of steps. The following is the procedure.
At the end of this article you should able to create your first WCF Service and consume that in a Console Application. I will continue to add further articles in this series to make WCF easier for you.
Project setup
To create a Service Contract:
Next you need to perform the following tasks:
ICalculator.cs
Press F5 to run the Console Client Application. You will get the desired results.
You have now created a Calculator WCF Service and consumed that in a Console Application. In further articles I will simplify other concepts for WCF for beginners. I hope you find this article useful. Thanks for reading.
Those are using VS2010, here given below the web.config updates..
Here
At the end of this article you should able to create your first WCF Service and consume that in a Console Application. I will continue to add further articles in this series to make WCF easier for you.
Project setup
- Launch any edition of Visual Studio 2010 or 2012.
- Create a project by choosing WCF Service Application project template from WCF tab.
- Delete default created IService1.cs and Service1.svc file.
To create a Service Contract:
- Right-click on the project and select "Add" | "New Item..."
- From the Web tab choose WCF Service to add.
- Let us give the service the name "Calculator.svc".
Next you need to perform the following tasks:
- Open ICalculator.cs and remove the "void DoWork()" function.
- Ensure the attribute of the Interface is set as "[ServiceContract]"
- Define the functions you want to create as part of the Contract
- Set the Attribute of the functions as "[OperationContract]"
ICalculator.cs
using System.ServiceModel;
namespace fourstepblogdemo
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double AddNumbers(double number1, double number2);
[OperationContract]
double SubstractNumbers(double number1, double number2);
[OperationContract]
double MultiplyNumbers(double number1, double number2);
[OperationContract]
double DivisionNumbers(double number1, double number2);
}
}
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.
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="fourstepblogdemo.Calculator">
<endpoint address="" contract="fourstepblogdemo.ICalculator" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</service>
</services>
Step 3: Implement Service
In this step we need to implement the service. To implement the service, open Calculator.svc and provide code to implement the functions defined in ServiceContract ICalculator.cs. Delete the implementation of the DoWork function from Calculator.svc and implement the services as in the following.
Calculator.svc.cs
Step 3: Implement Service
In this step we need to implement the service. To implement the service, open Calculator.svc and provide code to implement the functions defined in ServiceContract ICalculator.cs. Delete the implementation of the DoWork function from Calculator.svc and implement the services as in the following.
Calculator.svc.cs
namespace fourstepblogdemo
{
public class Calculator : ICalculator
{
public double AddNumbers(double number1, double number2)
{
double result = number1 + number2;
return result;
}
public double SubstractNumbers(double number1, double number2)
{
double result = number1 - number2;
return result;
}
public double MultiplyNumbers(double number1, double number2)
{
double result = number1 * number2;
return result;
}
public double DivisionNumbers(double number1, double number2)
{
double result = number1 / number2;
return result;
}
}
}
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:
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.
To view the metadata of the Service click on the WSDL URL. You may notice that the Service is hosted by "localhost".
Step 3: 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.
In the browser you will see the Service as in the following.
To view the metadata of the Service click on the WSDL URL. You may notice that the Service is hosted by "localhost".
Step 3: 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.
- 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".
You can consume the service by the client as in the following:
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 let us say you want to make a call to the Add function. That can be done as in the following:
So at the client side you can call all four of the functions of the Calculator Service as in the following:
And let us say you want to make a call to the Add function. That can be done as in the following:
So at the client side you can call all four of the functions of the Calculator Service as in the following:
using System;
using ConsoleClient.ServiceReference1;
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
CalculatorClient proxy = new CalculatorClient();
double addResult = proxy.AddNumbers(9, 3);
Console.WriteLine("Result of Add Operation");
Console.WriteLine(addResult);
double subResult = proxy.SubstractNumbers(9, 3);
Console.WriteLine("Result of Substract Operation");
Console.WriteLine(subResult);
double mulResult = proxy.MultiplyNumbers(9, 3);
Console.WriteLine("Result of Multiply Operation");
Console.WriteLine(mulResult);
double divResult = proxy.MultiplyNumbers(9, 3);
Console.WriteLine("Result of Division Operation");
Console.WriteLine(divResult);
Console.ReadKey(true);
}
}
}
Press F5 to run the Console Client Application. You will get the desired results.
You have now created a Calculator WCF Service and consumed that in a Console Application. In further articles I will simplify other concepts for WCF for beginners. I hope you find this article useful. Thanks for reading.
Login to add your contents and source code to this article
Article Extensions
Contents added by
Suthish Nair
on
Apr 17, 2013
Those are using VS2010, here given below the web.config updates..
|
Here
WcfService1
is my solution name.
Comments
Post a Comment