Service Instance creation

Jul 12, 2009 at 9:24 AM

Hi, Thanks for the great template and webcast.

I was running a sample created by using ServiceHost template. I have noticed that you are creating the instance of ClientService in constructor of main window. It works fine, if all goes well on client side, but in case there is an issue, this results in incorrect error propogation.

If anything goes wrong in contructors of WPF windows, framework treats it as an Xaml parsing error. To correctly propogate error on WPF side, we should initialize services in 'Loaded' event.

You can recreate the above scenario, by creating a project from the template, dont' change anything, just make sure that you run it by user rights, not administrator. This will make sure the service is not able to register port 8010 as a listener port. Now, once ser vice host starts, it will give Xaml parsing error. If you move service instiation to 'Loaded' event of MainWindow, running the project again would give proper error and correctly point out, what went wrong.

Just a thought, since you are going for the best practice. :-)

Cheers, keep up the good work.

- Awais Bin Khalid

Coordinator
Jul 13, 2009 at 8:51 PM

Hi Awais,

Yes, I have since changed my tune and now always move initialization for WPF clients into the Window.Loaded event...I will update these templates within the week to reflect this change. Thanks for the reminder! Sometimes hard to remember all the places you have to go back when you learn a new best practice!

-Michele