Pry it from my cold dead fingers

 

I usually try avoid getting into the OS / Browser etc drama, yawn….

But I found this blog post on Pluralsite tonight, and I can help but laugh, I’m with the 72% it turns out, I am asking myself did I pick that option because it was just funny however Smile

image

A little drama

Q: Will I use Windows8?  A: Yes I’ve been using it since developer preview.
Q: Am I bothered? A: Not really, it’s a new opportunity so I’m getting on board.
Q: Would I like the option to switch off Metro? A: Hell ya! (but I’m then back in Win7 ain’t I? maybe I sub consciously picked the correct answer).

Uploading a file in MVC4 C#5 .NET 4.5

 

Back on the bleeding edge again Hot smile I’m in the early stages of my next killer app and I’m investigating the pros and cons of using the new ASP WebApi.

One of the features of this so called killer app will be to upload pictures (nothing special I agree). But how would I do this for all the clients I hope to support (WinRT/WP7/Html5/IOS).

Let me first present the server that will be used for all these clients, I’ll then follow up with what I consider to be the simplest client a html5 browser!

Server

So I fired up VS11 and created a new MVC4 application using .net 4.5 / C#  and the WebApi template.

I then added a controller called FileUploadController.cs

   1:  using System.Collections.Generic;
   2:  using System.Linq;
   3:  using System.Net;
   4:  using System.Net.Http;
   5:  using System.Threading.Tasks;
   6:  using System.Web.Http;
   7:   
   8:  namespace MvcApplication16.Controllers
   9:  {
  10:      public class FileUploadController : ApiController
  11:      {
  12:          public async Task<IEnumerable<string>> PostMultipartStream()
  13:          {
  14:              // Check we're uploading a file
  15:              if (!Request.Content.IsMimeMultipartContent("form-data"))            
  16:                  throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
  17:                 
  18:              // Create the stream provider, and tell it sort files in my c:\temp\uploads folder
  19:              var provider = new MultipartFormDataStreamProvider("c:\\temp\\uploads");
  20:   
  21:              // Read using the stream
  22:              var bodyparts = await Request.Content.ReadAsMultipartAsync(provider);            
  23:          
  24:              // Create response.
  25:              return provider.BodyPartFileNames.Select(kv => kv.Value);            
  26:          }
  27:      }
  28:      
  29:  }

You can see from line 12 that I’ve made this operation async, you’ve really got to admire the simplicity of async/await construct in .net 4.5! In line 22 you can see that the compiler and some state machine magic allow the freeing up of the asp worker thread….. (If you have read my previous posts you may be a little confused now.. didn’t I say that Tasks will use use the same threadpool!?  have a look at this link for someone that pondered the very same concerns )

 

HTML5 Client

The client couldn’t have been easier, fist a look at it in the browser

image

   1:  <!DOCTYPE html>
   2:  <html lang="en">
   3:  <head>
   4:      <meta charset="utf-8" />
   5:      <title>ASP.NET Web API</title>
   6:      <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
   7:      <meta name="viewport" content="width=device-width" />
   8:  </head>
   9:  <body>
  10:      @using (Html.BeginForm("FileUpload", "api", FormMethod.Post, new { enctype = "multipart/form-data" }))
  11:      { 
  12:          <div>Please select some files</div>
  13:          <input name="data" type="file" multiple>
  14:          <input type="submit" />            
  15:      }
  16:  </body>
  17:  </html>

 

The important part above is using the enctype attribute, in fact line 10 loosely translates to

<form action="~/api/FileUpload" enctype="multipart/form-data" method="POST">
 

Don’t believe me? Then try VS11’s awesome new feature – page inspector

Right click on the html and choose view in page inspector

image

 

 

and we’re done! Of course in the real world we’ll use ajax with a few trick re sandbox, but here’s the response in the browser with xml.

image

