Tuesday, September 25, 2012

Lightswitch error ‘Exception has been thrown by the target of an invocation’

Problem

This error message popped up after the Lightswitch 2011 application was deployed on a new server and when the application was started for the first time. The default screen comes up, but without data in the screen, then the messagebox with the error:

An error occurred while running the screen.
Error details: Exception has been thrown by the target of an invocation.

image

After clicking ‘Close Screen’ a second messagebox pops up with the message:

Invalid operation. Dispatcher has been stopped.

image

The problem in this case is not as much correcting the error, but finding out what the error actually is. To do so we need to turn diagnostics on

DIAGNOSE

To diagnose this kind of error in Lightswitch, diagnostics needs to be enabled. See the MSDN article How to: Enable Diagnostics

Since this problem did not occur on my development computer, but after deployment on the new Windows Server, we can turn the diagnostics on in the server without redeploying the solution. Either way do not forget to switch diagnostics off on the server once the cause of the error has been found.

To enable diagnostics on the server

The diagnostics feature will generate a Trace.axd file in the directory where the app is deployed.

  • Use IIS Manager and connect remotely or log on to the server and start IIS Manager on the server.
  • In the Connections treeview, under <your server>/sites/Default Web site/<your app>
  • In the features view, click on Application Settings
    image
  • In the Application settings pane, change Microsoft.LightSwitch.Trace.Enabled to true,
  • Set …trace.Lavel to Error
  • Set …trace.LocalOnly to false. If you leave this to true then you can only browse on the server to localhost to look at the diagnostics, in my case security settings on the server browser did not allow me to do this. So when set to false I can browse the trace file from the client PC.
  • This is the Applications settings panel after applying the changesimage
  • On the client PC browse to the app to invoke the error once more.
    http://myserver/myapp
  • Then browse to the same URL with the trace file name appended http://myserver/myapp/Trace.axd
    image
  • The last line of the trace log most probably contains the error information (in my case it did). Click on the view details link to open.
    image
  • The error shows that there is a SQLconnection problem.

Solution

finally the solution was that the SQLserver could not be connected from the new app server unless the full domain name was used. I fixed the connection string in visual studio accordingly and all worked fine.

2 comments:

Paul Van Bladel said...

Jan,
Excellent write-up.
I recently found out how to debug silverlight client side problems (see: http://blog.pragmaswitch.com/?p=415)
I'm mentioning, because the error you describe here is sometimes purely client side.
paul.

Jan D'Hondt said...

Paul, Thank you.