zoukankan      html  css  js  c++  java
  • A simple test of WF Messaging Activity

    firstly create a WF service which act like normal WCF service to be our service for testing.

    1. create a wf console project.

    2. add a class which  is responsible for building a workflow . here is the code.


    class ReceiveAndReplyWorkflow
        {
            public WorkflowService GetInstance()
            {
                WorkflowService service;
                Variable<int> x = new Variable<int> { Name = "x" };
                Variable<int> y = new Variable<int> { Name = "y" };
                Variable<int> addResult =
                new Variable<int> { Name = "addResult" };
                Receive receive = new Receive
                {
                    ServiceContractName = "ICalculateService",//Wcf合约名
                    OperationName = "GetData",//合约中的方法
                    CanCreateInstance = true,//必需的,收到消息后创建一个WF实例
                    Content = new ReceiveParametersContent
                    {
                        Parameters ={
                            {"xIn",new OutArgument<int>(x)},//接收消息中的"xIn"和"yIn"关键字的值,并赋值给x和y变量
                            {"yIn",new OutArgument<int>(y)}
                        }
                    }
                };
                Sequence workflow = new Sequence()
                {
                    Variables = { x, y, addResult },
                    Activities = {
                        new WriteLine{Text="WF service is starting..."},
                        receive,
                        new WriteLine{Text="receive request with two numbers"},
                        new WriteLine{
                            Text=new InArgument<string>(aec=>"x="+x.Get(aec).ToString()+" y="+y.Get(aec))
                        },
                        new Assign<int>{
                            Value=new InArgument<int>(aec=>x.Get(aec)+y.Get(aec)),
                            To=new OutArgument<int>(addResult)
                        },
                        new WriteLine{
                            Text=new InArgument<string>(aec=>"addResult="+addResult.Get(aec).ToString())
                        },
                        new WriteLine{Text="Then send the result back to client"},
                        new SendReply{
                            Request=receive,
                            Content=new SendParametersContent{
                                Parameters=
                                {
                                    {"addResult",new InArgument<int>(addResult)}//将addResult变量的值赋给返回消息中的元素“addResult”
                                    
    //个人认为这里到底是InArgument还是OutArgument。要看这里是输入还是输出。如果是输出到这个参数。那么就是Out,反之就是In.
                                },
                            },
                        },
                        new WriteLine{Text="sent result back done"}
                    },
                };
                service = new WorkflowService
                {
                    Name = "AddService",
                    Body = workflow
                };
                return service;
            }

        } 

    3.  in the main method code .

    class Program
        {
            static void Main(string[] args)
            {
                ReceiveAndReplyWorkflow rrw =new ReceiveAndReplyWorkflow();
                WorkflowService wfService = rrw.GetInstance();
                Uri address =
                new Uri("http://localhost:8000/WFServices");
                WorkflowServiceHost host =
                new WorkflowServiceHost(wfService, address);
                
                try
                {
                    Console.WriteLine("Opening Service...");
                    host.Open();
                    Console.WriteLine
                    ("WF service is listening on " + address.ToString());
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine
                    ("some thing bad happened" + e.StackTrace);
                }
                finally
                {
                    host.Close();
                }
            }

        }

    4. and the configuration in app.config.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="True"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

    </configuration> 

    5. until now . the server endpoint is ready . ctrl +f5 make this service work.

     

    6. you can use the wcf test client tool to verify the service working.

     

    as we can see. this wcf  has two input parameter . one return result. we can check it out in the "xml" tab.

     

    above figure show the send and response message which contact with the service we build . 

    next . we will build a client which in charge of calling the service just like the work that wcf test client done.

    1. we create a wf console project being the client.

    2.  add a class which  is responsible for building a workflow . here is the code.

    class SendAndReceiveWorkflow
        {
            public Activity GetInstance()
            {
                Variable<int> x = new Variable<int>("x"20);
                Variable<int> y = new Variable<int>("y"10);

                Variable<int> addResult = new Variable<int>("addResult"0);
                var endpoint = new System.ServiceModel.Endpoint
                {
                    AddressUri = new Uri("http://localhost:8000/WFServices"),//必须指定,要请求的WCF endpoint  
                    Binding = new BasicHttpBinding(),
                };
                Send addRequest = new Send
                {
                    ServiceContractName = "ICalculateService",
                    Endpoint = endpoint,
                    OperationName = "GetData",
                    Content = new SendParametersContent
                    {
                        Parameters = {
                        {"xIn",new InArgument<int>(x)},
                        {"yIn",new InArgument<int>(y)}
                        }
                    },
                };
                var workflow = new CorrelationScope
                {
                    Body = new Sequence
                    {
                        Variables = { x, y, addResult },
                        Activities ={
                            new WriteLine{Text="Send x:20 and y:10 to WF service"},
                            addRequest,
                            new ReceiveReply{
                                Request=addRequest,//必须指定,在sendandreceivereplay activity 中会自动指定 如分别建立,send activity 和reveive activity时则要手动指定那个request.
                                Content=new ReceiveParametersContent{
                                    Parameters={
                                    {"addResult",new OutArgument<int>(addResult)}//第一个参数代表消息中的一个关键字。第二参数表示将前一个关键字中的值赋值给后一个变量,并生成一个出参。
                                    }
                                },
                            },
                            new WriteLine{
                                Text=new InArgument<string>(aec=>("The result is:"+addResult.Get(aec).ToString()))
                            },
                            new Delay
                            {
                                Duration= new TimeSpan(0,0,5)
                            }
                        }
                    }
                };
                return workflow;
            }

        }

    3. the client main method code just is simply like this.

    class Program
        {
            static void Main(string[] args)
            {
                SendAndReceiveWorkflow srw =new SendAndReceiveWorkflow();
                WorkflowInvoker.Invoke(srw.GetInstance());
            }

        } 

    well . this is a beginning guild for building and consuming the WF service . in the real business .i believe it is much more complicated than this.

    any comment are welcome . i will be appreciate for your attention. thanks


    you can down load the project from here . testSendReceive.rar

  • 相关阅读:
    flex布局以及相关属性
    css 选择器
    两侧定宽,中栏自适应布局
    两列定宽,一列自适应布局
    左列定宽,右列自适应布局
    Flex接收51单片机发送过来的16进制数据转换为String
    Flex与51单片机socket通信 策略问题
    sql For XML Path
    sql多对多探讨
    JavaScript
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2552762.html
Copyright © 2011-2022 走看看