XAMLX工作流与C#表达式

我正在vs2012 / .Net 4.5 / WF 4.5中开发一个自主主机工作流程,并且很难找出以下消息

表达式活动类型'CSharpValue`1'需要编译才能运行。 请确保工作流程已编译完成。

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

浏览MSDN我遇到了这些文章:

  • http://msdn.microsoft.com/en-us/library/ee358749.aspx
  • http://msdn.microsoft.com/en-us/library/29110be7-f4e3-407e-8dbe-78102eb21115#CodeWorkflows
  • 他们这样说

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

    所以,我终于明白我的问题了: 我该怎么做,我可以打F5并调试我的工作流程,让它在IIS上运行? 并阻止这个该死的例外......

    我试图去到项目配置并设置为使用本地IIS如下所示: iis配置

    但因为我仍然得到错误,我认为它不工作...


    老问题,但在将各种信息拼凑在一起之后,我想我会分享以防其他人遇到这种情况。

    从C#表达式MSDN文档:

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

    在添加自定义WorkflowHostFactory之前,这是真实的。 在那种情况下,如果你重写错误的方法,你的C#将不会被编译。 下面的代码不会编译C#表达式,你会得到可怕的:

    表达式活动类型'CSharpValue`1'需要编译才能运行。 请确保工作流程已编译完成。

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

    System.ServiceModel.FaultException:操作无法执行,因为WorkflowInstance'5cfc33d1-b546-4ba8-a8ec-86d3cb16a68b'被中止。

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

    您可以通过覆盖其他工厂方法来解决此问题,然后使用Workflow Foundation 4.5中通过MSDN C#表达式提供的代码编译您的活动:

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

    尽管文章中提到的有关在IIS / WAS上运行的内容,但我只是在实施面料时才成功运行了工作流程......这不是问题的答案......它更像是一种解决方法...

    链接地址: http://www.djcxy.com/p/95881.html

    上一篇: XAMLX Workflow with c# expressions

    下一篇: adding WCF reference to a workflow project