gpt4 book ai didi

c# - 带有 c# 表达式的 XAMLX 工作流

转载 作者:太空宇宙 更新时间:2023-11-03 13:50:51 25 4
gpt4 key购买 nike

我正在 vs2012/.Net 4.5/WF 4.5 中开发自托管工作流,并且很难弄清楚以下消息

Expression Activity type 'CSharpValue`1' requires compilation in order to run. Please ensure that the workflow has been compiled.

当我调用由服务引用生成的事件时会发生此错误(当您添加 WCF 服务引用时,端点上的每个操作都会成为一个事件)。

在 MSDN 中环顾四周,我发现了这些文章:

他们说

When a workflow service is hosted in IIS or WAS then no additional steps are required, but if the XAML workflow service is self-hosted, then the C# expressions must be compiled

所以,我终于回答了我的问题:我应该怎么做才能按 F5 键并调试我的工作流,让它在 IIS 上运行?并停止这个该死的异常......

我尝试转到项目配置并设置为使用本地 IIS,如下所示:iis config

但由于我仍然收到错误,我认为它无法正常工作...

最佳答案

老问题,但在将各种信息拼凑了一天之后,我想我应该分享一下,以防其他人遇到这个问题。

来自 C# 表达式 MSDN 文档:

XAMLX 工作流服务支持 C# 表达式。 如果工作流服务托管在 IIS 或 WAS 中,则不需要额外的步骤,但如果 XAML 工作流服务是自托管的,则必须编译 C# 表达式。

在您添加自定义 WorkflowHostFactory 之前,这是真的。在这种情况下,如果您覆盖了错误的方法,您的 C# 将不会被编译。以下代码不会编译 C# 表达式,您将得到可怕的结果:

Expression Activity type 'CSharpValue`1' requires compilation in order to run. Please ensure that the workflow has been compiled.

或者,如果您不查看跟踪消息,则更有帮助:

System.ServiceModel.FaultException: The operation could not be performed because WorkflowInstance '5cfc33d1-b546-4ba8-a8ec-86d3cb16a68b' was aborted.

   public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
return base.CreateServiceHost(constructorString, baseAddresses);
}

您可以通过覆盖其他工厂方法然后使用 MSDN C# Expressions in Workflow Foundation 4.5 提供的代码编译您的事件来解决此问题:

 protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
{
CompileExpressions(service.Body);
return base.CreateWorkflowServiceHost(service, baseAddresses);
}

这是整个 WorkflowServiceHostFactory:

 public class MyWorkflowServiceHostFactory : WorkflowServiceHostFactory
{
protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
{
CompileExpressions(service.Body);
return base.CreateWorkflowServiceHost(service, baseAddresses);
}

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);
}
}

关于c# - 带有 c# 表达式的 XAMLX 工作流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13753276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com