简体   繁体   中英

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. We need to make use of that compression because the raw data that we are pulling is more than 5Gb daily. Using gzip that will fall dramatically.

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. 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?

In the same place where you set in SendingRequest that you want to get compressed data, just add the following:

    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

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.

  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. You can very easily leave out the Accept: application/json header and then you will get back XML. In this case, something like linq-to-xml will also work, in very much the same way.

Next the client code. This shows how to construct the URL to pass to the MakeHttpQuery method also what to do with the result.

  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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM