使用ODataLib调用WCF数据服务服务操作和JSON [英] Using ODataLib to call a WCF Data Services service operation and JSON

查看:116
本文介绍了使用ODataLib调用WCF数据服务服务操作和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了广泛的搜索,但完全找不到关于如何使用ODataLib调用服务操作的任何好的示例或教程.我见过几个告诉我如何获取实体和实体集的方法,这很棒,但是为了调用服务操作.

I have searched far and wide and been completely unable to find any good samples or tutorials of how to call service operations using the ODataLib. I've seen several that tell me how to get entities and entity sets and that's great, but in order to call a service operation.

我几乎肯定我需要使用除ODataEntryODataFeedODataNavigationLink对象之外的其他东西.我在JustDecompile中打开了该库,并看到了诸如ODataActionODataFunction之类的类,但无法弄清楚如何使用它们.

I'm almost positive that I need to use something other than an ODataEntry, ODataFeed or ODataNavigationLink object. I've opened up the library in JustDecompile and see classes like ODataAction and ODataFunction but can't figure out how to use them.

我的需求非常简单:我需要调用WCF Data Services 5.0服务操作,并且需要使用JSON进行操作.

My needs are pretty simple: I need to call WCF Data Services 5.0 service operations and I need to do it using JSON.

我还知道即将发布的WCF数据服务客户端库将支持JSON,但是不幸的是,我必须像昨天一样对此进行编码.此外,出于绝望,我什至尝试使用 Mark Stafford的JSON light实现RC版本( gasp )样本,但并不太了解其工作原理.

I'm also aware that the upcoming release of the WCF Data Services Client library is going to support JSON but unfortunately I have to code this up like yesterday. Additionally, out of desperation I even tried implementing the RC version (gasp) using Mark Stafford's JSON light sample but didn't really understand how it works.

任何帮助或指导将不胜感激! (特别是如果出现在屏幕上,马克!)

Any help or direction would be GREATLY appreciated! (Especially if this comes up on your screen, Mark!)

谢谢! J

推荐答案

在ODataLib和WCF数据服务(取决于ODataLib)之间进行选择,因为客户端通常归结为需要控制.如果您的用例简单明了,并且需要最常用的功能,则WCF DS可能很适合.如果您需要高级功能或对如何读取有效负载进行精确控制,则ODataLib可能是更好的选择.话虽如此,Vitek已经从概念上介绍了如何使用ODataLib读取服务操作.

The choice between ODataLib and WCF Data Services (which depends on ODataLib) as a client often boils down to the need for control. If your use case is straightforward and you need the most common functionality, WCF DS is probably a good fit. If you need advanced functionality or precise control over how a payload is read, ODataLib may be the better match. All that said, Vitek has already covered conceptually how you would read a service operation using ODataLib.

在版本5.1中,WCF DS将使读取JSON Light变得更加容易.我将在本周的某个时候发布有关此内容的博客,但是您引用的示例是此RC需要做的事情.我放在一起的示例中只有一个新调用-对context.Format.UseJson(Func<Uri,ModelResolverResult>)的调用.让我们谈谈为什么必须首先进行此调用. OData(至少在Microsoft世界中是如此)与强类型配合使用效果很好. $metadata是OData描述其使用的数据模型的一个很好的例子.使用 Atom有效负载或什至是JSON Verbose有效负载,其中大部分类型信息都承载在有效负载中.使用JSON Light,目标是从传输"有效负载中剥离尽可能多的元数据.

WCF DS will make reading JSON Light much easier in version 5.1. I will blog about this sometime this week, but the sample you referenced is what you'll need to do for this RC. There is only one new call in the sample I put together - the call to context.Format.UseJson(Func<Uri,ModelResolverResult>). Let's talk about why this call is necessary first. OData (at least in the Microsoft world) plays nice with strong typing. $metadata is a good example of OData describing the data model it is working with. With an Atom payload or even a JSON Verbose payload, much of that type information was carried in the payload. With JSON Light the goal was to strip as much of that metadata out of "transport" payloads as possible.

如果元数据在带内不可用,则必须使其在带外可用.这是对UseJson的调用签名背后的基本要求.本质上,每当WCF DS需要元数据时,它都会去查找给定URI的模型.如果找不到该模型,它将最终将元数据拨满.我们要先行一步,因为它会使有效负载膨胀.我们可以通过告诉WCF DS提前解决模型的方式来抢占它.这就是大多数样本正在做的事情.

If the metadata isn't available in-band, it must be made available out-of-band. This is the fundamental requirement behind the signature of the call to UseJson. In essence, whenever WCF DS needs metadata it will go look up the model for a given URI. If it can't find that model it will eventually dial up the metadata to full. We want to preempt that as it will bloat the payload. We can preempt it by telling WCF DS how to resolve the model ahead of time. This is what the majority of the sample is doing.

按逻辑顺序遍历样本(是的,我知道还可以进行许多其他优化-样本主要是针对可读性进行了优化):

Walking through the sample logically (yes, I know there's a number of other optimizations that could have been made - the sample was mostly optimized for readability):

  • 如果过去没有解决过该模型
    • 构造一个新的XmlReader来调用EdmxReader.TryParse
    • EdmxReader.TryParse
    • 中的out参数命名一些值
    • 调用EdmxReader.TryParse
      • 如果调用成功,请将已解析的模型缓存在本地字典中(目前解析是一项昂贵的操作)
      • 如果通话失败,请汇总错误并抛出
      • If the model hasn't been resolved in the past
        • Construct a new XmlReader for the call to EdmxReader.TryParse
        • Name some values for the out params in EdmxReader.TryParse
        • Invoke EdmxReader.TryParse
          • If the call succeeded, cache the parsed model in a local dictionary (parsing is an expensive operation right now)
          • If the call failed, put together the errors and throw

          希望这是有道理的.如果没有,我很想听听为什么,这样我就可以使博客文章更加清晰.并且请记住,当我们将这些位发布到生产环境时,这将变得更加简单.

          Hopefully that makes sense. If it doesn't, I'd love to hear why so that I can make the blog post clearer. And remember, this will get much simpler by the time we release these bits to production.

          这篇关于使用ODataLib调用WCF数据服务服务操作和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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