Debugging NUnit tests under Visual Studio 2010

Upgrading to a new version of a development framework always holds some little surprises and gotcha’s, no matter how careful you are.  I found one of these the other day during the transition of some of my projects from .Net 3.5, and VS2008, to the new Visual Studio 2010 and .Net 4.0.  When I initially upgraded I ran all my unit tests to ensure they worked, and they do – the new version of NUnit supports .Net 4 already which is great news.

I didn’t play around with it too much after that as I had some problems with the Moq mocking framework running in VS2010 (more on that in a post to come), however when I did find the time to make some modifications to the tests and check them I noticed something very strange – I could no longer debug my tests.  They ran fine, but in the debugger I was greeted with an old friend of anyone who has done significant debugging in Visual Studio over the last ten years – Breakpoint will never be reached, no symbols loaded.

I played around for a while and eventually found the problem.  Traditionally debugging NUnit in Visual Studio has been done in one of two ways. First, you can attach the debugger to the NUnit gui manually after launching it, or you can set up the debugger to launch NUnit as an external program to launch on build (so you can F5 it!).  The second method is the one I usually prefer, but unfortunately we can no longer use -either- of these methods.

I’m not sure what has caused this issue but in order to debug NUnit tests in visual studio now you need to manually attach the debugger, as above, but instead of attaching to the NUnit process you need to attach it to the NUnit-Agent process.  This causes problems for people, like myself, who like to hook up NUnit debugging to the F5 key as that will attach you to the loaded program, in this case NUnit, not to the agent process.

Seems that for now the only method is manual attachment. Hopefully this will be changed back in a future version, though it may be some time.

Edit:

Thanks the Klaus for pointing out a better workaround below!

Apparently the real issue is that NUnit loads up in .Net 2.0 and then has to load up the modules in 4.0, so does so under the agent process. (Though why it didn’t have to do this for 3.5 is not clear).  The solution is to force NUnit to run under 4.0 itself.  This can be done with the following addition to the NUnit.exe.config file.

First, directly under the configuration element, add these lines:

<startup>
<requiredRuntime version=”v4.0.30319″ />
</startup>
This sets NUnit to run under the 4.0 CLR.
According to this page you should also add this line under the runtime element:
<loadFromRemoteSources enabled=”true” />
This allows libraries downloaded from a remote site (such as a website) to be loaded with full trust.  I’m not sure why this would be necessary and indeed, my application debugged NUnit just fine without it.  However, if you have problems, you might want to give it a try.
Note that the link given above gives the version number to enter as v4.0.20506 whilst here I’ve said 4.0.30319.  The former is for the .Net 4 beta, whilst the latter is the RTM version.  A good list of version numbers can be found here.
Hope that helps!

Broken SmtpClient in .Net

I’ve been meaning to post about this for a while as I discovered the bug a few weeks ago, SmtpClient in .Net 1.1 – 3.5 is broken.

By broken, I mean non-standards compliant with the SMTP specification.  The SmtpClient object is a fantastic little object that can handle most scenarios you would need for personal apps and sending emails, including multiple encoding formats and SSL handling.  However, it has a flaw – it doesn’t close connections to the server correctly.

The SMTP spec calls for the client to issue the “QUIT” command when finishing up communications.  The SmtpClient object instead, after sending the mail data, returns control back to the calling app and leaves the connection hanging.  Most servers seem to handle this ok but it’s certainly not good practice, and it’s causing me no end of bother now.

I’ve been working on a project called PseudoSmtpServer, which is exactly as it sounds, a fake smtp server designed for Test-Driven Development.  It implements the standards-based smtp commands but instead of possessing the usual smtp internals, it stores all information its sent so that it can be queried by test code to ensure sends occurred properly.

It’s been an interesting project, but I’ve had quite a lot of problems, some of which are due to this SmtpClient bug and some due to the TCPListener not performing as expected (in particular, taking a long time to release a socket.)  This generally leads to performing erratically, seeming to fail without cause generally because the previous test claims to have cleaned up but in reality hasn’t finished releasing the socket for the next test.

I’m currently rewriting my listener to make direct use of sockets rather than relying on TCPListener’s implementation, and I am led to believe that the bug in SmtpClient may finally have been fixed (about time!) with the release of .Net 4.0.  I’m currently trying to upgrade the project to .Net 4.0 but am having difficulty with the Moq libraries.  I’m currently investigating this and will post more later on the topic.

AddressAccessDeniedException thrown when starting a service with ServiceHost on Localhost

I’ve been working my way through Apress’ “Introducing dot Net 4.0 with Visual Studio 2010” to get a handle on what’s new in the latest version of my environment. I skipped this step when moving from 2.0 to 3.5 which led to me missing quite a few great tips, which I was determined would not happen this time.