I’ll hopefully follow up with the samples for the client list below when I get to the respective development machines.

  • WinRT (c#/xaml)
  • iPhone (objective c)
  • Android (java)

Async MVC Controllers

 

I previously wrote a post on MVC async controllers. Now I want to follow up on something that’s more cutting edge, unfortunately I’m one of those people that like nosing about with what new, how it will affect me and how I could leverage it in future etc., Often, I’ll readily admit it’s more bleeding edge than cutting edge (xaml support in Blend5 for example Thumbs down).

However in this post I want to show you something that you might agree is pretty nice.

A historical example

So lets take a look at the app we’re trying to build.

Client is a simple web form (yes it’s webforms but I’m trying to catch the non MVC microsofties too), the instruments are entered in the text box, fetch button is clicked and the result is output

image_thumb1

Now the workflow, for the purpose of this test I won’t be connecting to any database webservice etc, I’ve just added a delay of 10 seconds and and assign LastClose to RIC & " " & New Random().NextDouble()

image_thumb4

Here’s the code behind for the non-async button event handler

        protected void Button1_Click(object sender, EventArgs e)
        {
            Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(tbInst1.Text) || !string.IsNullOrWhiteSpace(tbInst2.Text), "Please enter values for instruments");
            // Make the call to the workflow to get the close for each instrument
            try
            {
                var args = new Dictionary<string, object>();
                args.Add("RIC", tbInst1.Text);                
                var res = WorkflowInvoker.Invoke(_getPricesWFDefinition, args);
                Label1.Text = res["LastClose"].ToString();
 
                args.Clear();
                args.Add("RIC", tbInst2.Text);
                res = WorkflowInvoker.Invoke(_getPricesWFDefinition, args);
                Label2.Text = res["LastClose"].ToString();
            }
            catch (Exception exp)
            {
                Trace.Warn(exp.Message);
            }
 
        }

So as you can see, we’re waiting at least 20 seconds for our page to return, nasty.
In theory we should be able to bring this down to 10 seconds as we can make the calls to the workflows in parallel.

Let me show you one way to “incorrectly” achieve this

        private void Option1()
        {
 
            var t1 = Option1TaskCreater(tbInst1, Label1);
            var t2 = Option1TaskCreater(tbInst2, Label2);
 
            Task.WaitAll(t1, t2);
 
        }
 
        private Task Option1TaskCreater(TextBox tb, Label lb)
        {
            var t1 = Task.Factory.StartNew(() =>
            {
                var args = new Dictionary<string, object>();
                args.Add("RIC", tb.Text);
                var res = WorkflowInvoker.Invoke(_getPricesWFDefinition, args);
                lb.Text = res["LastClose"].ToString();
            });
            return t1;
        } 

So we’re using the task library to make the two workflow requests async, and results look promising, down to about 10 seconds now… so any problems with doing this?

Well, tasks use threadpool threads to execute. So, the query will execute on a threadpool thread. To get true asynchronous execution(no worker threads blocked), we need to jump through a few more hoops. For webform people, there’s a very good explanation here. MVC people read on!

But Why/When use async?

The response to this question has been discussed many times in both books and magazines. ASP.NET uses threads from a common language runtime (CLR) thread pool to process requests. As long as there are threads available in the thread pool, ASP.NET has no trouble dispatching incoming requests. But once the thread pool becomes saturated-that is, all the threads inside it are busy processing requests and no free threads remain-new requests have to wait for threads to become free. If the jam becomes severe enough and the queue fills to capacity, ASP.NET throws up its hands and responds with a "Server Unavailable" to new requests.

Often to solve the problem of horizontal scalability more servers are added to the webfarm, however this only provided temporary relief to what in fact is an underlying design problem, it’s not a case of adding more processors(threads) but a case of using the threads more efficiently, as you can see from the diagram below, if you are CPU bound, there is little more you can do but add some more servers.

image_thumb2

So what is this telling us? Basically, if your app is I/O bound then you should use parallelism, if requests are computationally cheap to process, then parallelism is probably an unnecessary overhead.

If the incoming request rate is high, then adding more parallelism will likely yield few benefits and could actually decrease performance, since the incoming rate of work may be high enough to keep the CPUs busy.

If the incoming request rate is low, then the Web application could benefit from parallelism by using the idle CPU cycles to speed up the processing of an individual request. We can use either PLINQ or TPL (either Parallel loops or the Task class) to parallelize the computation over all the processors. Note that by default, however, the PLINQ implementation in .NET 4 will tie-up one ThreadPool worker per processor for the entire execution of the query. As such, it should only be used in Web applications that see few but expensive requests.

MVC4

In MVC4 it becomes even easier that my previous post on AsyncControllers, actually to me it looks quite like the WinRT async calls in .net 4.5 and C#5

Sample using task support for asynchronous controllers

You can now write asynchronous action methods as single methods that return an object of type Task or Task<ActionResult>.

e.g.

public async Task<ActionResult> Index(string city)
{    
    var newsService = new NewsService();    
    var sportsService = new SportsService();        
    return View("Common",        
                new PortalViewModel 
                {       
                    NewsHeadlines = await newsService.GetHeadlinesAsync(),        
                    SportsScores = await sportsService.GetScoresAsync()    
                });
}

