none
Visual Studio 2008 WPF IDE Debugger "Exception has been thrown by the target of an invocation"

    Question

  •  

    Hi,

     

    I'm not quite sure if this is the correct forum for this question (it may be more IDE or Debug related)  but I'll try here first anyway.

     

    I've written a WPF application the compiles and runs successfully.  However when I bring up one of my WPF .xaml pages it underlines one of my ObjectDataProvider tags in blue and gives me the non helpful message as follows:

     

    Exception has been thrown by the target of an invocation

     

    I've checked my code and can't see anything wrong and like I said- the program does run as expected.

    Then I hear you say - "Well if your program works who cares about the message".  Ahh but the problem is whilst this error message exists I can't display the page in the designer and I can't see the page properties.  Perhaps it doesn't like databinding in user controls?

     

    Code Block

    <UserControl x:Class="IB.WESS.UI.FieldSales.Contracts.ContractUserControls.UC_Conditionality"

    xmlns:local="clr-namespace:IB.WESS.UI.FieldSales.Contracts.ContractUserControls"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Height="Auto" Width="Auto">

    <UserControl.Resources>

    <ObjectDataProvider ObjectType="{x:Type local:ProductCategories}" x:Key="odpProductCategories" />

    <ObjectDataProvider ObjectType="{x:Type local:PaymentTypes}" x:Key="odpPaymentTypes" />

    <ObjectDataProvider ObjectType="{x:Type local:TrackingFrequencies}" x:Key="odpTrackingFrequency"/>

    <ObjectDataProvider ObjectType="{x:Type local:ConditionalityGroups}" x:Key="odpConditionalityGroups" />

    </< FONT>UserControl.Resources>

    etc...

     

     

     

    code behind class of the 1st Object (rest of the objects are the same)

     

    Code Block

    public class ProductCategories : ObservableCollection<ProductCategoryEntity>

    {

    public ProductCategories()

    {

    ProductCategoryCollection products = new ProductCategoryCollection();

    BaseDataManager baseDataManager = new BaseDataManager();

    products = baseDataManager.GetAllProductCategories();

     

    foreach (ProductCategoryEntity productCategoryEntity in products)

    {

    this.Add(productCategoryEntity);

    }

    }

    }

     

     

     

    Now I am still using Visual Studio 2008 beta.  May be this is an issue that is fixed in the recently released MSDN version?

     

    May be there is some way to get the IDE to give me a more detailed description of what it's complaining about?

     

    Any help / pointers would be great.

     

    Thanks

    Tuesday, January 22, 2008 3:14 PM

Answers

  • Hi,

    I only know this exception from using Reflection, in which case you get the exact error whenever you're invoking a method via reflection and something goes wrong along the way.

    Are you sure your classes are prepared to be called at design time? Could it be that your "GetAllProductCategories()" method relies on some database being initialized at application startup? I'd comment out the constructor completely and then add it again line by line to see which call is causing the problem.

    John
    Tuesday, January 22, 2008 4:38 PM
  • Thanks John,

     

    You have put me on the right track.

    I've commented out all the code and added it back in bit by bit, recompiling each time.

    The offending line is

     

    BaseDataManager baseDataManager = new BaseDataManager();

     

    I guess I'll just need to comment code out within that class also to see if I can work out what the problem is.

    Such a shame that I get such a non specific error message that doesn't really point to the actual problem.

     

    I'm hoping the release Version of Visual Studio 2008 is a bit more helpful.

     

    Thanks again.

     

    Matt.

    Wednesday, January 23, 2008 9:53 AM

All replies

  • Hi,

    I only know this exception from using Reflection, in which case you get the exact error whenever you're invoking a method via reflection and something goes wrong along the way.

    Are you sure your classes are prepared to be called at design time? Could it be that your "GetAllProductCategories()" method relies on some database being initialized at application startup? I'd comment out the constructor completely and then add it again line by line to see which call is causing the problem.

    John
    Tuesday, January 22, 2008 4:38 PM
  • Thanks John,

     

    You have put me on the right track.

    I've commented out all the code and added it back in bit by bit, recompiling each time.

    The offending line is

     

    BaseDataManager baseDataManager = new BaseDataManager();

     

    I guess I'll just need to comment code out within that class also to see if I can work out what the problem is.

    Such a shame that I get such a non specific error message that doesn't really point to the actual problem.

     

    I'm hoping the release Version of Visual Studio 2008 is a bit more helpful.

     

    Thanks again.

     

    Matt.

    Wednesday, January 23, 2008 9:53 AM
  •  I have also experienced this phenomenom when using an ObjectDataProvider calling a method on an ObjectInstance that is set to reference a static property on a class.  I believe my situation may be caused by the fact that the property being referenced is loading an instance of a custom provider based on settings in the app.config file.  Also, I am passing in MethodParameter, which may have some impact on things.  I am currently speculating that there may be an issue with the configuration settings being loaded at design time, largely because I get the same error at runtime if I either remove or otherwise screw up the configuration settings.  Currently I have no way of testing this for sure though.  Any suggestions are greatly appreciated.
    Tuesday, June 03, 2008 10:24 PM
  • I am also receiving this message in a wpf/xbap application.  My error message states 

    System.Reflection.TargetInvocationException was unhandled

    Message: Exception has been thrown by the target of an invocation.


    The error occurs intermittently when I call a web service method which populates a datagrid on my page.

    This type of message is referenced in an MSDN article http://support.microsoft.com/kb/828991 but I am not sure if it is referencing the specific problem I am having since my error message is not as specific as the message outlined in that article.

    We also notice that the error occurs intermittently.  When we retreive a record and the error occurs, we will restart the application.  When we try to retreive the same record, the error does not appear again.   Any help would be greatly appreciated.  Thanks

    Monday, July 14, 2008 9:33 AM
  • If you are using an ObjectDataProvider to invoke the service, you might try setting the "IsAsynchronous" property to true.  This cleared up some issues for me with web service invocations.  It also frees up the UI thread (if you are invoking the web service in response to a UI event).

    Monday, September 08, 2008 3:55 PM

  • Acctually, this problem usually occurs when there is a code put inside the constructor that creates some other usercontrol object that the IDE cannot create during design time.  However, the code is find at runtime.

    To continue to have the code inside the contructor, you only need to wrap the code inside a if block, as shown blow:

    if(this.IsInitialized)
    {
         someObject = new someObject();
    }


    David N.
    Friday, August 14, 2009 11:03 PM
  • The above code would work in the case of a user control, however I have an ObjectDataProvider that is not a user control.  Initially it had code within the static constructor that called out to another service.  This would error out and the xaml wouldn't render at all.  I have since moved the static constructor code into an Initialize method that is called during the defined ObjectDataProvider method. 

    This has helped, however I noticed that the defined method is actually called as well.  The xaml will now render, however in my case I still get additional Assert failed messages showing up.  Is there a way to code for design mode - so that certain code is not ran during design mode?

    Tuesday, September 22, 2009 5:33 PM