Working through Chapter 7, WCF Services, I’ve found something the book author seems to have missed. One of his code samples fails to work for some people, including myself. I tracked down the problem to the fact I was running Vista (strangely, I would assume the problem exists then on Windows 7, the authors operating system, but maybe not).

Basically, the example instructs you, having written a service, to create a console app that registers a ServiceHost object and open it, thus starting your service. This is done like this:

ServiceHost Host = new ServiceHost(typeof(YourServiceType), new Uri(“http://localhost:8888/myservice&#8221;); Host.Open();

After this, assuming all goes well, you can browse to localhost:8888/myservice and view the service as published. For me, it didn’t go well. I hit F5 and as the application began to run, it threw an AddressAccessDeniedException claiming that I didn’t have access rights to create an endpoint at that address.

This is confusing of course, until you realise that Vista requires you to elevate your permissions to do various things, one of which is open ports and create address endpoints. F5 in Visual Studio is completely unable to perform this task.

I have found two solutions to this issue.

The first, given that in this particular example we’re using a console app, is to open a command prompt with full administrator access (Run As Administrator), change directory to the bin/debug directory of the program and run it from there. It works fine.

The second is to open Visual Studio itself with full administrator access and then F5 will work as expected. It’s an interesting little bug/feature and worth keeping in mind next time you stumble over an unexpected access denied issue.

Geordie Guy waxing lyrical on the HTC Desire

An old friend of mine, Geordie, has recently posted his appraisal of the HTC Desire, considered against his own iPhone 3Gs. It makes a good read.

Click here for the whole article

Want the capsule summary? He puts it like this:

I think the Desire, the HTC brand and the Google OS are all direct threats to Apple’s smartphone market and they better make up another batch of addictive koolaid before the tides turn. I don’t think they will.

It’s about time we got a bit of competition in this market before all innovation slowly stagnates under the control of Big Brother Jobs. 😉

Nintex Workflow 2007

Recently I’ve been working on site with a local client helping to set up a new SharePoint 2007 based intranet site. Those of you who have been following my blog for some time would realise that this is really a bit of a homecoming for me; its the work I started out doing so many years ago.

I’ve learnt a lot these last few days, looking at things once again from a customers perspective. Our client was quite technical himself and I was there less to design and build the intranet site itself and more to provide the benefit of my experience and bootstrap his own learning.

As is usual in these sort of situations, his own explorations prior to engaging me showed me a few things I hadn’t encountered myself to this point. One of these was Nintex Workflow, a third party add-on to SharePoint 2007.

I have, as a rule, stayed away from third party add-ons in the past. In my regular role I design and develop solutions to be deployed to client servers and so we tend to stay away from including third part controls, over which we have no control, to our solutions lest they cause unknown problems we might be unable to resolve.

Nintex Workflow however is far too tempting to pass up. Traditionally our method has been to suggest the built in workflows wherever possible, and to use SharePoint designer as another (uncomfortable) option for simple workflows. SharePoint designer has the advantage of being fast and simple, however workflows created this way must be created in-situ as they are hard-wired into the environment they are created in. This is not a great situation for us, as we prefer to develop off site on test systems and create a deployment package wherever possible.

The other option of course is to use completely custom workflows. This is a powerful option that provides full control over every aspect of the workflow, but has its own disadvantages. One of these is that even tiny changes to the workflow often require a complete rebuild and redeploy to make it happen.

Nintex Workflow takes SharePoint designer workflows and custom workflows, sticks them in a blender and serves you up the result. It provides a visual editor inside the SharePoint environment itself in order to plot out the workflow flowchart and a massive library of components ranging from built-in approval/feedback tasks similar to the standard SharePoint ones all the way to the ability to make calls to external web-services. They also provide the ability to import and export workflows, which would likely aid in deployment.

A quick dart around their website reveals they are also providing an SDK to allow developers to create their own specialised components (one of the few saving graces of SharePoint designer workflows) and a version is currently in the works targeting SharePoint 2010.

Whilst working on-site I’ve created several small workflows for their new intranet and it really is the simplest, most powerful SharePoint workflow tool I’ve yet used. If you’re maintaining, or planning, on complicated workflows, you really should give it a look.

Nintex Workflow 2007

Disclosure: I am not affiliated with Nintex in any way, have received no compensation of any kind for this review, and in fact have never had any communication with them whatsoever. I just really do think this is a great product.

Silence broken

Well, i’ve been silent for quite a while now. I seem to often spend a lot of time in design and documentation work these past few months so I don’t often come across interesting problems to solve. Hopefully that will change, at least a little, from this point on.

It is quite gratifying to know that many of my articles, some years old, are still being read and commented on today. Thanks to everyone who has left a comment over the years, it’s nice to know that I’ve been of some help.

iPhone for writers

A quick round-up of some useful and free iPhone applications for writers:

Quickpedia – Wikipedia is the first stop for quick overview reference data on just about anything. Quickpedia gives a nice, easy to use iPhone interface to the popular site.

Wikihow – On the heels of quickpedia, this useful little application gives you a direct portal to thousands of how-to articles on a range of subjects. Makes for interesting reading and you never know when you might find some obscure little tip that a character could use.

BabyNames, Baby Namer – I found these two free apps last night while browsing the appstore. I’ve been putting off renaming my characters in my current WIP primarily because I didn’t want to go digging around for my baby name book, these two apps provide tens of thousands of names in an easily searchable format. They stand to be incredibly useful.

All we need now is for a browse-able phonebook for last names and we can do away with the most bulky necessary reference material for any book.

What you have to give up to write

Apparently an “aspiring novelist’s letter” inspired a post over at Whatever yesterday- whoever that was, I bet they were -awesome-.

That aside, the post is an excellent comment on what is actually a significant problem: the prevalent idea that you must be willing to sacrifice everything in order to be a professional writer.

There are people who write sheerly for the enjoyment of it, but far and away the majority of people write to one day see themselves in print, and many dream of giving up their day job and being a full time writer themselves.

To these people the words of those who have “Made It”, the John Scalzi’s and Lawrence Block’s, mean a great deal – and there are a lot of writers out there who are writing books, blogs and articles aimed at these aspiring novelists.

A lot of these books are full of stories about the pain that it can take to become a writer. I think some people probably think they are doing a kindness by preparing others for the pain that there dreams may lead them through: but some of the stories are horrific. They tend to go something like this:

A friend of mine quit his job to become a writer. His first book sucked but he kept trying. He ran out of money but he got a subsistence job to live on and kept trying. His wife left him and took the kids, and his dog died and he was living under a bridge but he kept trying and then his twentieth book took off and now he’s living the dream!

Sure, this is an extreme example (but not -that- extreme, compared to some of the similar stories out there), but there are a lot of them. It has bled into sort of a global belief that in order to be a writer you have to be willing to sacrifice everything – kids, wife, house, your entire life. Only then are you dedicated enough to being a writer, to your dreams and your art, that you can fight your way through the struggle.

At an intellectual level, I don’t believe that. I believe that with hard consistent work and a supportive family you can one day live the dream without having to sacrifice all else that you love. But it is this sort of irrational belief that curls up in your stomach like one of Conan’s serpents and squeezes the confidence out of you, the courage from your heart, until at midnight you are sweating in your bed and staring at the ceiling and you realise that you really -don’t- want it enough to give up your family.

I had that moment. I’ll say it without reservation, there is -nothing- in this world that I want enough to give up my family. I get up in the morning, go to work and slog through anything they throw at my purely so I can go home and see my daughter’s smile and talk with my wife.

So a big thank you to Scalzi for saying what we already knew, but needed to hear someone else say.

I am reminded of an anecdote I read once, went something like this:

A man with a burning desire to play violin met a traveling violin master and begged the master to listen to him play.

“If you tell me I have potential”, he said, “I will devote my life to the violin, but I would never want to do that if I had no chance of success.”

He played and the Master sat, and afterwards said. “I am sorry, you do not have the fire.”

The man went away bitterly dejected.

Years later their paths crossed again and the man shook the Master’s hand and said, “Thankyou, I have become a successful businessman and am glad I didn’t waste my life. I am glad you were able to recognise that I had no talent.”

And the Master said, “I didn’t listen to you play.”

The man was shocked, “Why?” He asked, “Why would you have done that? I could have been great! I could have been a master myself by now!”

“Because,” said the Master, “I tell everyone they lack the fire. If you had the fire, what I said should have made no difference.”

I guess we need to remember that in the end, no matter what anyone else says, it’s down to us.

Go read Scalzi:
http://whatever.scalzi.com/2009/07/29/what-you-have-to-give-up-to-write/

Love that Capitalism

Nokia sells spy systems to Iran

I find it amazing that so many people can wholeheartedly embrace Capitalism, and then get angry about things like this. The rabid defense of Capitalism makes me suspect that it has gotten too tied in with the western national identities and too few people actually understand what it is, why it’s good, or even -if- it is good. It’s become a synonym for “us”, “good” and “freedom”, much the same way that 50’s propaganda made Communism a synonym for “evil”, “scary”, “violent” and “totalitarianist”. (Though, admittedly, the communist regimes themselves did much to aid that.)

Bottom line: You have a free market and don’t be surprised when people in it realise that they can make large profits selling nasty things to nasty people. Capitalism has no social conscience. (Which is why we have regulated, moderate capitalist markets, and not complete free-market capitalism. To protect us from people like that.)