c# - 当 C# 表达式使用引用类型时 Activity 抛出异常

标签 c# workflow-foundation-4

我有一个原始的“作业”事件。它从父序列的输入变量中获取值,并将它们放入父序列的输出变量中。如果我简单地执行这个复制操作,一切正常,工作流成功完成。如果我引入系统类型“Random”或项目枚举“ReportStatusType”,工作流将抛出异常“抛出 System.NotSupportedException:“表达式事件类型‘CSharpValue`1’需要编译才能运行。”

我根据 wf 4 的示例创建了此工作流。我认为我使用的是 wf 4.5。我在 VS2013 中工作,目标是 .NET Framework 4.5。我使用了使用 IIS 平台的“WCF 工作流服务应用程序”模板。我正在使用“WCF 测试客户端”来调用服务并查看响应。

这个值表达式有效:

new ExpenseReportConfirmation() {
       Amount = report.Amount,
       City = report.Amount,
       Client = report.Client,
       Employee = report.Employee,
       EndDate = report.EndDate,
       StartDate = report.StartDate,
       ReportID = 5
    };

此值表达式失败:

new ExpenseReportConfirmation() {
       Amount = report.Amount,
       City = report.Amount,
       Client = report.Client,
       Employee = report.Employee,
       EndDate = report.EndDate,
       StartDate = report.StartDate,
       ReportID = new Random().Next(0,5),
    };

我导入的命名空间如下所示:

enter image description here

如果我尝试在另一个分配事件中创建 ReportID,然后在上面显示的值表达式中引用它,这也会失败。在表达 Random() 的任何地方它都会失败。

这对我来说可能是一个菜鸟错误,但我没有主意。还有人有吗?

最佳答案

来自 here :

C# expressions are supported in XAMLX workflow services. 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. To compile the C# expressions in a self-hosted XAMLX workflow service, first load the XAMLX file into a WorkflowService, and then pass the Body of the WorkflowService to the CompileExpressions method described in the previous Using C# expressions in code workflows section. In the following example, a XAMLX workflow service is loaded, the C# expressions are compiled, and then the workflow service is opened and waits for requests.

所以你可以:

// Load the XAMLX workflow service.
WorkflowService workflow1 =
    (WorkflowService)XamlServices.Load(xamlxPath);

// Compile the C# expressions in the workflow by passing the Body to CompileExpressions.
CompileExpressions(workflow1.Body);

// Initialize the WorkflowServiceHost.
var host = new WorkflowServiceHost(workflow1, new Uri("http://localhost:8293/Service1.xamlx"));

// Enable Metadata publishing/
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);

// Open the WorkflowServiceHost and wait for requests.
host.Open();
Console.WriteLine("Press enter to quit");
Console.ReadLine();

选项 2 - 使用自定义主机工厂(可能更容易和更直接)

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

现在将工厂添加到您的 web.config 中:

<system.serviceModel>
    <serviceHostingEnvironment>
        <serviceActivations>
             <add relativeAddress="WcfWorkflow.xalmx" service="WcfWorkflow.xalmx" factory="Namespace.CSharpWorkflowServiceHostFactory" />
        </serviceActivations>
    </serviceHostingEnvironment>
</system.serviceMode>

关于c# - 当 C# 表达式使用引用类型时 Activity 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915005/

相关文章:

c# - 如何检查我的计算机上是否打开了另一个应用程序窗口(即遍历所有打开的窗口)?

c# - 使用 ApplyConfigurationsFromAssembly() 程序集扫描时访问 IEntityTypeConfiguration<T> 内的 DI 服务

c# - 在 Try/Catch 中包含 ExecuteReader 使其成功?

wcf - 将 ChannelFactory 与 WorkflowServiceHost 一起使用

c# - WF4工作流服务和 Entity Framework 问题

c# - SetWindowsHookEx,键盘 Hook

c# - 是否可以使 SubItem 在 ListView 中可点击

c# - Silverlight 中 WF4 的设计工作流

workflow-foundation - 具有 OutArgument 和分配事件的自定义 WF4 事件

wcf - 为每个工作流程定义不同的默认 ServiceHostFactory