Monday, March 26, 2012

Fixing "URL-encoded form data is not valid" Error in ASP.NET

Today I had to solve a tricky error raised on the back-end system implementation I'm currently leading. Whenever a user tried to submit the values of a heavy loaded ASP.NET MVC form, the system was throwing the following exception:

System.Web.HttpException (0x80004005): The URL-encoded form data is not valid. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding)

The error is thrown immediately after clicking the submit button, and although I have logging implementation in the code, nothing was being reported on the app logs.

I then manage to copy the data to my local development environment and was able to replicate the issue. I did a few tests double checking if it was a JavaScript issue or something around the front-end part, but I could quickly discard it.

After researching on-line around forums, dev sites and official documentation I found out that in ASP.NET there is a security default setting that restricts form key/value fields to a maximum of 1000. Although it is probably more than enough for most cases, in this particular scenario I needed to be able to manage many more keys. Luckily the fix didn't require much effort. It is enough to add a new configuration settings on the "" section of your application configuration file to tell ASP.NET secutity settings to allow a bigger number of form key/value fields.

That will increase the default ASP.NET Security Form Key collection to 2000 instead of the default 1000 value. After updating the configuration file I was able to solve the issue right away.

Keep this in mind or on a handy place, since dealing with those kind of errors are tricky and difficult to solve because the lack of error information.

Hope it helps !

Sunday, March 25, 2012

Getting Started with Kinect for Windows

 

This is one of the first articles of a series I’m planning to share my experience, tips and solutions when developing applications, games and interaction behaviours using the new Kinect for Windows, which was released by Microsoft last month.

A couple of noticeable improvements that caught my attention are the Near Mode and the Audio Device out-of-the-box capabilities.

Near Mode

The Near Mode allows the sensor to detect closer objects and get a lot more data on close interactions than with its previous version. You get closer depth detection and you can be at 1.3 feet from the camera, and still get data.

Audio Device

Once the Kinect is installed, it will appear on the device manager as an extra audio device, that can be managed as a normal audio device and even be used as a recording device.

Untitled

Getting Started

As usual, to get started quickly, you will need to follow the following steps:

  1. Download the tools
  2. Get your hardware
  3. Install the SDK and required tools
  4. Get started with some basic samples

This post is intended to give you a quick reference on how to get started with Kinect for Windows development quickly. You will definitely need to get the hardware to develop applications since the depth tracking and gesture recognition capabilities can’t be emulated easily without the Kinect sensor.

The new hardware can be acquired in Amazon for about $250. The new version is more expensive than the previous one, but it now includes a commercial license, meaning that if you develop using the new SDK and hardware, you will be able to sell your applications !

To get started, do the following:

  1. Go to kinectforwindows.org
  2. Download the Kinect SDK
  3. Order Kinect Hardware

Once your order is in place and the hardware on its way, you need to install the SDK you just downloaded. The SDK Installs the following:

  • Microsoft Kinect SDK v1.0
  • Microsoft Kinect Speech Recognition Language Pack (en-US)
  • Microsoft Kinect runtime v1.0
  • Microsoft Kinect Drivers v1.0

Just double click on the file named “(KinectSDK-v1.0-Setup.exe)” and follow the wizard.

Install Process

To begin the install process, execute the SDK install wizard by double clicking the KinectSDK-v1.0-Setup.exe. This will launch the Kinect SDK installer.

image

image

The wizard will ask you to install the Microsoft Visual C++ 2010 x86 Redistributable.

image

image

image

The wizard will also ask you to install the Microsoft Visual C++ 2010 x64 Redistributable version targeting x64 systems.

image

image

image

Once the Visual C++ redistributable packages are installed, the Kinect SDK installer will continue to install the remaining components of the SDK, including the language pack for the speech recognition capabilities and the drivers.

image

image

And that’s it. You have all the tools, runtime and software you need to start developing your Kinect for Windows applications. Hopefully you have the hardware ready and you are eager to start testing and ramping up with sample projects where you can learn the basics. A few posts to come soon to get our hands into the code.

