This article will give you an introduction to Windows communication foundation services and teach you how to create a WCF service library with basic WCF service example. And this will also tell you how to test WCF services using WCFTestClient.
With WCF services you can do all which you could do with ASMX web services, .Net remoting, use MSMQ for Message queuing, COM+ applications. Functionality of any combination of these technologies can be by using WCF services. WCF gives you several advantages over these technologies.
Creating WCF service follow the below steps.
namespace NorthwindServices
{
[ServiceContract]
public interface IProducts
{
}
}
Add methods to interface. In WCF the method which is accessible by thee client is called as OperationContract. Now you can add the OperationContracts for product interface.
[ServiceContract]
public interface IProducts
{
[OperationContract]
public string GetProductName(int productID);
[OperationContract]
public int GetProductQty(int productID);
[OperationContract]
public string GetCategoryName(int productID);
}
Add a service class where you can write your operation logic. Add a new class to the project. Name it as ProductService. Inherit the service contract IProducts.
public class ProductService : IProducts
{
public string GetProductName(int productID)
{
throw new NotImplementedException();
}
public int GetProductQty(int productID)
{
throw new NotImplementedException();
}
public string GetCategoryName(int productID)
{
throw new NotImplementedException();
}
}
For this tutorial, we will use XML file as our datasource. Add a new XML file with name products.xml to C drive. Add the below data to file.
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<Products>
<productID>1</productID>
<productname>Chai</productname>
<categoryID>1</categoryID>
<UnitsInStock>39</UnitsInStock>
<CategoryName>Beverages</CategoryName>
</Products>
<Products>
<productID>2</productID>
<productname>Chang</productname>
<categoryID>1</categoryID>
<UnitsInStock>17</UnitsInStock>
<CategoryName>Beverages</CategoryName>
</Products>
<Products>
<productID>3</productID>
<productname>Aniseed Syrup</productname>
<categoryID>2</categoryID>
<UnitsInStock>13</UnitsInStock>
<CategoryName>Condiments</CategoryName>
</Products>
</DocumentElement>
Add your method logic and return details. Basically you will implement the service contract here. Your complete service class would look like this.
using System.Linq;
using System.Xml.Linq;
namespace NorthwindServices
{
public class ProductService : IProducts
{
public string GetProductName(int productID)
{
XDocument doc = XDocument.Load("C:\\products.xml");
string productName =
(from result in doc.Descendants("DocumentElement")
.Descendants("Products")
where result.Element("productID").Value
== productID.ToString()
select result.Element("productname").Value)
.FirstOrDefault<string>();
return productName;
}
public int GetProductQty(int productID)
{
XDocument doc = XDocument.Load("C:\\products.xml");
int ProductQty = 0;
string strProductQty =
(from result in doc.Descendants("DocumentElement")
.Descendants("Products")
where result.Element("productID").Value == productID.ToString()
select result.Element("UnitsInStock").Value)
.FirstOrDefault<string>();
int.TryParse(strProductQty, out ProductQty);
return ProductQty;
}
public string GetCategoryName(int productID)
{
XDocument doc = XDocument.Load("C:\\products.xml");
string categoryName =
(from result in doc.Descendants("DocumentElement")
.Descendants("Products")
where result.Element("productID").Value == productID.ToString()
select result.Element("CategoryName").Value)
.FirstOrDefault<string>();
return categoryName;
}
}
}
Using endpoint clients can communicate with service. The Endpoint is an access point for clients to service functionality. Endpoint contains Address, Bindings Contract which is commonly called Endpoints ABC.
Endpoint Address provides information about its location.
Endpoint Binding specifies what kind of protocols should be used to communicate to clients.
Endpoint Contracts gives details about what agreement needed between service and client means what operations are available for the client.
Create an Endpoint for ProductService. Open App.config of service and locate <system.serviceModel><services>. Remove default endpoint for NorthwindServices.Service1 and add below <service> tag under <system.serviceModel><services>
<service name="NorthwindServices.ProductService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses
/NorthwindServices/ProductService/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding"
contract="NorthwindServices.IProducts">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
WCFTestClient is a simple GUI tool to test your WCF service. You can provide values to input parameters of service operations, invoke it, and see the response values. You can find the WCF Test Client (WcfTestClient.exe) at C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
For testing our ProductService run the application from the visual studio by pressing F5 and you will see the WCFTestClient with available operations.
Double click on any operation, provide value for the input parameter and click invoke. You will see the response in Response block.
With this your service is ready for hosting WCF service in IIS or in hosting in Windows service.
If I want to pass class object as parameter ..how would you do?
you can use faultcontract with DataContract for more details see https://geeksarray.com/blog/handling-wcf-service-exceptions-using-fault-contracts
sorry previous one was for FaultContract to handle exception for DataContract see below article https://geeksarray.com/blog/wcf-datacontract-with-enum-example
Excellent. Please add that the WCFTestClient (in Visual Studio 2015) will automatically run from the debugger (F5) only when the wcf service library is marked as the Startup project when there are multiple projects in the solution. Great tutorial.
there is one more extra step that we need to do, attach the bindingConfiguration & behaviourConfiguration to endPoint in the app.config / web.config Thanks Kumar Vaddadi 9849230576