Category

C#

Not so long ago I had a small problem with inspecting pretty complex exception. Usually what you see after some exception is thrown is basically this kind of window:

Exception_occured_dialog

I’ve highlighted Break when this exception type is thrown because without this box being ticked you won’t be able to see $exception pseudovariable inside your Watch window, but about that in a second. After seeing that, usually you click on View Details to check what exactly had happened:

Exception_details_dialog

Inspecting all of the provided information doesn’t give you much more information about Exception itself besides the type of it and that there are some more specific information inside the Exception (two exception parameters). Even though we don’t have much information about ‘insides’ of the exception we can tell something from exception name itself, right ? That’s correct, you can figure out what kind of exception or with which group of exceptions you’re dealing with, but you still don’t have specifics. With help comes mentioned earlier $exception pseudovariable.

Exception_in_full_details_in_watch_window

As you can see there’s a lot information about exception that was thrown. For us the most important part is kept inside ConsoleApplication1.ComplexException:

Exception_complex_params_inspected_inside_watch_window

Now you know exactly what had happened. You can do a little tweak around displaying it in the Watch window, so you won’t be bothered by other properties of Exception object. Just cast $exception to specific Exception type, like so

Exception_casting_inside_watch_window

and voilla

Recently I’ve been doing some work with ASP.NET MVC, going back to the roots ;]. I had to create simple Web Portal to manipulate database tables. It had to have possibility to make CRUD (Create, Remove, Update and Delete) actions on tables records. I used MVC scaffolding option with specified model. After few clicks everything was generated for me, I mean Controllers and Views. Of course it wasn’t exactly as it was suppose to be, so I had to apply some changes. Making these changes I’ve realized that not everything is correct when it comes to models. Applied changes to models forced changes on the Views. In that moment I could just generate everything again but changes were only minor so it would take more time to make the ‘clicking’ than dealing with these changes on my own. What I’ve done to the models was simply deleting some properties. Everything look ready to compile, so I’ve tried. Errors occurred. Obviously, property that I’ve just removed from the model was used somewhere in the code and now compiler can’t find it’s representation. I’ve removed all of the problems and I finally could check how does the website look like in the browser:

yellow_page_of_death_with_compilation_error

Of course..yellow page of death showed up ;] There were still some references in the Views aka *.cshtml files that should be cleaned. One change, refresh page, another error, second change,  refresh page, another error, … , tenth change and so on and so on. So is there a way to put some guard or sentry to keep your Views compilable ? Yes there is! There is a big cost, though, that comes with it. Your build time will increase dramatically, but you can pretty easily switch it on and off whenever you need it. After major changes to your models just build it once with this function turned on, catch all the errors and switch it off afterwards.

What you have to do to get your Views to be compiled is rather straightforward. You need to change *.csproj file. You can do it in two ways. One is from Visual Stuido and the second just usual file edition from File System (Explorer). We will do it with VS. Let’s get this party started. First we have to unload our project so the *.csproj file could be edited.

unloadin_project_in_visual_studio

After unloading, we’re going to edit our project file like so:

unloaded_project_in_visual_studio_edition

Now we should see *.csproj file and this is the place where we can force compiler to build our Views along with the code. All we have to do is to change MvcBuildViews setting to True or if the entry is missing just add it

mvc_build_views

Now you should see your compilation errors inside Error List window in Visual Studio

compilation_error_on_the_viewI’ve noticed that with MVC 5 this solution isn’t working, and what I’ve found out is that you need to add some extra stuff. In the same project file that you’ve been editing recently, at the very end of it, uncomment element

and add this code

You can read more about this topic on stackoverflow.com

 

What I’ve learned today

Quick explanation: This is going to be a title for a kind of series that I plan to make from a blog posts about my struggles, mistakes, obstacles, errors and problems that’ve encountered during development time. It’s going to be a log of a WP8 and WP greenhorn programmer trying to make his way through a maze of new stuff. Don’t expect some fireworks, it’ll be rhather kind of compendium of what to do when you stumble upon a particular problem. I hope it will help others like me to avoid same mistakes, ease the pain of searching over the internet for a solution to their issue, make their life easier or just learn with me. Let’s get started!

Favorite Time Zones

