1

想要一种使用 REST 而不是 SOAP 调用 WCF 工作流服务应用程序的方法。我已经使用自定义 webhttpbehavior 定义了一个 REST 端点,并且从该调用中我试图加载 XAMLX 并运行它。

我的第一次尝试失败了


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

我在调用工作流之前找到了编译表达式的代码,然后我得到了类似的错误。


    The type or namespace name 'Activities' does not exist in the namespace 'System'

我找到了一些其他的代码来设置

 AttachableMemberIdentifier and AttachablePropertyServices

然后我得到了

The type or namespace name 'Activities' does not exist in the namespace 'System.ServiceModel'

​尽管我正在为所有 3 个命名空间(我的本地解决方案,System.ServiceModel 和 System.Activities)添加程序集引用​</p>

xamlx 是简单的开箱即用的,它使用 GetData 生成一个 int 并返回 int.ToString()

我错过了什么?

代码如下:


    namespace WFStarterSolution
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RestService
    {

        static void CompileExpressions(DynamicActivity activity)
        {
            var activityName = activity.Name;

            // 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.
            var activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
            // Take everything before the last . for the namespace.
            var activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());

            // Create a TextExpressionCompilerSettings.
            var settings = new TextExpressionCompilerSettings
            {
                Activity = activity,
                Language = "C#",
                ActivityName = activityType,
                ActivityNamespace = activityNamespace,
                RootNamespace = null,//"CSharpExpression",
                GenerateAsPartialClass = false,
                AlwaysGenerateSource = true,
                ForImplementation = true
            };

            // Compile the C# expression.
            var results = new TextExpressionCompiler(settings).Compile();

            // Any compilation errors are contained in the CompilerMessages.
            if (results.HasErrors)
            {               
                var cm = results.CompilerMessages.Aggregate(" 
", (current, e) => current + (e.Number + " - "+e.Message + " : Line Number "+e.SourceLineNumber + "
")); throw new Exception("Compilation failed."+cm); } // Create an instance of the new compiled expression type. var compiledExpressionRoot = Activator.CreateInstance(results.ResultType, new object[] { activity }) as ICompiledExpressionRoot; // Attach it to the activity. CompiledExpressionInvoker.SetCompiledExpressionRoot( activity, compiledExpressionRoot); } [OperationContract] public string DoWork() { // call WFService XAMLX somehow var filepath = AppDomain.CurrentDomain.BaseDirectory; try { var serviceImplementation = XamlServices.Load(filepath + "WFService.xamlx"); var service = serviceImplementation as WorkflowService; if (service == null) { return "Failed"; } else { var activity = service.Body; var operand1 = new InArgument(); var dyanamicActivity = new DynamicActivity { Name = "WFServiceName", Implementation = () => activity}; var p = new DynamicActivityProperty { Name = "data", Type = typeof(InArgument), Value = operand1 }; dyanamicActivity.Properties.Add(p); var impl = new AttachableMemberIdentifier(typeof(TextExpression), "NamespacesForImplementation"); var namespaces = new List { "WFStarterSolution" }; var ar = new[] { new AssemblyReference { Assembly = typeof (DynamicActivity).Assembly }, new AssemblyReference { Assembly = typeof (RestService).Assembly }, new AssemblyReference { Assembly = typeof (ServiceContractAttribute).Assembly } }; TextExpression.SetReferencesForImplementation(dyanamicActivity, ar); AttachablePropertyServices.SetProperty(dyanamicActivity, impl, namespaces); CompileExpressions(dyanamicActivity); var iDict = new Dictionary() { { "data", 45} }; var output = WorkflowInvoker.Invoke(dyanamicActivity, iDict); return "success"; } } catch (Exception ex) { return ex.Message+"
"+ex.StackTrace; } } } }

更新* 如果我将以下内容添加到 AssemblyReference 数组中


,new AssemblyReference 
{
    Assembly = typeof (WorkflowService).Assembly
}

,它编译得很好......但仍然给我原来的错误


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

0 回答 0