Cannot subscribe to aggregate events

Topics: Prism v2 - Silverlight 3
Mar 1, 2010 at 5:54 AM

Hi All

I have been following the video series by Mike Taulty: http://channel9.msdn.com/posts/mtaulty/Prism--Silverlight-Part-1-Taking-Sketched-Code-Towards-Unity/

Without getting too deep, when ever I subscribe to an aggregate command, I get the following error:

{System.InvalidOperationException: The parameter model could not be resolved when attempting to call constructor CALSilverlight.ModuleB.Views.View1(CALSilverlight.ModuleB.ModuleBView1ViewModel model). ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[CALSilverlight.ModuleB.ModuleBView1ViewModel, null]) failed: CALSilverlight.ModuleB.ModuleBView1ViewModel.OnTextChangedEvent(System.String) (Strategy type BuildPlanStrategy, index 3) ---> System.MethodAccessException: CALSilverlight.ModuleB.ModuleBView1ViewModel.OnTextChangedEvent(System.String)
   at System.Delegate.BindToMethodInfo(Object target, RuntimeMethodHandle method, RuntimeTypeHandle methodType, DelegateBindingFlags flags)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
   at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
   at Microsoft.Practices.Composite.Events.DelegateReference.TryGetDelegate()
   at Microsoft.Practices.Composite.Events.DelegateReference.get_Target()
   at Microsoft.Practices.Composite.Events.EventSubscription`1..ctor(IDelegateReference actionReference, IDelegateReference filterReference)
   at Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent`1.Subscribe(Action`1 action, ThreadOption threadOption, Boolean keepSubscriberReferenceAlive, Predicate`1 filter)
   at Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent`1.Subscribe(Action`1 action, ThreadOption threadOption, Boolean keepSubscriberReferenceAlive)
   at Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent`1.Subscribe(Action`1 action, ThreadOption threadOption)
   at Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent`1.Subscribe(Action`1 action)
   at CALSilverlight.ModuleB.ModuleBView1ViewModel..ctor(ITextService textService, IEventAggregator eventAggregator)
   at BuildUp_CALSilverlight.ModuleB.ModuleBView1ViewModel(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)
   at BuildUp_CALSilverlight.ModuleB.Views.View1(IBuilderContext )
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForResolutionFailed(Exception inner, String parameterName, String constructorSignature, IBuilderContext context)
   at BuildUp_CALSilverlight.ModuleB.Views.View1(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)}

 

 

Here is the view that is registering the viewmodel that is causing the exception:

public partial class View1 : UserControl
    {
        public View1(ModuleBView1ViewModel model)
        {
            InitializeComponent();

            this.Loaded += (s,e) =>
                {
                    this.DataContext = model;
                };
        }
    }

 

Here is the contructor for the ViewModel that causes the exception:

public ModuleBView1ViewModel(ITextService textService, IEventAggregator eventAggregator)
        {
            this.textService = textService;
            TextChangedEvent evt = eventAggregator.GetEvent<TextChangedEvent>();
            evt.Subscribe(OnTextChangedEvent);
        }

 

When the line: evt.Subscribe(OnTextChangedEvent); is called, I get the above error??

Any help at all would be fantastic as I have lost days chasing my tail on this one.

 

Regards

 

Nick

 

Mar 1, 2010 at 8:24 PM
nickwoodgts wrote:

System.MethodAccessException:

<snip>

Off hand, check that you really have an OnTextChangedEvent method defined and check the visibility of the method, is it public? private?

 

 

Mar 1, 2010 at 10:02 PM

Hi Waday

 

Thanks for the reply. That was exactly it!!!!

the method was private. Cant break old habbits because as the method was essencially for the local class, I marked it private and then even over I went over and over it, I never noticed the visibility of the samples that I was comparing it too.

 

Thanks again, you have saved me several keyboards for sure!.

 

Nick

 

Mar 1, 2010 at 10:05 PM

Glad it helped. Total guess on my part :-)