• Downloading/streaming Azure Storage private container blobs to AngularJS through .Net WebAPI

    Downloading/streaming Azure Storage private container blobs to AngularJS through .Net WebAPI

    When our Azure storage contains files that are meant to be publically accessible, it’s pretty trivial to deliver them to the end-user. We can either embed such items (e.g. images) or simply add links which point to them (e.g. pdfs) because Azure provides direct links to them. But what happens when these files contain sensitive data that is not meant for just anyone? Perhaps some kind of reports?

    Well, it gets a bit more complicated.. Since these files don’t have publically accessible URI’s any more, there are several steps which we need to go through:

    • Authenticate against Azure using SDK
    • Load the file into MemoryStream
    • Deliver the stream to the client (browser)
    • Convert the byte array into an actual file on the client-side and simulate “downloading”

    Since we’ll need more than just the MemoryStream, we’ll wrap it together with the file metadata into a model object.

    We will use AzureProvider class to authenticate against Azure, download the file from Azure and to create the model object.

    Our actual controller will inherit from this BaseApi class which contains a custom IHttpActionResult method which we can name AzureBlobOk. This is something pretty reusable so it’s good to have it at hand in your base class. What it does is it sets up all the content headers and it attaches the stream as the response content payload . It also returns HTTP status 200 which means OK – everything went fine.

    The actual controller is pretty simple..

    On the client side, we’ll need the following service to actually convert the byte array that we got from the API into something meaningful. I tried various approaches, but in the end decided to use FileSaver.js which “implements the HTML5 W3C saveAs() FileSaver interface in browsers that do not natively support it”. What it will do is turn the byte array into an actual file and prompt the user to download it.

    This service can easily be consumed by injecting it into your AngularJS controllers and calling the .getBlob() function which will do all the heavy lifting for you.

    Hope this helped, enjoy! :)

  • PdfSharp/MigraDoc to Azure Storage in-memory upload

    PdfSharp/MigraDoc to Azure Storage in-memory upload

    From my (somewhat limited) experience PdfSharp/MigraDoc seems like a pretty fine and powerful library for creating .pdf documents, but it’s documentation – not so much. It’s a bit all over the place and with multiple different NuGet versions/builds and outdated StackOverflow code samples not really helping the situation.

    However, creating a .pdf document in-memory and uploading it straight to Azure is not really that complicated. When might this be useful? For example when you need to generate a report but instead of immediately giving it to the user it just needs to get stored for later access.

    Magic word we’re looking for is MemoryStream. We’ll use two classes – one which will take a MemoryStream and upload it to Azure (AzureProvider.cs), and another one which will create a very simple MigraDoc document (ReportProvider.cs) which you can then build upon and then feed that document to the AzureProvider in the form of MemoryStream.

    The code is pretty straightforward and looks like this:

    Somewhat related – in the next post I’ll explain how to stream a file from Azure Storage private container through .net WebAPI to an AngularJS app.

  • Handling WebAPI exceptions with Angular http interceptor

    Handling WebAPI exceptions with Angular http interceptor

    It’s good to return meaningful HTTP status codes upon server exceptions. Whether it be invalid model state, conflict upon trying to create/update an object in the database, unauthorized access or something else it adds additional value and sends a message – one that’s different from generic 400’s or 500’s. These messages can then be used to take certain kinds of actions on the client side based on what happened.

    For example you could register a custom ApplicationExceptionHandler.cs inside your WebApiConfig.cs which would then be able to catch and handle any kind of exception thrown by lower server layers (such as business layer or the DB/ORM layer):

    You need to register ApplicationExceptionHandler in WebApiConfig.cs like this:

    You can also use something like ValidationFilter to take care of invalid model states in an easy way (by replying with a 422). After you have all your desired/needed exception handlers in place, it’s time to setup the client side. You can easily intercept every HTTP request and act upon responses from the server with an http request interceptor that looks like this:

    Now you have a centralized way to handle all kinds of WebAPI errors. Simply build upon this infrastructure and add whatever else you might need. In one of the future posts I’ll explain how to create basic html input element directives (for text, textarea, number, datetime and similar fields) which also seamlessly take care of modelState error messages by presenting them to the user just below the input element.

  • Handling .net’s model state with ValidationFilter attribute

    Handling .net’s model state with ValidationFilter attribute

    This is a simple ValidationFilter class which you can attach to any .net controller action method:

    What it provides is a “one-line” way to handle invalid model states. For example – instead of doing ModelState.IsValid check on each POST/PUT action, and then handling it manually again and again (by basically copying and pasting the same chunk of code all around your controllers) you can simply reuse the [ValidationFilter] attribute in front of your action and it will check whether the ModelState is valid. If it is, the action method will execute whatever code it has inside of it, and if it’s not the WebAPI will respond with a 422 (Unprocessable Entity) together with the associated ModelState error messages.

    For example if you had this model/action combination:

    …and you didn’t send any data to the WebAPI in your POST request, this is the 422 response that you would get back:

    This response can then be further parsed/handled by the client-side without problems.

  • KatKiss >> Cyanogenmod (Asus TF300T)

    KatKiss >> Cyanogenmod (Asus TF300T)

    Ever since my little TF300T rooting/flashing adventure I have had nothing but trouble with Cyanogenmod on my TF300T. I flashed 3 new versions throughout 3 months, but none of them were stable or fast enough. I have been a long time Cyanogenmod user and I like it a lot so that was my obvious choice. But the tablet would often become very, very slow, couldn’t connect to Wi-Fi or wouldn’t even be able to run certain apps. There are also other issues people reported and it seemed that things wouldn’t change in future.. I guess the device is simply not popular enough to have enough people care about and it maintain it’s CM version. Lately I started thinking I would either have to flash an older version of Android (perhaps even Asus stock crap) or retire the device to a dark corner of a drawer..

    Hopefully, I remembered seeing everyone giving praise to the KatKiss ROM so I decided to give it one last try and try KatKiss out. Man was this a pleasant surprise. The device is now bursting with speed! It’s really responsive, I haven’t noticed any bugs so far, it looks nice and I have the latest Android 5.1 features. It’s even faster than what it was with the old 4.x stock Android, it can now run anything (including Hearthstone! :D). I’m really happy as this means I will now be able to use the device with joy again.

    If you’re looking for a fresh rom for your TF300T – avoid CM and give KatKiss a try, it will save you time.

    Mad props to timduru! :)

  • Codealike bug squashing

    Codealike bug squashing

    I stumbled upon codealike a bit more than a month ago and decided to give it a try. I was interested in programming language usage percentages and coding vs. debugging vs. building statistics as well as some additional stuff that they track. After a while I even decided to get a premium for a month to see what the differences are and whether it would be worth for me personally to pay the fee.

    Today I discovered a potential bug in their system that I thought could probably lose them some money so I submitted a bug ticket. In 10 minutes, I got a response from their team. After that, we exchanged a few emails – they were happy that I notified them and in the end they even awarded me with one year of VIP subscription. Which is even better as it means I can now use their staging environment to experience the latest features they’re working on. That was quite cool of them and I guess it pays off to be a good guy. :)

Back to top