【问题标题】:Call another WCF Data Service from WCF RIA Service using Entity Framework使用实体框架从 WCF RIA 服务调用另一个 WCF 数据服务
【发布时间】:2011-10-24 09:47:43
【问题描述】:
我想使用 WCF RIA 服务从我的 Silverlight 应用程序访问数据。但是,数据不是从本地数据存储提供的,而是来自另一个 WCF 数据服务(我正在访问外部 CRM 系统)。我不想直接访问外部服务,因为我必须在我的 RIA 服务中混合来自多个数据源的数据。
这可能是实现这一目标的最简单方法吗?一些 C# 中的源代码将不胜感激。
编辑:
中心问题是如何以简单的方式从外部服务填充实体。有一个related question,但是答案并没有解决我的问题。
【问题讨论】:
标签:
c#
silverlight
entity-framework
wcf-ria-services
wcf-data-services
【解决方案1】:
我认为您的困惑可能是用于添加 RIA 服务的 Visual Studio 向导假定您将对数据使用 EntityFramework。我认为您不想从第二个 WCF 服务的数据中创建 EF 模型。相反,创建您的 RIA 服务以直接从 DomainService 派生并覆盖您需要的方法。在每个查询方法中,只需查询远程服务并将结果返回给 Silverlight 客户端。为了使 RIA 服务魔术代码生成工作,您需要在您的应用程序中定义一组 DTO 对象,这些对象包装来自远程 WCF 服务的结果。
这是一个快速示例。注意 - 我只是编造这个来说明我的意思。您将需要调用您正在使用的实际服务并构建错误处理、输入检查等。
namespace YourApp.Web
{
[EnableClientAccess]
public class WcfRelayDomainService : DomainService
{
public IQueryable<Restaurant> GetRestaurants()
{
// You should create a method that wraps your WCF call
// and returns the result as IQueryable;
IQueryable<MyDto> mydtos = RemoteWCF.QueryMethod().ToQueryable();
return mydtos;
}
public void UpdateDTO(MyDto dto)
{
// For update or delete, wrap the calls to the remote
// service in your RIA services like this.
RemoteWCF.UpdateMethod(dto);
}
}
}
希望对您有所帮助!有关更多提示,请参阅 How to set up RIA services with Silverlight 4.0 and without EF。