locked
Why can I not load a workflow using ActivityXamlServices.Load when it contains a custom activity? RRS feed

  • Question

  • Created a new workflow console application
    Added a new code activity.
    Built the application.
    Dragged on a flowchart activity.
    Dragged on the new code activity.
    Replaced the default code of the Main method for the console application with the following:

    Activity a = ActivityXamlServices.Load(@"c:\visual studio 10\Projects\WorkflowConsoleApplication1\WorkflowConsoleApplication1\Workflow1.xaml");

    Get the following error:
    Cannot create unknown type '{clr-namespace:WorkflowConsoleApplication1}CodeActivity1'.


    Friday, October 23, 2009 8:37 PM

Answers

  • CodeActivity1 is a type local to an assembly that you're using to compile itself, so it gets put in a namespace that is serialized by VisualStudio in local-assembly format ("clr-namespace=foo", no ";assembly=bar"). When you compile it, XamlBuildTask specifies the LocalAssembly on the XamlXmlReaderSettings, and if you want to use ActivityXamlServices instead of just saying new Workflow1() you have to do the same thing XamlBuildTask would do and provide the LocalAssembly.

    XamlServices
    .Load(ActivityXamlServices.CreateReader(new XamlXmlReader(@"Workflow1.xaml", new XamlXmlReaderSettings { LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly() })));

    This requirement is not unique to ActivityXamlServices, you'd have to do the same thing (provide XamlXmlReaderSettings.LocalAssembly) if you wanted to do a plain old XamlServices.Load on XAML that didn't specify an assembly:

    XamlServices.Parse(@"<Foo xmlns=""clr-namespace:WorkflowConsoleApplication9""/>"); // fails, Foo unknown because no assembly is specificed in namespace

    XamlServices.Load(new XamlXmlReader(XmlReader.Create(new StringReader(@"<Foo xmlns=""clr-namespace:WorkflowConsoleApplication9""/>")), new XamlXmlReaderSettings { LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly() })); // succeeds

    -Max


    Max Wilson
    • Proposed as answer by MaxWil - MSFT Monday, October 26, 2009 11:00 PM
    • Marked as answer by SameerX8 Wednesday, October 28, 2009 9:27 AM
    Monday, October 26, 2009 9:46 PM

All replies

  • CodeActivity1 is a type local to an assembly that you're using to compile itself, so it gets put in a namespace that is serialized by VisualStudio in local-assembly format ("clr-namespace=foo", no ";assembly=bar"). When you compile it, XamlBuildTask specifies the LocalAssembly on the XamlXmlReaderSettings, and if you want to use ActivityXamlServices instead of just saying new Workflow1() you have to do the same thing XamlBuildTask would do and provide the LocalAssembly.

    XamlServices
    .Load(ActivityXamlServices.CreateReader(new XamlXmlReader(@"Workflow1.xaml", new XamlXmlReaderSettings { LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly() })));

    This requirement is not unique to ActivityXamlServices, you'd have to do the same thing (provide XamlXmlReaderSettings.LocalAssembly) if you wanted to do a plain old XamlServices.Load on XAML that didn't specify an assembly:

    XamlServices.Parse(@"<Foo xmlns=""clr-namespace:WorkflowConsoleApplication9""/>"); // fails, Foo unknown because no assembly is specificed in namespace

    XamlServices.Load(new XamlXmlReader(XmlReader.Create(new StringReader(@"<Foo xmlns=""clr-namespace:WorkflowConsoleApplication9""/>")), new XamlXmlReaderSettings { LocalAssembly = System.Reflection.Assembly.GetExecutingAssembly() })); // succeeds

    -Max


    Max Wilson
    • Proposed as answer by MaxWil - MSFT Monday, October 26, 2009 11:00 PM
    • Marked as answer by SameerX8 Wednesday, October 28, 2009 9:27 AM
    Monday, October 26, 2009 9:46 PM
  • Thanks for your response Max.

    What I tried to move ahead was to add a new activity library project to the solution and moved my custom activity there.
    I added a reference to the new activity library project in the console application.
    The same code now works.

    I suppose the assembly reference now resolves correctly as the output type of the activity library is an assembly that is now available for the XamlServices.Load to work.

    -Sameer




    Tuesday, October 27, 2009 1:50 PM
  • Hi Thanks for your Answer it was more Helped me
    Wednesday, July 21, 2010 6:16 AM