Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is it possible to make call to WCF service from WebApi Controller?
For example lets say we have some random WCF Service that returns some data on call. Can we create WebApiController with Get method that gets this data from WCF?
What are possible ways to preform something like this, and some tutorials and examples are more then welcome, because I was unable to find them online.
Thanks! :)
Posted

1 solution

Hi,
Yes, it can be done . Below is a prototype code.
Ex:
C#
[HttpGet, Route("Employee/id/{id}")]
public EmployeeDetail GetEmployee(int id)
{
   //Create the client and provide the binding.
   //The WCF service (Employee Service) should be part of the service reference to the WebAPI project
    EmployeeServiceClient  client = new EmployeeServiceClient("binding");
    //Here Employee is a DataContract of the Employee Service.
    Employee emp =  client.getEmployeeDetail(id);
    //Convert the emp object to more user friendly object if required. else pass it directly.
    EmployeeDetail empDetail = ConvertEmployee(emp);
    return EmployeeDetail;
}

Thanks
Srikant
 
Share this answer
 
v2
Comments
Nemanja M 23-Jan-15 5:02am    
So your suggestion is to create some kind of proxy instance from WCF service inside WebApi method? :)
SrikantSahu 23-Jan-15 5:15am    
yes, you can create a proxy class for the WCF service.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900