Validation error when you use IActivityTemplateFactory to generate activities with custom types through VisualBasicValue or VisualBasicReference in Windows Workflow Foundation

Article translations Article translations
Article ID: 2015641 - View products that this article applies to.
Expand all | Collapse all

Symptoms

In Windows Workflow Foundation (WF) 4.0, you  use IActivityTemplateFactory to create an activity that has some properties preconfigured. When you do this, all the IActivityTemplateFactory that you defined are shown in the toolbox as activity templates. This allows you to  drop the activities into WF designer to create preconfigured activities.

Sometimes you may need to use a variable of a custom type in a VisualBasicValue or VisualBasicReference instance to preconfigure the activities. In this case, after you drop the IActivityTemplateFactory activity into WF designer, you receive a validation error. This validation error states that the variable you used is not declared. For example, the following code sample causes  the following compile error message when you drop the IActivityTemplateFactory activity into the WF designer.

Compile error message

 “Compiler error(s) encountered processing expression "MyName.Id". 'MyName' is not declared. It may be inaccessible due to its protection level”  

Code Sample

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Presentation;
using System.Activities.Statements;
using System.ServiceModel.Activities;
using Microsoft.VisualBasic.Activities;


namespace WorkflowConsoleApplication1
{
    public class MyCustomType
    {
        public string Id { get; set; }
        public int Number { get; set; }
    }


    public class MyActivity : Activity
    {
        public ActivityFunc<MyCustomType, bool> MyFunc { get; set; }
    }

 
    public class MyActivityTF : IActivityTemplateFactory
    {
        public Activity Create(System.Windows.DependencyObject target)
        {
            DelegateInArgument<MyCustomType> inArg = new DelegateInArgument<MyCustomType>("MyName");
            DelegateOutArgument<bool> outArg = new DelegateOutArgument<bool>();

            return new MyActivity
            {
                MyFunc = new ActivityFunc<MyCustomType, bool>
                {
                    Argument = inArg,
                    Handler = new Sequence
                    {
                        Activities =
                        {
                            new Send
                            {
                                ServiceContractName = System.Xml.Linq.XName.Get("TestContract", "http://foo.org"),
                                OperationName = "Foo",
                                EndpointConfigurationName = "MyFooEndpoint",
                                Content = new SendParametersContent
                                {
                                    Parameters =
                                    {
                                        { "parameter1", (InArgument<string>)new VisualBasicValue<String>("MyName.Id") }
                                    }
                                }
                            },
                            new Assign<bool> { To = outArg, Value = new InArgument<bool>(false) }
                        }
                    },
                    Result = outArg
                }
            };
        }
    }
}

Cause

This validation error occurs because the required namespaces and assemblies where you defined the custom types are not added automatically into the XAML file.

Resolution

To resolve this issue, programmatically add the namespaces and assemblies into the XAML file when you drop the IActivityTemplateFactory activity. To do this, add code that resembles the following inside the public Create(System.Windows.DependencyObject target) method of IActivityTemplateFactory activity:

public Activity Create(System.Windows.DependencyObject target)
{
    // …

    WorkflowViewElement element = target as WorkflowViewElement;
    if (element != null)
    {
        ModelService modelService = element.Context.Services.GetService<ModelService>();
        object rootActivity = modelService.Root.GetCurrentValue();
        VisualBasicSettings settings = VisualBasic.GetSettings(rootActivity);
        if (settings != null)
        {
            settings.ImportReferences.Add(new VisualBasicImportReference()
            {
                Import = "ClassLibrary1", // change this to your required namespace
                Assembly = "ClassLibrary1" // change this to your required assembly
            });

            // you can add more import reference if needed.
        }
        VisualBasic.SetSettings(rootActivity, settings);
    }

// …

}

More Information

For more information about VisualBasicSettings, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.activities.visualbasicsettings(VS.100).aspx

Workaround

To work around this issue, use one of the following methods:

    1. Explicitly import the request namespaces through Import Designer.
    2. Force the WF designer to reload the XAML file. To do this, close the WF designer and then reopen it.

Then, build the project.

Note You can only use this method in Microsoft Visual Studio together with the WF designer to reference the required assemblies.

Properties

Article ID: 2015641 - Last Review: April 12, 2010 - Revision: 1.0
APPLIES TO
  • Windows Workflow Foundation
Keywords: 
KB2015641

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com