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:
I’ve highlighted Break when this exception type is thrown because without this box being ticked you won’t be able to see $exceptionpseudovariable 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:
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.
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:
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
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:
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.
After unloading, we’re going to edit our project file like so:
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
Now you should see your compilation errors inside Error List window in Visual Studio
I’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
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.
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.
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:
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.
_clock=newClock(1000);// 1000 ms = 1 sec.
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 ?
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.
Recently I’ve had a little change in my carrer. From being a .NET Web Developer I’ve become (at least trying to become) Windows Phone & Windows 8 Developer. Almost everything is different, minor change ;]
Right now on regular basis I’m dealing with XAML, MVVM concept, MVVM libraries, Blend, Animations, VisualStates and much more stuff that comes with creating applications for Windows Phone (WP) and Windows 8 (W8)platform. Although I had some, none professional, experience with Windows Phone and all that I’ve mentioned above – I’ve made 3 really simple applications – it was a drop of knowledge in the sea of stuff that you have to know as a decent WP and W8 programmer.
With that being said, I’m trying to catch up, bacause of the obvious lack of knowledge, and in my spare time I’m doing some easy projects that hopefully will let me understand better the concepts, best practices, syntax, libraries and everything that hovers around the WP and W8 development. Keep your fingers crossed.
Lately Microsoft presented new way of building apps for WP & W8. Universal apps, that’s the name of the idea, allows you, as a developer, to create only ‘one’ application for both earlier mentioned platforms. That’s truly great, like a special offer in the shop, 2 for 1, more for less. What’s even better, it’s a new thing in a .NET world and I’m new to this particular part of a .NET world. Everybody knows that two negatives gives plus. Isn’t it great ;] ? So! Besides reading about it
I figured out that theory isn’t enough and it would be good to do some practical exercises. Brilliant idea! I wasn’t thinking more and just started coding. Not really…Firstly you have to download and install Visual Studio 2013 Update 2*. Read what’s new in it here and download it from here.
After a while you can create your first Universal Application. Just choose a template, pick a name for a project and you’re ready to go.
First Universal App project – Favorite Time Zones
The application that I’m going to create is really simple one, and you can find a lot similar ones already in the store, but that’s not the case here. The deal is to learn about WP8 and WP platform, API, possibilities, limitations, behaviour etc.
Idea came from a need. Right now I live in Australia but originally I’m from Poland and sometimes I want to call my family and I don’t know what exact time is in Poland, same thing goes for my family but in the opposite way. It’s 8h difference, which can be easily calculated but if there was a tile on my phone or laptop screen with my Favorite Time Zone(s) that would be great. No more calculations, just a quick glimpse and I know if I can make a call or not. Let’s get to work then.
To be continued…
* Apparently there was another package of updates. Now you can download Update 3 for Visual Studio.
Starting a project with Visual Studio is really easy process. There are just few steps, how many depends on the project template, to get going with new idea. You just go to File -> New -> Project…
then choose the best template that suites your needs, put desired name to it, click OK and you have got yourself a brand new project that is ready to use. You can start coding, right ? There’s always a but, isn’t it ? Yes you can, but you should bear in mind one thing, a project naming convention which determines your project directory tree and has an impact on a C# namespaces.
Examples are the best way to explain a point. I, myself, am a Web Developer so I’ll use a MVC project template as an instance. Let’s create a emptyASP.NET MVC 4 Web Application project and I’ll try to get you through my thinking.
Imagine that we are creating some kind of application for heater company. I’m a little bit cold right now so this is why heaters company, nothing in particular. Let’s say that the company name that we are doing the project for is HeatItUp. They want for themselves a nice website. We already know the first step, File -> New -> Project… and then we choose template and name, easy.
Although project is perfectly fine, in the end the Microsoft shiped the template, you should be aware of the way Visual Studio creates for you project directories. Notice the two input fields for Solution Name and Name (Project Name) in the New Project Window. After creating it like on the above screenshot you will end up with situation like that:
You can see that you’ve got Solution named HeatItUp and Web Application Project (website) named the same way. This is bad from the logic and good practices point of view. Logic, because project named like that does’t say anything to anyone besides you, the creator, about itself. Nobody knows if it is a WPF, WinForms, just a library or as it really is a Web Application unless they open it and look closely. Good practices because of the mentioned above and everything that corelates with it, generated directory tree and namespaces in your code. Little advice, always keep your namespaces clear and tidy because in large projects you can get really confused and even lost when they are poorly designed and when it comes to namespace refactoring…pure evil.
Ok, but even when I made a mistake with the Web Application project name I can easly change it in the Soultion Explorer. Yes, but don’t, because it changes nothingbut the *.csproj file name, directories tree and namespaces are still the same. Obviously you can change folder names by yourslef and edit *.csproj file and change RootNamespace tag, refactor all the namespaces and it’s done. Sure, you can, but the price is really high, a lot of effort.
Solution to this issue is just to keep in mind these two input fileds on the New Project window while creating new project, Solution Name and a Name aka Project Name. Or you can do it my way, start with the BlankSoultion
and add systematically projects that you need, always thinking about others who will work, inspect and try to understand the project solution.