问题
I am new to windows workflow and trying to get my head round variables, the following code gives me an error -
public sealed class CodeActivity1 : CodeActivity
{
Variable<int> wfVar = new Variable<int>("wfVar", 0);
protected override void Execute(CodeActivityContext context)
{
wfVar.Set(context, 1);
}
}
Variable 'wfVar' of type 'System.Int32' cannot be used. Please make sure it is declared in an Activity or SymbolResolver.
What does the error mean, considering that I have declared the variable in an Activity.
Thanks, Ilias
回答1:
If you're trying to use a Variable as a CodeActivity implementation variable, you're thinking it wrong. CodeActivity should be used mostly to execute some code (within Execute() method of course), quickly, and exit.
What you want to do can be only achieved with NativeActivity. This way execution engine is aware of the existence of a variable that can be used (getted or setted) all over the activity execution.
public sealed class CustomActivity : NativeActivity
{
Variable<int> wfVar = new Variable<int>("wfVar", 0);
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
base.CacheMetadata(metadata);
metadata.AddImplementationVariable(wfVar);
}
protected override void Execute(CodeActivityContext context)
{
wfVar.Set(context, 1);
}
}
回答2:
The workflow runtime needs to be aware of your variables. By default it uses reflection over public properties. So making your variable public will do the trick. The other option is to use the CacheMetadata() function and register the variable there yourself.
来源:https://stackoverflow.com/questions/7767434/declare-variablet-variable-in-a-codeactivity-in-windows-workflow-4-0