如何在 WF 4.0 中编写长时间运行的活动来调用 Web 服务 [英] How to write a long running activity to call web services in WF 4.0

查看:13
本文介绍了如何在 WF 4.0 中编写长时间运行的活动来调用 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个活动,它执行网络请求并将结果存储到数据库中.我发现对于这些长时间运行的活动,我应该编写一些不同的代码,这样工作流引擎线程就不会被阻塞.

I created an activity which executes a web request and stores the result into the database. I found out that for these long running activities I should write some different code so that the workflow engine thread won't be blocked.

public sealed class WebSaveActivity : NativeActivity
{
    protected override void Execute(NativeActivityContext context)
    {
       GetAndSave(); // This takes 1 hour to accomplish.
    }
}

我应该如何重写此活动以满足长时间运行的活动的要求

How should I rewrite this activity to meet the requirements for a long running activity

推荐答案

您可以使用例如在现有进程中生成一个线程ThreadPool.QueueUserWorkItem() 所以如果需要,您的工作流程的其余部分将继续运行.不过,请务必先了解多线程和线程同步的含义.或者,您可以查看 Hangfire 或类似组件,将整个工作转移到不同的进程中.

You could either spawn a thread within your existing process using e.g. ThreadPool.QueueUserWorkItem() so the rest of your workflow will continue to run if that is desired. Be sure to understand first what multithreading and thread synchronization means, though. Or you could look into Hangfire or similar components to offload the entire job into a different process.

根据您的评论,您可以查看基于任务的异步模式 (TAP):链接 1链接 2 这将为您提供一个很好的代码编写模型,该模型继续处理可以完成的事情,同时等待您长时间运行的操作的结果,直到它返回.但是,我不确定这是否涵盖了您的所有需求.特别是在 Windows Workflow Foundation 中,您可能需要查看某种形式的 工作流休眠/持久化.

Based on your comment you could look into Task-based Asynchronous Pattern (TAP): Link 1, Link 2 which would give you a nice model of writing code that continues to work on things that can be done while waiting for the result of your long running action until it returns. I am, however, not certain if this covers your all needs. In Windows Workflow Foundation specifically, you might want to look into some form of workflow hibernation/persistence.

这篇关于如何在 WF 4.0 中编写长时间运行的活动来调用 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