如何从ASP.NET WebApi调用WCF服务

问题描述:

是否可以从WebApi控制器调用WCF服务?

例如,假设我们有一些随机的WCF服务,它会在调用时返回一些数据。我们可以用Get方法创建WebApiController,从WCF获取这些数据吗?

有什么方法可以预先形成这样的东西,一些教程和例子更受欢迎,因为我无法在网上找到它们。

谢谢! :)

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! :)



是的,可以做到。下面是原型代码。

Ex:

Hi,
Yes, it can be done . Below is a prototype code.
Ex:
[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;
}



谢谢

Srikant


Thanks
Srikant