In the previous action method, the calls to newsService.GetHeadlinesAsync andsportsService.GetScoresAsync are called asynchronously and do not block a thread from the thread pool.

Asynchronous action methods that return Task instances can also support timeouts. To make your action method cancellable, add a parameter of type CancellationToken to the action method signature. The following example shows an asynchronous action method that has a timeout of 2500 milliseconds and that displays a TimedOut view to the client if a timeout occurs.

[AsyncTimeout(2500)] 
[HandleError(ExceptionType = typeof(TaskCanceledException), View = "TimedOut")]
public async Task<ActionResult> Index(string city, CancellationToken cancellationToken) 
{    
    var newsService = new NewsService();    
    var sportsService = new SportsService();       
    return View("Common",        
        new PortalViewModel 
        {        
            NewsHeadlines = await newsService.GetHeadlinesAsync(cancellationToken),        
            SportsScores = await sportsService.GetScoresAsync(cancellationToken)    
        });
}
 
 
 

Conculsion

MVC4 Makes programming of async controllers even easier.

Layout in Windows Phone 7

 

So my wife left me for a hen weekend and I needed something to do with my time.
Friday night I dedicated to my real work to clear my conscience and then I went to play.

Windows 8

I spent a few frustrating hours on Saturday working on a Win8 application to compliment a certain website, frustrating… in so far as the Xaml designer in VS11/Blend on Windows 8 hangs like a good one. Actually at serveral points in time I considered changing my app to html5/js, because, for at least for the search section of the app I could see no blockers. However I want to add some pretty nifty features going forward and I don’t feel like implementing them in javascript.
I guess I may the possibility of writing them in C++/C# as a library and using this from the UX application if it continues to slow me down. I’ll have to consider cutting my losses soon if it keeps hanging the development environment that’s for sure (the joys of working with not only a Beta application, but a beta OS! Smile )

WP7

So for a bit of fresh air I started writing the WP7 app, at least this is more familiar ground to me having written quite a few of them already, and I just took my resource dictionaries from the WinRT app. To start with I just used some sample data in expression blend, dropped on a list box and bound my sample data to it.

image

But the layout wasn’t really what I wanted. I’m hoping that that API for the real data may provide me with a list of categories, one wouldn’t really want to have to release a new app every time a new section was added would they.
So my approach would be just to bind the data to a list for now (it may well turn out to be an ordered list in the API). The problem as you can see above what that I was getting a single row for each item.. not what I wanted.

Would you believe this was the first time I’ve not wanted this default layout in a WP7 app, so I decided to use the WrapPanel container that I’ve previously used in a WPF app… but alas it doesn’t exist out of the box!

Silverlight WP7 Toolkit to the rescue.

A quick Google indicated that the WP7 toolkit had the WrapPanel I was looking for, I fired up the Nuget package manager and added it to my project.

image

Items Panel

One more change was needed in my xaml, I needed to specify where to use this wrap panel.
This I specified in the ItemsPanelTemplate of the listbox in question.

image

The final result!

image

Hopefully this helps someone else out.

Reading a file in Windows 8 html5 js

 

Hi again, this is my last post were I compare the same WinRT application written in three different ways.

  • C++ / Xaml
  • C# / Xaml
  • JavaScript / html

My previous post covers c++ and c#, this one covers javascript.

JavaScript / html

image

image

I have to admit, this really took me by surprise! It was pretty painless. I had it written off before I even tried it, but this task proved somewhat simplistic. I’d one little obstacle trying to use a CDN, but other than that it was trivial.

So the penny has dropped, the WinRT developer pool is going to be massive! I predict good things.
If Microsoft play their cards correctly it will be a very successful platform (even more so with the promises of WP8 portability).

So what didn’t I like about this particular app? Not much really, other that realising I’ve a lot more talent to content with Winking smile

 

Summary

So I compared the three different approaches, for the task in question c#/xaml or js/html won hands down.
C++ was overkill.

I think each option would have to be considered on an application per application basis.

 

btw: on a non related note: my wife has just switched on the bedroom networked TV and windows 8 on the laptop here in the sitting room started installing stuff

image

I’m sure it’s got to be cool, if only I knew what for Smile

About the author

Brian Keating is a developer addicted to Microsoft Technologies.

Month List

Tag cloud

RecentComments

Comment RSS