ASP.NET invoke workflows and services synchronously/asynchronously
在ASP.NET同步调用workflow或service是比较简单的,直接用workflowInvoker或workflowApplication运行workflow,用生成的代理类调用service中的方法。
异步调用则首先需要在页面指令中添加:
<%@ Page Async="true" %>
然后service可以在添加引用的时候,指定创建异步调用的方法(WCF实现异步执行的方法是另一个话题,这里指的客户端异步的去调用)。
var proxy = new TestServiceClient(); proxy.DoWorkCompleted += (o, args) => { if (!args.Cancelled) { this.Trace.Write(string.Format("Completed calling service on thread {0} delay {1}", Thread.CurrentThread.ManagedThreadId, args.Result)); this.labelDelay.Text = args.Result.ToString(); } }; proxy.DoWorkAsync(delay);
workflow相对复杂一点,需要指定workflow host程序的SynchronizationContext为ASP.NET的当前SynchronizationContext,并显示的开始和结束异步调用
var workflowApplication = new WorkflowApplication(SayHelloDefinition, input) { // Tell the workflow runtime we are using the ASP.NET synchronization context SynchronizationContext = SynchronizationContext.Current, Completed = args => { this.Trace.Write( string.Format("Completed workflow on thread {0}", Thread.CurrentThread.ManagedThreadId)); // Update page controls here this.labelGreeting.Text = args.Outputs["Greeting"].ToString(); // Tell ASP.NET we are finished SynchronizationContext.Current.OperationCompleted(); } }; this.Trace.Write(string.Format("Starting workflow on thread {0}", Thread.CurrentThread.ManagedThreadId)); // Tell ASP.NET we are starting an async operation SynchronizationContext.Current.OperationStarted(); workflowApplication.Run(); // Don't try anything here - it will run before your workflow has completed
Addition: ASP.NET Tracing 确实提供很多详尽的信息
<%@ Page Trace="true" %>
http://blogs.msdn.com/b/rjacobs/archive/2011/03/16/asp-net-wf4-wcf-and-async-calls.aspx

浙公网安备 33010602011771号