In this article, we will create a WCF service. Firstly, we will discuss some basics understanding of WCF services.
Basics of WCF services
WCF Services can be used to communicate with different type of applications using different protocols. if we want to use the WCF services we will have the basic understanding of its main components which is called ABC. Let's discuss these main components of WCF services one by one.
Address
A WCF provides a URI which can be used to locate the WCF services. This URI called the address of the WCF service.
Binding
Once we are able to locate the WCF service, The next point is how to communicate with WCF service like which protocol will be used to communicate. the binding which defines how the WCF service handle this communication, it also defines other communication parameters like message encoding
Contract
The contract defines what public data and interfaces provide by WCF service to the client. In other words what functionality WCF service will provide to the client.
Implementation
I am going to implement a WCF service in my MVCProject project which I create in my article .NET MVC5 .
Right click on the solution in solution explorer and click on add a new project. Select WCFService and name it ArithmeticService and press ok
Change the name of Service and interface.
Right click on the App_Code folder and add a new class with name ArithmeticOperation. Copy paste following code into that class. this class is actually a contract.
[DataContract]
public class ArithmeticOperation
{
int m_first;
int m_second;
public ArithmeticOperation()
{
m_first = 0;
m_second = 0;
}
public ArithmeticOperation(int first, int second)
{
m_first = first;
m_second = second;
}
[DataMember]
public int First
{
get { return m_first; }
set { m_first = value; }
}
[DataMember]
public int Second
{
get { return m_second; }
set { m_second = value; }
}
}
We will expose this class from our service so this class will have to be tagged with the attribute
DataContract
This attribute specifies that this data type can be used by consumers of this WCF service. Also, the public properties of this class will have to be tagged with the DataMember
attribute to specify that clients can use these properties and to indicate that they will be needing serialization and deserialization.in the next step Add following lines of code in the interface IArithmetic which was automatically created by the VS.
[ServiceContract]
public interface IArihmetic
{
[OperationContract]
ArithmeticOperation Add(ArithmeticOperation p1, ArithmeticOperation p2);
}
This interface will list the major functionalities provided by this service. We will have to tag this interface with the attribute
ServiceContract to specify
that this interface is being exposed by this service and can be used by the clients.
We will then have to write methods for all major operations provided by this service and tag them with the attribute
OperationContract
to specify that these operations can be used by the clients.
Now open the ArithmeticService class and add following lines of code into it.in this service, we will implement the interface which we created earlier.
public class ArihmeticService : IArithmetic
{
ArithmeticOperation IArithmetic.Add(ArithmeticOperation p1, ArithmeticOperation p2)
{
ArithmeticOperation result = new ArithmeticOperation ();
result.First = p1.First + p2.First;
result.Second = p1.Second + p2.Second;
return result;
}
}
In the Next step, we have to make this service visible to the client. To do this we need to specify the service behavior in the web.config.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
In th above line o code httpGetEnabled="true"
which enables this service to provide its metadata when a client requests for it.
Set the .SVC file as the startup page and run the website to see what happens.
congratulation! we have successfully created a WCF service.
Happy Programming :)
Comments
Post a Comment