WCF数据服务-代理中间层服务 [英] WCF Data Service - Proxy mid-tier service

查看:82
本文介绍了WCF数据服务-代理中间层服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在研究的项目是经典的3层体系结构.第1层是数据库服务器,第2层是应用程序服务,第3层是表示层(网站).

The project we are working on is a classic 3 tiered architecture. tier 1 being the database server, tier 2 the application services and tier 3 the presentation tier (a web site).

在应用程序服务层中,我有一个项目,其中包括实体框架模型和基于WCF数据服务的服务,该服务公开了模型中的实体,例如:

In the application services tier I have a project that includes an entity framework model and a WCF data services based service that exposes the entities within the model e.g.:

public class DataService : DataService< PortalEntities >

这是一个完整的OData服务,可以通过URI查询,例如:/dataservice.svc/mytable?$filter=contains(fieldname,’string’).这对于使用jQuery开发任何东西的人来说非常有用,因为他们要做的就是定义查询.问题在于该服务属于中间层,因此外界无法看到.

This is a fully fledged OData service that can be queried through the URI e.g.: /dataservice.svc/mytable?$filter=contains(fieldname,’string’). This is great for the guys developing anything using jQuery as all they have to do is define the query. The problem is that this service is a mid-tier so it cannot be seen by the outside world.

我正在尝试的解决方案是在网站上公开另一个WCF数据服务,该网站公开由服务引用创建的实体.如果将服务引用添加到中间层服务,它将为我提供一个数据上下文,表明该数据上下文正在新的WCF数据服务中使用:

The solution that I am trying is to expose another WCF data service on the web site that exposes the entities created by the service reference. If I add a service reference to the mid-tier service it gives me a data context that data context is being used in the new WCF Data service:

public class DataService : DataService< PortalEntities >

我确实必须覆盖CreateDataSource:

I do have to overwrite the CreateDataSource:

protected override PortalEntities CreateDataSource()
{
    return new PortalEntities(GetMianModelServiceUri());
} 

新服务确实像代理一样工作,并且确实返回公开的实体(查询.../Services/OData/DataService.svc/tbl_Country正常工作).

The new service does act like a proxy and does return the entities exposed (the query .../Services/OData/DataService.svc/tbl_Country works fine).

但是,当查询传递给服务时,例如:.../OData/DataService.svc/tbl_Country?$select=Name,它将引发未实现的异常.

But when a query is passed to the service e.g.: .../OData/DataService.svc/tbl_Country?$select=Name it throws a not implemented exception.

关于如何扩展网站服务以使其支持与中间层服务相同的查询的任何想法?

Any ideas on how to extend the web site service so that it supports the same queries as the mid-tier service?

推荐答案

如果不需要更改数据服务器的形状或功能,则应该能够简单地转发请求和响应,就像透明地HTTP代理.您可能需要做的唯一区别是调整服务URL.由于代理服务将具有与真实服务不同的基本URI,因此有效负载将包含真实服务URI(在链接等中),这将不起作用.您可以通过为您的真实服务使用自定义主机并对其URI撒谎来解决此问题.这是通过IDataServiceHost2接口完成的,您从AbsoluteRquestUri和AbsoluteServiceUri属性返回新" URI.不错的接口实现示例(尽管出于不同的目的)在这里:

If you don't need to change the shape or functionality of the data server, you should be able to simply forward the requests and responses, just like a transparent HTTP proxy. The only difference you might need to do is to tweak the service URL. Since the proxy service will have a different base URI than the real service, the payload would contain the real service URIs (in the links and such), which would not work. You can workaround this by using a custom host for your real service and lie to it about its URI. This is done through IDataServiceHost2 interface, you return the "new" URI from the AbsoluteRquestUri and AbsoluteServiceUri properties. Nice sample of an implementation of the interface (although for a different purpose) is here: http://blogs.msdn.com/b/tom_laird-mcconnell/archive/2010/01/18/using-ado-net-wcf-data-services-for-streaming-infinite-event-result-sets.aspx.

如果您需要更改形状或功能,那么您确实需要真正的分层.

If you need to change the shape or functionality, then you really need a true layering.

将一个WCF数据服务分层放置在另一个之上目前非常困难.由服务器"生成的LINQ表达式树并不总是被客户端" LINQ提供程序理解.这就是您遇到的问题.

Layering one WCF Data Service over another is currently rather hard. The LINQ expression trees generated by the "Server" are not always understood by the "Client" LINQ provider. That's what you're running into.

有一个原型(更像是一个实验),通过重写表达式树在某种程度上使这项工作有效.它是OData Provider工具包的一部分,您可以在此处下载 http ://www.odata.org/developers/odata-sdk#/media/7579/odataprovidertoolkit.zip . (位于实验"文件夹中的AstoriaOverAstoria项目中.)

There's a prototype (more like an experiment) of making this work to some extent by rewriting the expression trees. It's part of the OData Provider Toolkit which you can download here http://www.odata.org/developers/odata-sdk#/media/7579/odataprovidertoolkit.zip. (It's in the Experimental folder, AstoriaOverAstoria project).

但是请注意,这实际上只是一个实验,用来说明需要解决的问题等等.我绝对建议不要在任何生产环境中使用它.

But please be aware that this is really just an experiment to show what kind of issues are there to be solved and so on. I definitely recommend to NOT use it in any kind of production environment.

这篇关于WCF数据服务-代理中间层服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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