Skip to main content
 首页 » 编程设计

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

2024年02月13日35sky-heaven

我有一个原始的“作业”事件。它从父序列的输入变量中获取值,并将它们放入父序列的输出变量中。如果我简单地执行这个复制操作,一切正常,工作流成功完成。如果我引入系统类型“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), 
    }; 

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

如果我尝试在另一个分配事件中创建 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>