Stay tuned !

Adaptive Layout goes Native in Windows 8


Since Microsoft’s public release of Windows 8 Developer Preview back in September, 2011, I have been exploring all the new OS capabilities related to the touch centric interface on all the different language flavours.
One aspect that caught my attention is the ability to quickly implement adaptive layouts. As a quick prototype, I implemented a Windows 8 touch first application that allows users to browse colleagues across the different offices. The whole app took me less than 8 hours to implement, but the adaptive part took me around two hours for one of the views, taking into account best practices and including the whole snap mode layout design.
locator_02
locator_03
locator_04
The nice thing is that you can centralize application style resources and create a story board to modify the layout when the view changes from full mode to snap mode. Then you can share the resource across all pages using the same layout template in your app.
I must say that with XAML and C# and the tools you get from Visual Studio and Expression Blend in Windows 8, it has never been easier to architect, implement and deliver cross resolution and size applications that can adapt dynamically to the display capabilities of the device running you app. You just need to know your stuff !
Cheers!

The Kinect Revolution

 

I wrote the original version of this article on February 1st while I was on a business trip in New York City. Coincidentally it was on the same week when the Kinect for Windows was released to the market. I wrote my initial thoughts about the Kinect and its surprising history from the labs to the market and how it is changing the way we develop interaction behaviours for applications and games. I have putting a few samples and blog posts together and I though it was a good idea to publish my full original article first. So without further talking, here it is.

For those people that haven’t been able to try a Kinect sensor yet, it is a motion sensor device created by Microsoft, which allows people to interact with games and applications without the need of a physical controller. While several companies out there were working on movement controllers, this little fellow went the extra mile and got rid of the hardware itself. After all, the smartest device in the market is actually the human body.

Launched in November 2010, Kinect started a revolution in the gaming industry. It sold 8 million units in the first 60 days, holding even a Guinness World Record of being the "fastest selling consumer electronics device". Kinect changed not only video game industry but also the ways people can interact with software applications. It brought new interaction behaviours to consumer market that only a few years ago was still considered science fiction. Kinect capabilities for gesture and speech recognition and its relatively low cost make natural user interface development easier than ever before.

The Kinect story is quite interesting for being a Microsoft product. It was launched as a closed source proprietary technology. When Microsoft released it to the public in 2010, it was intended to complement the Xbox 360 console. Right at that moment it caught the eyes of the development community. Developers were so interested that they broke into the device in record time (not one month after the public release) and started to use the Kinect to develop custom applications using the Kinect sensors. Suddenly there was an unofficial SDK circulating on the web allowing any developer to use it for Kinect application development. Instead of banning users and forbidding the software breaks, Microsoft took an unprecedented approach and actually supported the community and the application development for non-commercial purposes. In June 2011, Microsoft published an official Kinect non-commercial SDK for people and companies to continue exploring the power of the device.

Now, up until recently, Kinect for Xbox SDK was free to use for educational and non-commercial purposes, which allowed developers and companies to explore the technology and its applications but prevented the use of the SDK and the hardware for commercial products. In order to propel the Kinect usage and development, Microsoft announced in October last year that it was about to release an updated version of the hardware and the firmware for commercial purposes which was improved for Windows development.

Microsoft has just released (February 1st), the new version called Kinect for Windows. The updated hardware is a bit more expensive than the first version of the device, but it comes with a commercial license and several features intended to improve the sensor capabilities when used with a computer.

Kinect for Windows can be connected to computers running Windows 7 and even the Windows 8 developer preview. The Skeletal and Join tracking has been improved providing a more accurate and faster tracking. The new version also provides a new Near Mode that enables the depth camera to detect objects as close as 50 centimetres in front of the device with graceful degradation at 40 centimetres. This will effectively allow developers and companies to start creating and releasing commercial Kinect applications in the consumer market, hopefully helping to continue democratizing natural user interfaces for end consumers all over the world.

Over the next few months I’ll be posting a series of articles about developing applications, games and interaction behaviours with Kinect for Windows, so stay tuned.

