如何从ASP.NET WebApi调用WCF服务 [英] How to make call to WCF service from ASP.NET WebApi

查看:496
本文介绍了如何从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


这篇关于如何从ASP.NET WebApi调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