0
我試圖使用代碼創建工作流程,如here所述。但我無法理解我的結果。我用Class1.cs創建了包含我的工作流代碼和 Program.cs的控制檯應用程序,工作流託管在WorkflowApplication類中,包括輸入。在執行未處理的異常時出現消息 「表達式活動類型'CSharpValue`1'需要編譯才能運行。請確保工作流已編譯爲」「。但我也有 包括用於編譯的CompileExpressions方法,如here所述。我提前感謝您的幫助!無法使用WF 4.5中的CSharpValue表達式訪問Dictionary
ReverseStringWorkflow.cs
public class ReverseStringWorkflow : Activity
{
public InArgument<Dictionary<string,object>> StringToReverse { get; set; }
protected override Func<Activity> Implementation
{
get
{
return() =>
{
Sequence sequence = new Sequence
{
Activities =
{
new WriteLine
{
Text = new CSharpValue<string>("StringToReverse[\"name\"].ToString()")
}
}
};
return sequence;
};
}
set
{
base.Implementation = value;
}
}
}
的Program.cs
class Program
{
static void Main(string[] args)
{
Activity workflow2 = new ReverseStringWorkflow();
Dictionary<string, object> mainInputs = new Dictionary<string, object>();
Dictionary<string, object> subInputs = new Dictionary<string, object>();
subInputs.Add("name","name123");
mainInputs.Add("StringToReverse", subInputs);
WorkflowApplication app = new WorkflowApplication(workflow2, mainInputs);
app.OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs e)
{
Console.WriteLine("Error occurred");
return UnhandledExceptionAction.Terminate;
};
CompileExpressions(workflow2);
app.Run();
Console.ReadLine();
}
public static void CompileExpressions(Activity activity)
{
// activityName is the Namespace.Type of the activity that contains the
// C# expressions.
string activityName = activity.GetType().ToString();
// Split activityName into Namespace and Type.Append _CompiledExpressionRoot to the type name
// to represent the new type that represents the compiled expressions.
// Take everything after the last . for the type name.
string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
// Take everything before the last . for the namespace.
string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
// Create a TextExpressionCompilerSettings.
TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
{
Activity = activity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = null,
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = false
};
// Compile the C# expression.
TextExpressionCompilerResults results =
new TextExpressionCompiler(settings).Compile();
// Any compilation errors are contained in the CompilerMessages.
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
// Create an instance of the new compiled expression type.
ICompiledExpressionRoot compiledExpressionRoot =
Activator.CreateInstance(results.ResultType,
new object[] { activity }) as ICompiledExpressionRoot;
// Attach it to the activity.
CompiledExpressionInvoker.SetCompiledExpressionRoot(
activity, compiledExpressionRoot);
}
}
完整的錯誤信息:
[System.NotSupportedException] {System.NotSupportedException: Expression Activity type 'CSharpValue`1' requires compilation in order to run. Please ensure that the workflow has been compiled.
at System.Activities.Expressions.CompiledExpressionInvoker.InvokeExpression(ActivityContext activityContext)
at Microsoft.CSharp.Activities.CSharpValue`1.Execute(CodeActivityContext context)
at System.Activities.CodeActivity`1.InternalExecuteInResolutionContext(CodeActivityContext context)
at System.Activities.Runtime.ActivityExecutor.ExecuteInResolutionContext[T](ActivityInstance parentInstance, Activity`1 expressionActivity)
at System.Activities.InArgument`1.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance activityInstance, ActivityExecutor executor)
at System.Activities.RuntimeArgument.TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor, Object argumentValueOverride, Location resultLocation, Boolean skipFastPath)
at System.Activities.ActivityInstance.InternalTryPopulateArgumentValueOrScheduleExpression(RuntimeArgument argument, Int32 nextArgumentIndex, ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Boolean isDynamicUpdate)
at System.Activities.ActivityInstance.ResolveArguments(ActivityExecutor executor, IDictionary`2 argumentValueOverrides, Location resultLocation, Int32 startIndex)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
看來,你必須提供完整的錯誤消息... [本文](https://blogs.msdn.microsoft.com/tilovell/2012/05/24/wf4-5-using-csharpvaluet-and-csharpreferencet-in-net-4-5-compiling-expressionsand-changes-in-visual-studio-generated-xaml /)可能會有所幫助。 –