Sources:
Kinect For Windows: http://www.microsoft.com/en-us/kinectforwindows/
Official Blog: http://blogs.msdn.com/b/kinectforwindows/

Cheers!

Saturday, March 10, 2012

“ScatterPuzzle” Surface SDK Sample Error

 

Today I was playing with some Surface 2.0 SDK samples and working on my personal Surface prototypes when I started to get a confusing error when I tried to run the ScatterPuzzle sample. Below a screenshot of the exception message:

image

The error is telling me that the invocation of the constructor of the main window is failing. However the reason is obscure. The exception details say that the problem is a mal formed path, but the line and position indication is telling me nothing relevant since it points to the application namespace declaration:

xmlns:sp="clr-namespace:ScatterPuzzle"

If you put a breakpoint in the constructor and debug the application, you will be able to follow the execution of the constructor step by step. In my case, I found out that the path to the images was not being retrieved correctly by the following piece of code:

The CommonFolder element corresponds to a custom class defined in the app that uses a KnownFolder list of Guids to know folders, in this case, Sample Pictures and Sample Videos public folders in Windows. The class uses the following method to retrieve the folder path based on the passed Guid:

The issue here is that the application is not handling the edge case when one or both of the folders don’t exist in the local machine. In my case, I had deleted the “Sample Pictures” folder from the public user’s “Public Pictures” folder, so when the application tried to load the list of images from the empty path, the exception was being thrown.

Of course the right solution is not only to create the missing folder on the local machine, but also to update the code to prevent the exception by checking the path being passed. In the main Window1.xaml.cs file, locate the LoadPuzzleList() method, which contains the code to load the files with extension “jpg” from the pictures folder. As you can see the code doesn’t check if the path set in the constructor is null or empty. Lets add the code required to perform this simple check:

Do the same for the videos path. Now if I run the application and one or both folders don;t exist on the machine, the app will run without exceptions and just display the existing videos or images, or by default an empty list if both folders are not present.

imageApp running with only videos

image

App running with pictures and videos

That’s all for now. I hope it helps and saves you time figuring out the reason of the exception if you were getting it, and also to remind you to always improve your code by checking this type of edge cases.

Cheers!

Monday, March 5, 2012

Embedding Application Resource Dictionaries

Recently I have been working on a large scale project that requires localization into dozens of different languages. The localization is required not only for the content itself, which is displayed by touch screen applications, but also by the email services in the back-end, which are used by the applications to send information to users via email.

The email services application was implemented using ASP.NET MVC and WCF to expose not only a RESTFul service but also a web interface to view emails on-line. Along with this, we created a small test app that we use to test the email services and generate emails on demand.

Now, since the email's copy don't change often, we decided to use resource dictionaries to localize the email's copy. The advantage of using resource dictionaries is that you define a master template for the resources, and then just add localized copies of it which will be used depending on the region the content is associated with.

To embed a new resource dictionary, follow these steps:


1- Make sure you have your global resource dictionaries setup with valid Culture Info region and that you have your base/default resource file. You will get mysterious errors if you are trying to use a resource dictionary that doesn't have a valid .NET Culture Info code.


2- To add a new resource file, you could right click on the "App_GlobalResources" folder, select "Add New Item" and chose the "Resources File" template, but is a lot easier just to select one of the existing resource files which already contains all the string keys and copy it there. That way you will have all your string keys populated.



3- Once you have the copy, rename it to the corresponding Culture Info. Then you can open the resources file and enter all your localized values.


4- Don't forget to check the resources files properties. You need to make sure that it is set to be an "Embedded Resource" and use the appropriate resource file generation tool.

Re-build your app and test it. You should get the corresponding localized resource dictionary when selecting the corresponding region. In our case, we have the content associated to a given region, so when the touch application sends the requests to generate and send an email, we check the content region and use the corresponding email localized resource file.

This will also work if you are displaying the localized content of your app associated with the browser locale. You can read more about ASP.NET resource files here.

Happy localized coding !