在说wf HadleExternalEventActivity

外部触发的事件和工作流在出发之后使用调用的函数是两码事。可以这样认为所谓的接口只是提供了一种的对应关系。
 1)首先是接口提供了能干什么?
 2)接口的实现类来直接调调用接口中公开的方法,其实这里调用的就是在工作流中invoke中的方法。
所以将所谓的接口只是提供了一种的对应的方法。

例如下面的代码:

Test.cs

复制代码
namespace HandleExternalEventActivityTest
{
    
// Args
    [Serializable]
    
public class ArgsTest : ExternalDataEventArgs
    {
        
public ArgsTest(Guid id) : base(id) { }
    }


    [Serializable]
    
public class TestImpl : ITest
    {
        
public event EventHandler<ArgsTest> Agree;

        
public void OnAgree (ArgsTest arg)
        {
            
if (Agree != null)
            {
                Agree(
null, arg);
            }
        }
    }
}
复制代码

ITest.cs

复制代码
namespace HandleExternalEventActivityTest
{
    [ExternalDataExchange]
    
public interface ITest
    {
        
event EventHandler<ArgsTest> Agree;
    }
}
复制代码

Program.cs

复制代码
namespace HandleExternalEventActivityTest
{
    
class Program
    {
        
static void Main(string[] args)        {
            
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                ExternalDataExchangeService dataExchangeService 
= new ExternalDataExchangeService();
                workflowRuntime.AddService(dataExchangeService);

                TestImpl service 
= new TestImpl();
                dataExchangeService.AddService(service);

                AutoResetEvent waitHandle 
= new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted 
+= delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};
                workflowRuntime.WorkflowTerminated 
+= delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };

                WorkflowInstance instance 
= workflowRuntime.CreateWorkflow(typeof(HandleExternalEventActivityTest.Workflow1));
                instance.Start();


                
//////////////////////////////////////////////////////////////////////////
                // 这里时测试
                Console.WriteLine("Press any key !");
                Console.Read();

                service.OnAgree(
new ArgsTest(instance.InstanceId));

                waitHandle.WaitOne();
            }
        }
    }
}
复制代码

按照上面的思路其实就是定义了一种对应关系(其实这也是c#中delegate和event关键字的作用)如下:

Agree(null, arg);

实际上就是调用的是:

// workflow1.cs         
private void OnAgree(object sender, ExternalDataEventArgs e)
        {
            Console.WriteLine(
"Agree !");
        }

 

这里只是为了理解使用‘对应’讲话理解,实际应该参加:

http://msdn.microsoft.com/en-us/library/8627sbea(v=VS.80).aspx

posted @   qiang.xu  阅读(214)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示