This project, about which I’ve mentioned in my previous blog post, is a simple idea of showing on the screen, of your device with Windows 8.1 or cell phone with Windows Phone 8.1 on it, current time in different times zones.

After starting new Universal App project from Visual Studio with at least Update 2 on board I’ve added MVVM Light Libraries and Time Zone Conversion for Windows Runtime and PCL with NuGet pakcage manager.

Adding_Mvvm_Light_Libraries_to_universal_app

MVVM wasn’t necessary because of the pragmatic point of view you don’t have to apply MVVM concept to such simple application. The reason behind me adding MVVM Light to this project is simple, I want to do things right, following good practicies. After that my NuGet packages manager looked like on the screenshot below. CommonServiceLocator is atomatically added with MVVM Light.

MVVM_Light_Libraries_and_Time_Zone_Conversion

I used Time Zone Conversion for Windows Runtime and PCL because it was no longer that easy to list all the time zones within Windows RT . Usualy you would be able to get all the time zones just like that:

With this library it was similarly easy:

And the model class, rhather obvious, but anyway I’ll put the code here, looks like this

Having that, we can proceed further. We will be showing time, so it has to be ‘refreshed’ or ‘updated’ accordingly, like a watch. The first concept of welcome page of Favorite Time Zones project looks like this:

Favorite_Time_Zones_Concept

At the top, there will be displayed users current time and underneath it there will be a list with favorite time zones. After a little bit of design of the page, I put a lot of emphasis on little, we can start playing with some logic around it. First thing we do, is to get the user time clock to work. Usually you would create a Clock.cs class and imitate time lapsing with System.Threading, inifinite loop and Thread.Sleep(). Maybe it’s not the best way  to create clock when you have to be really precise, but for our purposes it’s enough. To start new Thread you could do something like, right ?

No, you cannot. Not in a framework for Universal Apps, with them you have to use Tasks. This implicates that we can no longer use Thread.Sleep(), but with help comes Task.Delay(), its equivalent. No worries, we can use Tasks, although I’m rhater used to Threads. After a while you will find Tasks really useful, because of their usage with async and await functions, that really simplifies things. But let’s stay on the topic of updateing our clock. Clock itself could look like this

It has a ctor() that takes tick interval and has one public method, Start(). Although, the most important part is the Tick event handler that we will register to and we will update our clock once the event is invoked. ClockStart() method is an endless loop, called from ClockStart() in a separete Thread (Task), that ‘Ticks’ every specified interval time. In our case it’s going to be 1 sec. Let’s then start our clock and register to its tick event.

This is our ViewModel class for MainPage. It inherits from ViewModelBase (MvvmLight). It provides implementation for INotifyPropertyChanged interface and gives us easy way of communicating to the View that some changes in the ViewModel have been made. This notification mechanism is done with RaisePropertyChanged method. In the ctor() we are creating a Clock (not the best place for it, but this is done only for the example purposes) and registering EventHandler for the Tick event and then we Start() the clock. Everything looks great. Clock is working, Tick event is fired and ‘captured’ so we could refresh View and its done with RaisePropertyChanged. Should work, right ?

the_application called_an_interface_that_was marshalled_for_a_different_thread

This is what you’ll get. Exception, telling you that some thread wanted to access area that he wasn’t allowed to enter.

The application called an interface that was marshalled for a different thread

This is rhater obvious when you think about it. I’ll relate this situation to a real life one. Let’s say you are driving a car and next to you sits a passanger. You’re on a long and boring drive (Main Thread), straight road for hundreds of kilomiters, and all of a sudden, you don’t know why, your passanger is trying to grab the wheel and turn the car (different Thread). So what do you do ? Of course you are going to slap his hand(s) and ‘revoke’ his try to kill you and himself. This rule applies even when it comes to a radio station, slap ;] ! What in a situation if passanger had a really good reason to try to interrupt you ? Like, we are going to miss a turn or they started to play Justin B. on the radio. So the passanger could succeed with his intentions and actions, he is obligated to say to a driver ‘listen, you should take next turn left’ or in our case ‘listen, there is something that you should update on your view’ and then the driver, who has proper permisions, will respond with an action. This is done through a DispatcherHelper and CheckBeginInvokOnUI() method in MvvmLight library.

It works!