Monthly Archives: November 2011

WCF Streaming – slides and code

I held a presentation about WCF Streaming last Saturday, November 26th, at Microsoft HQ Bucharest. I illustrated WCF Streaming in a small client/server application which was supposed to (and in the end implemented it all) :

  • Show all files available on the server in the client application
  • Allow the end user to upload a file (up to 2GB) to the server
  • Allow the user to download a file from the server
  • Display a progress bar that would update in real-time showing the progress of the current transfer (upload or download)
  • Allow the user to press the “Stop” button to stop the current transfer (upload or download)

The code to achieve this in a simple (non-robust, not production quality etc.) manner is quite small : around 50 lines of code for the server and around 200 lines for the client. The WCF runtime takes care of the rest.

Points of interest (things for which I suffered and hopefully you won’t) :

  • Cassini (ASP.NET Web development) server does not support streaming, reports a cryptic (400 Bad request) error and it’s not documented at Microsoft!
  • It’s not enough at the server level to set the maxReceivedMessageSize at the binding element, you must also set it in the maxRequestLength attribute on the system.web/httpRuntime element if you host the service in a site.
  • Don’t try to define an operation with mixed types, that is, complex types that are decorated with MessageContract and any other types (including System.String). If one is MessageContract then all have to be. Found out the hard way, at runtime (not compile time)
  • In order to get the folder path for a WCF application you must use HostingEnvironment.GetApplicationPhysicalPath.
  • In .NET 4 there is a CopyTo method on the Stream class which simplifies copying data from a stream to another.
  • Opt in for asynchronous method generation for the client-side WCF proxies

You can find below the PowerPoint slides and the code archive attached to this post.

WCF Streaming – slides

WCF Streaming – the code

IT community meeting (CodeCamp & ITSpark)

Saturday, November 26th, there will be a session of presentations at Microsoft’s Bucharest Headquarters.

Free entrance, drinks and lunch on the house.

The event agenda :

  • 09:30 – 10:00 Arrival
  • 10:00 – 11:00 MVC / EF (Andrei Ignat)
  • 11:00 – 12:00 WCF Streaming (Andrei Rinea)
  • 12:00 – 13:00 SQL Server Denali (Cristian Lefter)
  • 13:00 – 13:30 LUNCH
  • 13:30 – 14:00 A lap around Windows 8 (Mihai Nadăș)
  • 14:00 – 15:15 Hyper-V 3.0 și SCVMM 2012 (Valentin Cristea & Răzvan Rusu)
  • 15:15 – 16:30 Office 365 și Lync Online & On-Premise (Alexandru Dionisie & Paul Roman)

I will be presenting WCF Streaming at 11:00. But I assure you the other presentations will be just as interesting as this!

Come and join us and you won’t regret it! But first register at the event site first! Only 22 seats available at the time of the writing.

Hope to see you there!.

WCF service local path

Developing that small WCF presentation that I was talking about earlier, I got stumped on trying to get the local path. The server needs to access the App_Data folder to handle uploads and downloads but it needs the base path for that.

No, you don’t get HttpContext (HttpContext.Current is null) although the service is bound over basicHttpBinding.

Luckily I’ve found via StackOverflow that there is HostingEnvironment.ApplicationPhysicalPath which will help you.

Therefore a simple

public class Service : IService
    private readonly string _dataFolder;

    public Service()
       _dataFolder = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data");

.. will suffice.

Cassini and WCF streaming

I am preparing a small talk & demo on WCF streaming and I’ve tried a lot of things to get it started and working (the practical demo).

I was trying to showcase uploading and downloading large files, in an async manner with progress report and so on.

Turns out I get an exception, with HTTP code 400 Bad request no matter what I’ve tried :

  • transferMode : StreamedRequest, StreamedResponse or Streamed (Buffered works but it’s not streaming so…)
  • Tried using MessageContracts or plain Stream’s
  • .NET Framework 3.5 or 4.0
  • .. and many other things.

What was the issue? Well the damn development server (code named Cassini) !!! It seems another guy had the same issue, reported on StackOverflow and I was lucky to find it in the large WCF pile.

Running the same server project on the new IIS Express or plain old IIS makes it work.

Hope this will help someone too.. At all costs avoid Cassini (“ASP.NET Development Server”). This is not the first issue that this damn server introduces and surely not the last. I hope Visual Studi vNext will NOT include it anymore and will ship with IIS Express only.

IEnumerable.All() gotcha

Today as I was inspecting why some condition was evaluated to true instead of false I found out a strange thing. The code is something like :

var someList = new List<Person>();
if (someList.All(v => v.Age > 18))
    Console.WriteLine("All are 18 or older.");
    Console.WriteLine("At least one is less than 18.");

I was expecting that in case of an empty collection the All method would return false. But it doesn’t. The “All are 18 or older.” string would be printed.

In my case one more simple condition solved this issue :

    if (someList.Any() && someList.All(v => v.Age > 18))

After this I read the manual (RTFM) and according to MSDN (in a community comment however) :

It’s important to note that Enumerable.All() returns true for empty sequences

So it’s a “doh” moment for me.

Watch out for this in your code.