简体   繁体   English

如何在DataServiceContext WCF数据服务客户端中解码gzip?

[英]How can I decode gzip in a DataServiceContext WCF Data Services client?

We have a data service provider that is providing us with Dynamic Compression on a WCF Data Services site. 我们有一个数据服务提供商,可以在WCF数据服务站点上为我们提供动态压缩。 We need to make use of that compression because the raw data that we are pulling is more than 5Gb daily. 我们需要利用这种压缩,因为我们每天提取的原始数据超过5Gb。 Using gzip that will fall dramatically. 使用gzip将大大降低。

We have a client application which was created using the "Add service reference" wizard in Visual Studio 2010 which is based on the DataServiceContext class. 我们有一个客户端应用程序,它是使用Visual Studio 2010中的“添加服务引用”向导基于DataServiceContext类创建的。 I am able to specify the Accept: gzip, deflate header using the SendingRequest event, but how can I get the deserializer to decode the stream before it tries to parse the XML? 我可以使用SendingRequest事件指定Accept: gzip, deflate标头,但是如何在尝试解析XML之前让解串器对流进行解码?

In the same place where you set in SendingRequest that you want to get compressed data, just add the following: 在要获取压缩数据的SendingRequest中设置的同一位置,只需添加以下内容:

    e.RequestHeaders.Add("Accept-Encoding","gzip, deflate");
    ((System.Net.HttpWebRequest)e.Request).AutomaticDecompression = 
        (System.Net.DecompressionMethods.GZip | 
           System.Net.DecompressionMethods.Deflate);

Reference: http://social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/65127110-1213-45fa-a2ef-8bbd069d1294 参考: http : //social.msdn.microsoft.com/Forums/en/adodotnetdataservices/thread/65127110-1213-45fa-a2ef-8bbd069d1294

I couldn't figure out how to do this with the provided framework tools so I had to roll my own. 我无法弄清楚如何使用提供的框架工具来做到这一点,所以我不得不自己动手做。 First is the method that does the actual request that shows how to specify the header that requests gzip encoding and unzips the result. 首先是执行实际请求的方法,该方法显示了如何指定请求gzip编码并解压缩结果的标头。

  private static IEnumerable<dynamic> MakeHttpQuery(string uri)
  {
     var request = (HttpWebRequest)WebRequest.Create(new Uri(uri));
     request.Method = "GET";
     request.Accept = "application/json";
     request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");

     try
     {
        var response = request.GetResponse();
        var contentEncoding = response.Headers[HttpResponseHeader.ContentEncoding];
        var responseStream = response.GetResponseStream();

        if (!string.IsNullOrEmpty(contentEncoding) && contentEncoding.Equals("gzip"))
        {
           responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
        }

        var json = JsonObject.Parse(responseStream);
        var d = json["d"];
        if (!d.IsArray) return new JsonArray(new[] {d}).Values;
        else return ((JsonArray) d).Values;
     }
     catch (WebException webException)
     {
        log4net.LogManager.GetLogger(typeof(ProfileMediaDataInterface)).Error(webException);
        return new JsonArray(new JsonValue[] {}).Values;
     }
  }

The DynamicJson library is an open-source library that I wrote a while ago which came in very handy here. DynamicJson库是我不久前写的一个开源库,在这里非常方便。 You can very easily leave out the Accept: application/json header and then you will get back XML. 您可以很容易地省去Accept: application/json标头,然后返回XML。 In this case, something like linq-to-xml will also work, in very much the same way. 在这种情况下,类似linq-to-xml东西也将以相同的方式工作。

Next the client code. 接下来是客户端代码。 This shows how to construct the URL to pass to the MakeHttpQuery method also what to do with the result. 这显示了如何构造要传递给MakeHttpQuery方法的URL以及如何处理结果。

  public static List<BenchmarkListSqr> GetBenchmarkListSqr(string currencyCode)
  {
     return 
        MakeHttpQuery(
           CreateDataService()
           .BenchmarkList
           .Where(bm => bm.Currency == currencyCode)
           .ToString())
        .Select(x => new BenchmarkListSqr(
           x.Currency, 
           x.AssetClass, 
           ToNullDateTime(x.AvailableFromDate), 
           x.ID, 
           ToNullDateTime(x.InceptionDate), 
           x.Name, 
           x.Region, 
           ToNullDecimal(x.ShareID)))
        .ToList();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.Services.Client.DataServiceContext 超时属性不起作用 - System.Data.Services.Client.DataServiceContext Timeout Property is Not Working 在WCF数据服务中,如何限制使用者可见的实体? - In WCF Data Services, how can I limit the entities visible to the consumer? 支持5.2.0.0的WinRT DataServiceContext的服务参考(Microsoft.Data.Services.Client.WindowsStore) - Service reference for WinRT DataServiceContext that supports 5.2.0.0 (Microsoft.Data.Services.Client.WindowsStore) “System.Data.Services.Client.DataServiceContext.Execute <TElement> (System.Uri)&#39;无法从使用中推断出来 - 'System.Data.Services.Client.DataServiceContext.Execute<TElement>(System.Uri)' cannot be inferred from the usage WCF数据服务-客户端正在缓存结果 - WCF Data services - Client is caching results 如何在客户端(DataServiceContext)加密OData请求有效负载并在ServerSide解密请求? - How could I Encrypt OData Request Payload at Client Side(DataServiceContext) and Decrypting Request at ServerSide? 如何在WCF数据服务中使用多线程/ pfx加快速度? - How can I use multithreading / pfx in WCF Data Services to speed things up? 如何在WCF中使用客户端应用程序服务? - How to use Client Application Services with WCF? 如何使用客户端中的接口注入WCF服务? - How to inject WCF services using interfaces in client? 如何在WCF服务中重构数据合同? - How do I refactor data contracts in WCF services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM