Cannot create a new WorkItem within a CommandHandler

Topics: CAB & Smart Client Software Factory
Jan 21, 2007 at 11:08 AM
Does anyone know why this would be the case. Try as I may, using all the possibilities in the documentation, I cannot create a new WorkItem within a CommandHandler event of a parent WorkItem.

Inexplicably, nothing works.
If I use the documentations advice on injecting State into the child, it throws a very verbose exception, if I create the WorkItem and set a property on it, it throws a verbose exception in the creation of it. I absolutely cannot create a WorkItem.

For example, the code below, for which you can assume I have state called CurrentDTO, throws this fantastic exception below it

CommandHandler(CabCommands.ExportCsv)
public void OnExportCsv(object sender, EventArgs e)
{
ExportCsvWorkItem workItem = Items.AddNew<ExportCsvWorkItem>();
workItem.CurrentDTO = CurrentDTO;
workItem.Run();
}



===================================

Exception has been thrown by the target of an invocation. (mscorlib)

------------------------------
Program Location:

at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at Microsoft.Practices.ObjectBuilder.PropertySetterStrategy.InjectProperties(IBuilderContext context, Object obj, String id)
at Microsoft.Practices.ObjectBuilder.PropertySetterStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.CreationStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.ReflectionStrategy`1.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild)
at Microsoft.Practices.ObjectBuilder.TypeMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.CompositeUI.Collections.ManagedObjectCollection`1.BuildFirstTimeItem(Type typeToBuild, String idToBuild, Object item)
at Microsoft.Practices.CompositeUI.Collections.ManagedObjectCollection`1.Build(Type typeToBuild, String idToBuild, Object item)
at Microsoft.Practices.CompositeUI.Collections.ManagedObjectCollection`1.AddNew(Type typeToBuild, String id)
at Microsoft.Practices.CompositeUI.Collections.ManagedObjectCollection`1.AddNewTTypeToBuild()
Jan 21, 2007 at 3:43 PM
I ended up solving this by explicitly setting the State in the child WorkItem. Sort of inexplicable, but it works...

ExportCsvWorkItem csvWorkItem = Items.AddNew<ExportCsvWorkItem>();
csvWorkItem.StateStateConstants.CurrentDto = CurrentDTO;
csvWorkItem.Run();