Odata compression - Is there ANY support? (WCF Data Services 5.0 for WinRT) - wcf

Context:Currently using WCF Data Services 5.0 with Odata for a Windows 8 Store application.
Have been trying to compress the OData coming over the wire from my service today and running into plenty of issues surrounding support.
binary serialization isn't supported.
json isn't supported (library throws 'application/json' is currently not supported in Content-Type header)
json lite isn't released yet
adding gzip requests in the http response works (server returns compressed) - but client side lib doesn't seem to want to deserialize it. (xml parsing exceptions)
My final attempt today was to try and get a hold of the http response, and to manually deserialize the stream myself (GZipStream isn't available but i know there are 3rd party alternatives) - but I couldn't find ANY Microsoft.Data.Services.Client.WindowsStore events/overrides that would expose the stream to me.
I wanted to implement OData to leverage the performance and efficiency gains over standard WCF Web services - but with no way to compress the stream over the wire i don't get any.
Does anyone have any experience with this, or advice of how to approach?

Have you followed the same steps here to get json response.The client has to send MaxDataServiceVersion header:
http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx
I also checked that Odata 5.1.0 has been released on nuget. This has the better json support:
https://nuget.org/packages/Microsoft.Data.Services/5.1.0
Your other option is to use asp.net web api and try the odata support:
http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData

Related

Header propagation with Flurl and DotNetCore

I've really enjoyed using Flurl the last year but have encountered a problem that Im hoping I can solve using Flurl if possible and not resort ripping it out and using IHttpClientFactory and HttpClient from System.Net.Http
I've got a DotNetCore 3.1 API and our client is calling these APIs with custom headers. "x-activityid" as an example. My API calls out to an external API and so I've created a separate Client class where im calling the endpoints on the external API using Flurl.
I need to propagate some of the headers from the requests incomming to my API to the requests I make to the external API that Im calling using Flurl.
Some related links:
Header propagation using ASP.NET Core
Make HTTP requests using IHttpClientFactory in ASP.NET Core
The whole idea of header propagation depends on awareness of some HTTP server context from which to grab the incoming headers, which is why ASP.NET Core can support such a feature directly while Flurl, a stand-alone library that often gets embedded in things like Xamarin apps, cannot.
But all is not lost, because Flurl is really just a wrapper around HttpClient. To get this feature to work without giving up Flurl, just wire up header propagation in ASP.NET Core exactly as prescribed, allow it to inject HttpClient instances into your service classes, then wrap those instances with Flurl inside those classes. Note that you'll need to adapt the pattern of using FlurlClient directly, as opposed to building calls off URL strings, if you're not doing that already.

Using Wcf with gzip compression

We have a wcf service that takes large amount of data. As a requirement we want to take the request objects gzipped and response them using the same compression. How can I do this? I couldn't find any document describing this issue.
How can I develop a wcf soap service that works like that and how to consume this kind of services?
Microsoft has a sample that demonstrates this (you can compile their code, and simply plug it into your application)
GZip Encoder Sample
There is a more complete sample in the WCF/WF/Cardspace samples here:
Windows Communication Foundation Samples
If you look in WCF/Extensibility/MessageEncoder/Compression, there is a more complete sample with an example client and server
Good article : HTTP COMPRESSION in IIS 6 and IIS 7 using Service Account
Check : HTTP Compression
Enable Gzip compression in IIS 6.0 for ASP.NET 2.0 websites

WCF Web Api vs WebHttpBinding

I'm new to WCF RESTFull services developpment and I'm looking for some usefull information and your experience feedback about using webHttpBinding compared to the new WCF Web API http://wcf.codeplex.com/.
What I'm looking for is to know about the shortcomings of webHttpBinding and therefore why to use the new Web api and especially what problems the new API resolves.
If you could point me to some blog posts comparing both of them or just talking about the problems when using webHttpBinding I would appreciate. Thank you in advance.
Main shortcomings I would say is that the webhttpbinding makes it difficult to handle HTTP specific concerns. It works great if all you are doing is passing an object over HTTP that is serialized into XML or JSON and which may be transported over different formats.
HTTP is much more than a simple transport protocol for XML and JSON, it is an application layer protocol with rich semantics. Web API is specifically targetting folks that want to build systems over HTTP that fully levergage HTTP's richness.
Web API embraces that HTTP Resources can have a multitude of representations based on the needs of different clients. One end of the spectrum could be a dumb browser that just talks to a service using a Form url encoded post and a GET, while the other end could be a more rich client that uses Atom/OData or a hypermedia based media type.
Web API embraces that there are other HTTP specific concerns like conneg, etags, etc which allow better leveraging intermediary web servers.
Web API is designed with more testability in mind, thus you can address working with HTTP messages or other concerns in a more testable manner.
Web API has a more simplified configuration story.
You can read more about the rationale here: http://blogs.msdn.com/b/endpoint/archive/2010/11/01/wcf-web-apis-http-your-way.aspx
The most significant difference for me is the change in programming model. You no longer write 'services' which expose 'operations' bound to HTTP idioms (GET, POST etc.). With Web APIs you create 'resources' (POCOs) with which your clients can interact.
Web APIs seem to be better at handling various custom media types (like PNG images for example).
Last but not least, Web APIs are far better suited for automated testing. For instance, you no longer have to use static context classes to access HTTP concepts such as response codes. You use POCO request and response classes which can be easily instantiated in automated tests using old-style new() operator.
I agree with Ladislav that Web APIs are just a preview now and building application on top of it can be both risky and forbidden by the means of license agreement (but I haven't checked that).
Have you considered #serialseb's OpenRasta? It is stable and offers very nice programming model for building RESTful services.
The Web API is something like possible future of REST development in WCF. It is just preview which can significantly change before final release (probably in next version of .NET framework). So if you want to build production REST service you should use webHttpBinding.
Available information about Web Api can be found for example on .NET Connected Framework team's blog and on the site you mentioned. It is simplification and extension of current REST API.
Web API provides a REST-friendly HTTP based API. Web API uses the patterns of MVC and is going to be very familiar to ASP.NET MVC developers. Web API can leverage the capabilities of HTTP as an application layer protocol, returning resources in multiple representations (XML, JSON, HTML etc.) according the the client's request headers.
On the other hand WCF webHttpBinding uses the patterns of WCF, and is going to appeal more to the WCF developer - ServiceContracts, OperationContracts, comprehensive (or overweight, depending how you look at it, config file), ability to self-host outside of IIS.
One of the things I like about Web API is the ability to use dynamic types to escape the constraints of the type system. I also like the default exception behavior in Web API - contrast WCF webHttpBinding where, by default, exceptions bubble up as HTTP 500 + an HTML payload (yuk!).
Its nice to have the choice between two excellent technologies here. I wouldn't describe Web API as 'newer' or 'better' that WCF, as this implies its a replacement technology and that WCF webHttpBinding is legacy, which I don't believe is true.
I chose to use WCF webHttpBinding recently to expose a JSON API for an existing WCF SOAP service. I believe it was a good choice because it fitted that style of that existing solution and minimized the amount of change required.

How to upload a file to a WCF Service?

I've build a WCF Service to accept a file and write it to disk. The front-end consists of a page with SWFUpload which is handling the upload on the client side. Apparently, SWFUpload posts the data with a Content Type of: multipart/form-data.
I would think this ok but on the Service side I get an error. The error is "ProtocolException" expecting text/xml. I've tried different message encodings in the bindings but nothing seems to work.
How can I get this file uploaded using multipart/form-data?
#jdiaz,
#JasonS is right, to upload file you need to transfer it as a byte stream. You need to use WCF streaming. For example on how to upload file via WCF see an article from http://kjellsj.blogspot.com
What you want to use is probably MTOM, if you want it to be standard. Using this, you can have MIME multiparts messages.
You then have to read the file as a stream and stuff it into one of the parameters of the request.
It might be that your WCF service targets .NET Framework 3.5 and your IIS is running on .NET Framework 4.0. In this case (framework mismatch) you need to modify your service.
I believe you are going to have to tranfer the file as a byte array to WCF. You will need to handle the post from SWFUpload and convert to a byte array before sending to your service.

WCF REST Caching - Client Side & Server Side

I have wirtten a RESTful WCF Service. Incorporating E-Tags, expires headers.
The caching works great when using it from a browser. However how does the caching work when calling it from a WCF Channel Factory or .NET Web Request Objects?
So in the scenario where I have my website calling the WCF restful service when a 304 not modified response is returned to me. How do I handle this. The browser detects this fine and returns the unmodified version from its cache.
However when the client is not the browser do I need to write my own version of the cache similiar to the way the browser caches?
Any help or insight would be much appreciated.
Yes, you're going to have to handle that yourself, same as that you're responsbile for sending the datetime in the request, so the server can determine if there was a change. I would look at the RSS Bandit source for a sample implementation.
We have a sample that illustrates how to do this (using .NET 4) http://code.msdn.microsoft.com/cannonicalRESTEntity