想要一种使用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
我找到了在调用工作流之前编译Expression的代码,然后我收到类似的错误。
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)
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