21. February 2013 22:11
Normally, if you have to rethrow and existing exception in .NET, you should use the throw keyword without any parameters:
// Code that can generate exceptions here
catch (Exception ex)
// Do some logging or whatever
Using throw ex; would cause the stack trace of the exception to be lost, in which case we lose some valuable information. However, this only works inside the catch block that caught the exception in the first place.
Sometimes, you need to rethrow the exception at a later point outside the catch block, but of course we want to preserve the stack trace.
18. February 2013 20:13
Every now and then I just want to do something silly with code, like some code golf. Just for a moment to forget about best practices and write the shortest possible program to solve a problem.
Today I happened to run into one of those projects on my hard disk, which happened to be in C# (it's not exactly the tersest language there is, so it may be a little unusual). It's an exercise from an old Google Code Jam. The problem to be solved and some solutions can be found in this StackOverflow question, including my own. It measures 398 characters after removing insignificant whitespace, the longest solution between a host of different languages.
Today I realized that there was a slightly shorter solution than the one below, but perhaps you can come up with something even better?
17. February 2013 13:59
In hindsight to the created solution, there is an issue that cannot be addressed in this way: When a consumer fails to call Teardown properly at the right time for constructed objects, the DisposingSharedLifetimeManager will not behave as expected. For some, this may be a big issue, because it may not be very customary to bother to call Teardown when using Unity, precisely because it usually does nothing useful anyway.
Alternative Solutions Using Unity
One alternative is to use a wrapper of sorts and register that in the container instead. The usage would look something like this:
16. February 2013 19:25
Two New Lifetime Managers
Having explained the issue, how can we solve it?
Before starting, I should mention that my implementation is based on previous work by Rory Primrose, who wrote an extension that changes the behavior of the TransientLifetimeManager in order to dispose of objects. He does an excellent job of explaining some of the rationale behind his implementation, so be sure to read his article.
I've placed the complete source code for my article (including some quick unit tests) on GitHub.
My approach is a little different: I did not want to change the default behavior of Unity, so instead I defined two new lifetime managers to explicitly set the desired behavior with: DisposingTransientLifetimeManager and DisposingSharedLifetimeManager.
This lifetime manager behaves pretty much like the built-in TransientLifetimeManager; every time a new object is created. The only difference is, that on Teardown of any object, we will call Dispose() on objects that were created as a result of the original build-up operation (provided they are using this lifetime manager, of course).
12. February 2013 20:48
In modern software development, the use of Dependency Injection is a very important principle. It allows you to build large systems with properly decoupled and testable components. The use of Inversion of Control containers is a popular way of implementing Dependency Injection. If you are unfamiliar with these terms, this article probably isn't for you; I suggest you read Martin Fowler's excellent article about DI and IoC instead :)
In the .NET world there are probably dozens of different IoC containers available. They all serve the same basic need, but there are important differences in the features they offer. One popular IoC container is Unity, created by the Microsoft patterns & practices team.
Just because it's popular, doesn't mean Unity is perfect however. In this multi-part article, I will try to address some issues around classes implementing IDisposable in combination with Unity.
3. June 2011 21:20
Every now and then, you run into a piece of code that makes you wonder how it ever got into existance. I personally have a bit of a fascination for these things. I read TheDailyWTF regularly for amusement, but I always try to understand what caused these usually inventive but nevertheless misguided pieces of work. When I run into something within my own circle, understanding may also help me prevent problems in the future.
17. April 2011 21:53
Earlier today, I blogged about a reusable behavior extension to make debugging Silverlight apps a little easier. I wrote most of that article months ago and in the meantime I've added a second behavior extension to support Silverlight software development. I included it with the download, but some explanation about its purpose might be in order.
17. April 2011 18:16
If you're used to debugging ASP.NET web applications together with WCF services, you will know that Visual Studio automatically attaches the debugger to the service process in addition to the website. When you It doesn't do this for services being called from Silverlight. Instead, Visual Studio will probably warn you that the service call will fail, but we've taken care of that problem with the behavior extension from the previous post.
The solution is fairly obvious: you will have to make sure that the debugger is attached to both your web application and your service.
17. April 2011 13:51
If you've ever developed a Silverlight web application in conjunction with WCF services, you have probably run into some challenges when it comes to debugging the solution as a whole. Silverlight applications are limited to using services from the same server and port number as the application itself, unless the service explicitly allows external access through a clientaccesspolicy.xml file.
8. August 2010 22:25
Last week, I got an e-mail from a customer using our web services. They were wondering some operations were not working properly when no input parameters were given. This raised an eyebrow at first, as my documentation clearly stated that this information was mandatory.
It would soon become clear, that the developer in question did not read said documentation at all, but was in fact flying blind on the WSDL. Indeed, the input message schema said everything was optional; this is default behavior for WCF. In this article, I'll work on fixing this minor caveat in the interest of interoperability.