// 接收回调函数
        private void ReceiveCallback(IAsyncResult iar)
        {
            IPEndPoint   ip    = null;
            ReceiveState state = (ReceiveState)iar.AsyncState;

            byte[] receiveBytes = state.client.EndReceive(iar, ref ip);
            m_UdpClient.BeginReceive(new AsyncCallback(ReceiveCallback), state);

            if (iar.IsCompleted)
            {
                new Thread(() =>
                {
                    string receiveContent = Encoding.ASCII.GetString(receiveBytes);
                    Log($"Received: {receiveContent} [{ip.ToString()}]");

                    if (string.IsNullOrEmpty(receiveContent))
                    {
                        Log($"Received ERROR: null content [{ip.ToString()}]");
                        return;
                    }
                    else if (!receiveContent.Contains(c_Split.ToString()))
                    {
                        Log($"Received ERROR: can not find [{c_Split}] in [{receiveContent}] [{ip.ToString()}]");
                        return;
                    }

                    ReceiveContent content = new ReceiveContent(receiveContent, c_Split);

                    IReceiver receiver = state.receiversFactory.GetReceiver(content.receiverType);

                    if (receiver == null)
                    {
                        Log($"Received ERROR: type error! [{content.receiverType}] [{ip.ToString()}]");
                        return;
                    }

                    string receipt = receiver.Receive(ip, content.content);

                    if (receipt != null)
                    {
                        Send(ip, content.receiverType, receipt);
                    }
                }).Start();
            }
        }
Exemple #2
0
        static WorkflowService GetService()
        {
            Variable <string> incomingMessage = new Variable <string> {
                Name = "inmessage"
            };
            Variable <int> outgoingMessage = new Variable <int> {
                Name = "outmessage"
            };
            Receive receiveSecureData = new Receive
            {
                OperationName       = "AskQuestion",
                ServiceContractName = "ISecuredService",
                CanCreateInstance   = true,
                Content             = ReceiveContent.Create(new OutArgument <string>(incomingMessage))
            };
            Sequence SecuredWorkFlow = new Sequence()
            {
                Variables  = { incomingMessage, outgoingMessage },
                Activities =
                {
                    receiveSecureData,
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => ("Message received: " + incomingMessage.Get(env)))
                    },
                    new SendReply
                    {
                        Request = receiveSecureData,
                        Content = SendContent.Create(new InArgument <int>(4))
                    }
                }
            };

            WorkflowService service = new WorkflowService
            {
                Name = "SecuredService",
                Body = SecuredWorkFlow,
                ConfigurationName = "SecuredService"
            };

            return(service);
        }
Exemple #3
0
        static WorkflowService GetService()
        {
            Variable <Customer>          customer     = new Variable <Customer>();
            Variable <Order>             order        = new Variable <Order>();
            Variable <string>            drug         = new Variable <string>();
            Variable <double>            adjustedCost = new Variable <double>();
            Variable <int>               percentagePaidByInsurance = new Variable <int>();
            Variable <CorrelationHandle> customerHandle            = new Variable <CorrelationHandle>();
            Variable <CorrelationHandle> orderHandle = new Variable <CorrelationHandle>();

            XPathMessageContext pathContext = new XPathMessageContext();

            pathContext.AddNamespace("psns", Constants.PharmacyServiceNamespace);

            // <Snippet2>
            MessageQuerySet GetOrderQuerySet = new MessageQuerySet
            {
                {
                    "OrderID",
                    new XPathMessageQuery("//psns:Order/psns:OrderID", pathContext)
                }
            };
            // </Snippet2>
            MessageQuerySet GetOrderIDQuerySet = new MessageQuerySet
            {
                {
                    "OrderID",
                    new XPathMessageQuery("//ser:guid", pathContext)
                }
            };

            MessageQuerySet customerQuerySet = new MessageQuerySet
            {
                {
                    "CustomerID",
                    new XPathMessageQuery("//psns:GetBaseCost/psns:Customer/psns:CustomerID", pathContext)
                }
            };

            MessageQuerySet customerIDQuerySet = new MessageQuerySet
            {
                {
                    "CustomerID",
                    new XPathMessageQuery("//ser:guid", pathContext)
                }
            };

            // This will use implicit correlation within the workflow using the WorkflowServiceHost's default CorrelationHandle
            // <Snippet3>
            Receive prescriptionRequest = new Receive
            {
                DisplayName         = "Request Perscription",
                OperationName       = "GetBaseCost",
                ServiceContractName = Constants.PharmacyServiceContractName,
                CanCreateInstance   = true,
                //CorrelatesWith = customerHandle,  -- add this line for explicit correlation
                CorrelatesOn = customerQuerySet,
                Content      = new ReceiveParametersContent
                {
                    Parameters =
                    {
                        { "Customer", new OutArgument <Customer>(customer) },
                        { "Drug",     new OutArgument <string>(drug)       },
                    }
                }
            };
            // </Snippet3>

            // This will use implicit correlation within the workflow using the WorkflowServiceHost's default CorrelationHandle
            Receive GetInsurancePaymentPercentageRequest = new Receive
            {
                DisplayName         = "Get Insurance Coverage",
                ServiceContractName = Constants.PharmacyServiceContractName,
                OperationName       = "GetInsurancePaymentPercentage",
                CanCreateInstance   = true,
                //CorrelatesWith = customerHandle,  -- add this line for explicit correlation
                CorrelatesOn = customerIDQuerySet,
                Content      = ReceiveContent.Create(new OutArgument <Guid>())
            };

            // This will explicitly correlate with the SendReply action after the prescriptionRequest using the OrderID (stored in the orderHandle)
            Receive GetAdjustedCostRequest = new Receive
            {
                DisplayName         = "Get Adjusted Cost",
                OperationName       = "GetAdjustedCost",
                ServiceContractName = Constants.PharmacyServiceContractName,
                CanCreateInstance   = true,
                CorrelatesOn        = GetOrderIDQuerySet,
                CorrelatesWith      = orderHandle,
                Content             = ReceiveContent.Create(new OutArgument <Guid>())
            };

            Activity PrescriptonWorkflow = new Sequence()
            {
                Variables  = { customer, order, drug, percentagePaidByInsurance, adjustedCost, customerHandle, orderHandle },
                Activities =
                {
                    new WriteLine
                    {
                        Text = "Beginning Workflow"
                    },

                    new Parallel
                    {
                        Branches =
                        {
                            new Sequence
                            {
                                Activities =
                                {
                                    GetInsurancePaymentPercentageRequest,
                                    new Assign <int>
                                    {
                                        To    = new OutArgument <int>((e) => percentagePaidByInsurance.Get(e)),
                                        Value = new InArgument <int>((e) => new Random().Next(0, 100))
                                    },
                                    new SendReply
                                    {
                                        DisplayName = "Return Percentage",
                                        Request     = GetInsurancePaymentPercentageRequest,
                                        Content     = SendContent.Create(new InArgument <int>((e) => percentagePaidByInsurance.Get(e)))
                                    }
                                }
                            },

                            new Sequence
                            {
                                Activities =
                                {
                                    prescriptionRequest,
                                    new WriteLine
                                    {
                                        Text = new InArgument <string>(env => (string.Format("{0}, {1}\t{2}",customer.Get(env).LastName,                                                       customer.Get(env).FirstName, customer.Get(env).CustomerID.ToString())))
                                    },
                                    new Assign <Order>
                                    {
                                        To    = new OutArgument <Order>(order),
                                        Value = new InArgument <Order>((e) => new Order()
                                        {
                                            CustomerID = customer.Get(e).CustomerID,Drug = drug.Get(e),                                                               OrderID = Guid.NewGuid()
                                        })
                                    },
                                    new WriteLine
                                    {
                                        Text = new InArgument <string>(env => (string.Format("OrderID: {0}",order.Get(env).OrderID.ToString())))
                                    },
                                    new Assign <int>
                                    {
                                        To    = new OutArgument <int>((e) => order.Get(e).Cost),
                                        Value = new InArgument <int>((e) => new Random().Next(20,                   50))
                                    },
                                    // <Snippet0>
                                    new SendReply
                                    {
                                        DisplayName = "Send Adjusted Cost",
                                        Request     = prescriptionRequest,
                                        // Initialize the orderHandle using the MessageQuerySet to correlate with the final GetAdjustedCost request
                                        CorrelationInitializers =
                                        {
                                            // <Snippet1>
                                            new QueryCorrelationInitializer
                                            {
                                                CorrelationHandle = orderHandle,
                                                MessageQuerySet   = GetOrderQuerySet
                                            }
                                            // </Snippet1>
                                        },
                                        Content = SendContent.Create(new InArgument <Order>((e) => order.Get(e)))
                                    }
                                    // </Snippet0>
                                }
                            }
                        }
                    },

                    new Assign <double>
                    {
                        To    = new OutArgument <double>((e) => adjustedCost.Get(e)),
                        Value = new InArgument <double>((e) => order.Get(e).Cost *(100 - percentagePaidByInsurance.Get(e)) * .01)
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => (string.Format("Base Cost: ${0}", order.Get(env).Cost.ToString())))
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => (string.Format("Insurance Coverage: {0}%", percentagePaidByInsurance.Get(env).ToString())))
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => (string.Format("Adjusted Cost: ${0}", decimal.Round(Convert.ToDecimal(adjustedCost.Get(env)), 2))))
                    },
                    GetAdjustedCostRequest,
                    new SendReply
                    {
                        Request = GetAdjustedCostRequest,
                        Content = SendContent.Create(new InArgument <double>((e) => adjustedCost.Get(e)))
                    },
                    new WriteLine
                    {
                        Text = "Workflow Completed"
                    }
                }
            };

            WorkflowService service = new WorkflowService
            {
                Name = "PharmacyService",
                Body = PrescriptonWorkflow,
                ConfigurationName = "PharmacyService"
            };

            return(service);
        }
Exemple #4
0
        static Activity GetServiceWorkflow()
        {
            Variable <PurchaseOrder>     po           = new Variable <PurchaseOrder>();
            Variable <Customer>          customer     = new Variable <Customer>();
            Variable <CorrelationHandle> poidHandle   = new Variable <CorrelationHandle>();
            Variable <CorrelationHandle> custidHandle = new Variable <CorrelationHandle>();
            Variable <bool> complete = new Variable <bool>()
            {
                Default = false
            };

            Receive submitPO = new Receive
            {
                CanCreateInstance   = true,
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                Content             = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po)) // creates a ReceiveMessageContent
            };

            return(new Sequence
            {
                Variables = { po, customer, poidHandle, custidHandle, complete },
                Activities =
                {
                    submitPO,
                    new WriteLine {
                        Text = "Received PurchaseOrder"
                    },
                    new Assign <int>
                    {
                        To = new OutArgument <int>((e) => po.Get(e).Id),
                        Value = new InArgument <int>((e) => new Random().Next())
                    },
                    // <Snippet2>
                    new SendReply
                    {
                        Request = submitPO,
                        Content = SendContent.Create(new InArgument <int>((e) => po.Get(e).Id)),// creates a SendMessageContent
                        CorrelationInitializers =
                        {
                            new QueryCorrelationInitializer
                            {
                                // initializes a correlation based on the PurchaseOrder Id sent in the reply message and stores it in the handle
                                CorrelationHandle = poidHandle,
                                MessageQuerySet = new MessageQuerySet
                                {
                                    // int is the name of the parameter being sent in the outgoing response
                                    { "PoId", new XPathMessageQuery("sm:body()/ser:int",Constants.XPathMessageContext)                               }
                                }
                            }
                        }
                    },
                    // </Snippet2>
                    new Parallel
                    {
                        CompletionCondition = complete,
                        Branches =
                        {
                            new While
                            {
                                Condition = true,
                                Body = new Receive
                                {
                                    ServiceContractName = Constants.POContractName,
                                    OperationName = Constants.UpdatePOName,
                                    CorrelatesWith = poidHandle,       // identifies that the UpdatePO operation is waiting on the PurchaseOrderId that was used to initialize this handle
                                    CorrelatesOn = new MessageQuerySet // the query that is used on an incoming message to find the requisite PurchaseOrderId specified in the correlation
                                    {
                                        // Id is the name of the incoming parameter within the PurchaseOrder
                                        { "PoId",   new XPathMessageQuery("sm:body()/defns:PurchaseOrder/defns:Id",Constants.XPathMessageContext) }
                                    },
                                    Content = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po)) // creates a ReceiveMessageContent
                                }
                            },
                            new Sequence
                            {
                                Activities =
                                {
                                    new Receive
                                    {
                                        ServiceContractName = Constants.POContractName,
                                        OperationName = Constants.AddCustomerInfoName,
                                        Content = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po)), // creates a ReceiveMessageContent
                                        CorrelatesWith = poidHandle,                                          // identifies that the AddCustomerInfo operation is waiting on the PurchaseOrderId that was used to initialize this handle
                                        CorrelatesOn = new MessageQuerySet                                    // the query that is used on an incoming message to find the requisite PurchaseOrderId specified in the correlation
                                        {
                                            // Id is the name of the incoming parameter within the PurchaseOrder
                                            { "PoId",   new XPathMessageQuery("sm:body()/defns:PurchaseOrder/defns:Id",         Constants.XPathMessageContext)}
                                        },
                                        CorrelationInitializers =
                                        {
                                            new QueryCorrelationInitializer
                                            {
                                                // initializes a new correlation based on the CustomerId parameter in the message and stores it in the handle
                                                CorrelationHandle = custidHandle,
                                                MessageQuerySet = new MessageQuerySet
                                                {
                                                    // CustomerId is the name of the incoming parameter within the PurchaseOrder
                                                    { "CustId", new XPathMessageQuery("sm:body()/defns:PurchaseOrder/defns:CustomerId", Constants.XPathMessageContext)}
                                                }
                                            }
                                        }
                                    },
                                    new WriteLine   {
                                        Text = "Got CustomerId"
                                    },
                                    new Receive
                                    {
                                        ServiceContractName = Constants.POContractName,
                                        OperationName = Constants.UpdateCustomerName,
                                        Content = ReceiveContent.Create(new OutArgument <Customer>(customer)), // creates a ReceiveMessageContent
                                        CorrelatesWith = custidHandle,                                         // identifies that the UpdateCustomerName operation is waiting on the CustomerId that was used to initialize this handle
                                        CorrelatesOn = new MessageQuerySet                                     // the query that is used on an incoming message to find the requisite CustomerId specified in the correlation
                                        {
                                            // Id is the name of the incoming parameter within the Customer type
                                            { "CustId", new XPathMessageQuery("sm:body()/defns:Customer/defns:Id",              Constants.XPathMessageContext)}
                                        }
                                    },
                                    new Assign <bool>
                                    {
                                        To = new OutArgument <bool>(complete),
                                        Value = new InArgument <bool>(true)
                                    }
                                }
                            }
                        }
                    },
                    new WriteLine {
                        Text = new InArgument <string>((e) => string.Format("Workflow completed for PurchaseOrder {0}: {1} {2}s",po.Get(e).Id,                                                            po.Get(e).Quantity, po.Get(e).PartName))
                    },
                    new WriteLine {
                        Text = new InArgument <string>((e) => string.Format("Order will be shipped to {0} as soon as possible",customer.Get(e).Name))
                    }
                }
            });
        }
Exemple #5
0
        static Activity GetClientWorkflow()
        {
            // <Snippet0>
            Variable <PurchaseOrder> po       = new Variable <PurchaseOrder>();
            Variable <Customer>      customer = new Variable <Customer>();

            Endpoint clientEndpoint = new Endpoint
            {
                Binding    = Constants.Binding,
                AddressUri = new Uri(Constants.ServiceAddress)
            };

            Send submitPO = new Send
            {
                Endpoint            = clientEndpoint,
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                Content             = new SendMessageContent(new InArgument <PurchaseOrder>(po))
            };

            // </Snippet0>
            // <Snippet3>
            return(new Sequence
            {
                Variables = { po, customer },
                Activities =
                {
                    new Assign <PurchaseOrder>
                    {
                        To = po,
                        Value = new InArgument <PurchaseOrder>((e) => new PurchaseOrder()
                        {
                            PartName = "Widget", Quantity = 150
                        })
                    },
                    new Assign <Customer>
                    {
                        To = customer,
                        Value = new InArgument <Customer>((e) => new Customer()
                        {
                            Id = 12345678, Name = "John Smith"
                        })
                    },
                    new WriteLine    {
                        Text = new InArgument <string>((e) => string.Format("Submitting new PurchaseOrder for {0} {1}s", po.Get(e).Quantity, po.Get(e).PartName))
                    },
                    new CorrelationScope
                    {
                        Body = new Sequence
                        {
                            Activities =
                            {
                                submitPO,
                                new ReceiveReply
                                {
                                    Request = submitPO,
                                    Content = ReceiveContent.Create(new OutArgument <int>((e) => po.Get(e).Id))
                                }
                            }
                        }
                    },
                    new WriteLine    {
                        Text = new InArgument <string>((e) => string.Format("Received ID for new PO: {0}", po.Get(e).Id))
                    },
                    new Assign <int> {
                        To = new OutArgument <int>((e) => po.Get(e).Quantity), Value = 250
                    },
                    new WriteLine    {
                        Text = "Updated PO with new quantity: 250.  Resubmitting updated PurchaseOrder based on POId."
                    },
                    // <Snippet1>
                    new Send
                    {
                        Endpoint = clientEndpoint,
                        ServiceContractName = Constants.POContractName,
                        OperationName = Constants.UpdatePOName,
                        Content = SendContent.Create(new InArgument <PurchaseOrder>(po))
                    },
                    // </Snippet1>
                    new Assign <int>
                    {
                        To = new OutArgument <int>((e) => po.Get(e).CustomerId),
                        Value = new InArgument <int>((e) => customer.Get(e).Id)
                    },
                    new WriteLine    {
                        Text = "Updating customer data based on CustomerId."
                    },
                    new Send
                    {
                        Endpoint = clientEndpoint,
                        ServiceContractName = Constants.POContractName,
                        OperationName = Constants.AddCustomerInfoName,
                        Content = SendContent.Create(new InArgument <PurchaseOrder>(po))
                    },
                    new Send
                    {
                        Endpoint = clientEndpoint,
                        ServiceContractName = Constants.POContractName,
                        OperationName = Constants.UpdateCustomerName,
                        Content = SendContent.Create(new InArgument <Customer>(customer))
                    },
                    new WriteLine    {
                        Text = "Client completed."
                    }
                }
            });
            // </Snippet3>
        }
Exemple #6
0
        static Activity GetClientWorkflow()
        {
            Variable <PurchaseOrder> po          = new Variable <PurchaseOrder>();
            Variable <OrderStatus>   orderStatus = new Variable <OrderStatus>();

            Endpoint clientEndpoint = new Endpoint
            {
                Binding    = Constants.Binding,
                AddressUri = new Uri(Constants.ServiceAddress)
            };

            Send submitPO = new Send
            {
                Endpoint            = clientEndpoint,
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                Content             = SendContent.Create(new InArgument <PurchaseOrder>(po))
            };

            return(new Sequence
            {
                Variables = { po, orderStatus },
                Activities =
                {
                    new WriteLine              {
                        Text = "Sending order for 150 widgets."
                    },
                    new Assign <PurchaseOrder> {
                        To = po, Value = new InArgument <PurchaseOrder>((e) => new PurchaseOrder()
                        {
                            PartName = "Widget", Quantity = 150
                        })
                    },
                    new CorrelationScope
                    {
                        Body = new Sequence
                        {
                            Activities =
                            {
                                submitPO,
                                new ReceiveReply
                                {
                                    Request = submitPO,
                                    Content = ReceiveContent.Create(new OutArgument <int>((e) => po.Get(e).Id))
                                }
                            }
                        }
                    },
                    new WriteLine              {
                        Text = new InArgument <string>((e) => string.Format("Got PoId: {0}", po.Get(e).Id))
                    },
                    new Assign <OrderStatus>   {
                        To = orderStatus, Value = new InArgument <OrderStatus>((e) => new OrderStatus()
                        {
                            Id = po.Get(e).Id, Confirmed = true
                        })
                    },
                    new Send
                    {
                        Endpoint = clientEndpoint,
                        ServiceContractName = Constants.POContractName,
                        OperationName = Constants.ConfirmPurchaseOrder,
                        Content = SendContent.Create(new InArgument <OrderStatus>(orderStatus))
                    },
                    new WriteLine              {
                        Text = "The order was confirmed."
                    },
                    new WriteLine              {
                        Text = "Client completed."
                    }
                }
            });
        }
Exemple #7
0
        static Sequence VendorApprovalRequest(Endpoint endpoint)
        {
            Variable <VendorRequest> vendor = new Variable <VendorRequest>
            {
                Name = "vendor",
            };
            Variable <VendorResponse> result = new Variable <VendorResponse>
            {
                Name = "result"
            };

            Send approvedVendor = new Send
            {
                ServiceContractName = XName.Get("FinanceService", "http://tempuri.org/"),
                Endpoint            = endpoint,
                OperationName       = "ApprovedVendor",
                Content             = SendContent.Create(new InArgument <VendorRequest>(vendor)),
            };

            Sequence workflow = new Sequence
            {
                Variables  = { vendor, result },
                Activities =
                {
                    new Assign <VendorRequest>
                    {
                        Value = new InArgument <VendorRequest>((e) => new VendorRequest{
                            Name = "Vendor1", requestingDepartment = "HR"
                        }),
                        To = new OutArgument <VendorRequest>(vendor)
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>("Hello")
                    },
                    approvedVendor,
                    new ReceiveReply
                    {
                        Request = approvedVendor,
                        Content = ReceiveContent.Create(new OutArgument <VendorResponse>(result))
                    },

                    new If
                    {
                        Condition = new InArgument <bool> (env => result.Get(env).isPreApproved),
                        Then      =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Vendor Approved")
                        },
                        Else =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Vendor is not Approved")
                        },
                    },
                }
            };

            return(workflow);
        }
        private Activity InternalImplementation()
        {
            Variable <string> requestMessage = new Variable <string> {
                Name = "requestString"
            };
            Variable <string> replyMessage = new Variable <string> {
                Name = "replyString"
            };

            Receive receive = new Receive
            {
                OperationName       = "StartSample",
                CanCreateInstance   = true,
                Content             = ReceiveContent.Create(new OutArgument <string>(requestMessage)),
                ServiceContractName = "ITransactedReceiveService",
            };

            return(new Sequence
            {
                Activities =
                {
                    new WriteLine             {
                        Text = "Service workflow begins."
                    },

                    new System.ServiceModel.Activities.TransactedReceiveScope
                    {
                        Variables =           { requestMessage,   replyMessage },
                        Request = receive,
                        Body = new Sequence
                        {
                            Activities =
                            {
                                new WriteLine {
                                    Text = new InArgument <string>("Server side: Receive complete.")
                                },

                                new WriteLine {
                                    Text = new InArgument <string>(new VisualBasicValue <string>()
                                    {
                                        ExpressionText = "\"Server side: Received = '\" + requestString.toString() + \"'\""
                                    })
                                },

                                new PrintTransactionInfo(),

                                new Assign <string>
                                {
                                    Value = new InArgument <string>("Server side: Sending reply."),
                                    To = new OutArgument <string>(replyMessage)
                                },

                                new WriteLine {
                                    Text = new InArgument <string>("Server side: Begin reply.")
                                },

                                new SendReply
                                {
                                    Request = receive,
                                    Content = SendContent.Create(new InArgument <string>(replyMessage)),
                                },

                                new WriteLine {
                                    Text = new InArgument <string>("Server side: Reply sent.")
                                },
                            },
                        },
                    },

                    new WriteLine             {
                        Text = "Server workflow ends."
                    },
                },
            });
        }
Exemple #9
0
        private static Activity GetApproveExpense(Variable <Expense> expense, Variable <bool> reply)
        {
            Receive approveExpense = new Receive
            {
                OperationName       = "ApproveExpense",
                CanCreateInstance   = true,
                ServiceContractName = "FinanceService",
                SerializerOption    = SerializerOption.DataContractSerializer,
                Content             = ReceiveContent.Create(new OutArgument <Expense>(expense))
            };

            approveExpense.KnownTypes.Add(typeof(Travel));
            approveExpense.KnownTypes.Add(typeof(Meal));

            Activity workflow = new CorrelationScope()
            {
                Body = new Sequence
                {
                    Variables  = { expense, reply },
                    Activities =
                    {
                        approveExpense,
                        new WriteLine
                        {
                            Text = new InArgument <string>(env => ("Expense approval request received"))
                        },
                        new If
                        {
                            Condition = new InArgument <bool> (env => (expense.Get(env).Amount <= 100)),
                            Then      =
                                new Assign <bool>
                            {
                                Value = true,
                                To    = new OutArgument <bool>(reply)
                            },
                            Else =
                                new Assign <bool>
                            {
                                Value = false,
                                To    = new OutArgument <bool>(reply)
                            },
                        },

                        new If
                        {
                            Condition = new InArgument <bool> (reply),
                            Then      =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Expense Approved")
                            },
                            Else =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Expense Cannot be Approved")
                            },
                        },
                        new SendReply
                        {
                            Request = approveExpense,
                            Content = SendContent.Create(new InArgument <bool>(reply)),
                        },
                    },
                }
            };

            return(workflow);
        }
Exemple #10
0
        private static Activity GetApprovedVendor(Variable <VendorRequest> vendor, Variable <VendorResponse> replyVendor)
        {
            Receive approvedVendor = new Receive
            {
                OperationName       = "ApprovedVendor",
                CanCreateInstance   = true,
                ServiceContractName = "FinanceService",
                Content             = ReceiveContent.Create(new OutArgument <VendorRequest>(vendor))
            };

            Activity workflow = new CorrelationScope()
            {
                Body = new Sequence
                {
                    Variables  = { vendor, replyVendor },
                    Activities =
                    {
                        approvedVendor,
                        new WriteLine
                        {
                            Text = new InArgument <string>(env => ("Query for approved vendor received"))
                        },
                        new If
                        {
                            Condition = new InArgument <bool> (env => ((vendor.Get(env).requestingDepartment == "Finance")) || Constants.vendors.Contains(vendor.Get(env).Name)),
                            Then      =
                                new Assign <VendorResponse>
                            {
                                Value = new InArgument <VendorResponse>((e) => new VendorResponse {
                                    isPreApproved = true
                                }),
                                To = new OutArgument <VendorResponse>(replyVendor)
                            },
                            Else =
                                new Assign <VendorResponse>
                            {
                                Value = new InArgument <VendorResponse>((e) => new VendorResponse {
                                    isPreApproved = false
                                }),
                                To = new OutArgument <VendorResponse>(replyVendor)
                            },
                        },

                        new If
                        {
                            Condition = new InArgument <bool> (env => replyVendor.Get(env).isPreApproved),
                            Then      =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Vendor is pre-approved")
                            },
                            Else =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Vendor is not pre-approved")
                            },
                        },
                        new SendReply
                        {
                            Request = approvedVendor,
                            Content = SendContent.Create(new InArgument <VendorResponse>(replyVendor)),
                        },
                    }
                }
            };

            return(workflow);
        }
        static Activity GetClientWorkflow()
        {
            Variable <PurchaseOrder> po           = new Variable <PurchaseOrder>();
            Variable <bool>          invalidorder = new Variable <bool>()
            {
                Default = true
            };
            Variable <string> replytext = new Variable <string>();
            DelegateInArgument <FaultException <POFault> >         poFault         = new DelegateInArgument <FaultException <POFault> >();
            DelegateInArgument <FaultException <ExceptionDetail> > unexpectedFault = new DelegateInArgument <FaultException <ExceptionDetail> >();

            // submits a purchase order for the part and quantity stored in the po variable
            Send submitPO = new Send
            {
                Endpoint = new Endpoint
                {
                    Binding    = Constants.Binding,
                    AddressUri = new Uri(Constants.ServiceAddress)
                },
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                KnownTypes          = { typeof(POFault) },
                Content             = SendContent.Create(new InArgument <PurchaseOrder>(po))
            };

            return(new CorrelationScope
            {
                Body = new Sequence
                {
                    Variables = { invalidorder, po },
                    Activities =
                    {
                        // defines the original desired parts and quantity: 155 pencils
                        new Assign <PurchaseOrder>
                        {
                            To = po,
                            Value = new InArgument <PurchaseOrder>((e) => new PurchaseOrder()
                            {
                                PartName = "Pencil", Quantity = 155
                            })
                        },
                        new While
                        {
                            // loop until a valid order is submitted
                            Condition = invalidorder,
                            Variables =                                   { replytext             },
                            Body = new Sequence
                            {
                                Activities =
                                {
                                    // print out the order that will be submitted
                                    new WriteLine                         {
                                        Text = new InArgument <string>((env) => string.Format("Submitting Order: {0} {1}s", po.Get(env).Quantity, po.Get(env).PartName))
                                    },
                                    // submit the order
                                    submitPO,
                                    new TryCatch
                                    {
                                        Try = new Sequence
                                        {
                                            Activities =
                                            {
                                                // receive the result of the order
                                                // if ReceiveReply gets a Fault message, then we will handle those faults below
                                                new ReceiveReply
                                                {
                                                    Request = submitPO,
                                                    Content = ReceiveContent.Create(new OutArgument <string>(replytext))
                                                },
                                                new WriteLine             {
                                                    Text = replytext
                                                },
                                                // this order must be valid, so set invalidorder to false
                                                new Assign <bool>
                                                {
                                                    To = invalidorder,
                                                    Value = false
                                                }
                                            }
                                        },
                                        Catches =
                                        {
                                            // catch a known Fault type: POFault
                                            new Catch <FaultException <POFault> >
                                            {
                                                Action = new ActivityAction <FaultException <POFault> >
                                                {
                                                    Argument = poFault,
                                                    Handler = new Sequence
                                                    {
                                                        Activities =
                                                        {
                                                            // print out the details of the POFault
                                                            new WriteLine {
                                                                Text = new InArgument <string>((env) => string.Format("\tReceived POFault: {0}", poFault.Get(env).Reason.ToString()))
                                                            },
                                                            new WriteLine {
                                                                Text = new InArgument <string>((env) => string.Format("\tPOFault Problem: {0}", poFault.Get(env).Detail.Problem))
                                                            },
                                                            new WriteLine {
                                                                Text = new InArgument <string>((env) => string.Format("\tPOFault Solution: {0}", poFault.Get(env).Detail.Solution))
                                                            },
                                                            // update the order to buy Widgets instead
                                                            new Assign <string>
                                                            {
                                                                To = new OutArgument <string>((e) => po.Get(e).PartName),
                                                                Value = "Widget"
                                                            }
                                                        }
                                                    }
                                                }
                                            },
                                            // catch any unknown fault types
                                            new Catch <FaultException <ExceptionDetail> >
                                            {
                                                Action = new ActivityAction <FaultException <ExceptionDetail> >
                                                {
                                                    Argument = unexpectedFault,
                                                    Handler = new Sequence
                                                    {
                                                        Activities =
                                                        {
                                                            // print out the details of the fault
                                                            new WriteLine
                                                            {
                                                                Text = new InArgument <string>((e) => string.Format("\tReceived Fault: {0}",
                                                                                                                    unexpectedFault.Get(e).Message
                                                                                                                    ))
                                                            },
                                                            // update the order to buy 10 less of the item
                                                            new Assign <int>
                                                            {
                                                                To = new OutArgument <int>((e) => po.Get(e).Quantity),
                                                                Value = new InArgument <int>((e) => po.Get(e).Quantity - 10)
                                                            }
                                                        }
                                                    }
                                                }
                                            },
                                        }
                                    }
                                }
                            }
                        },
                        new WriteLine                                     {
                            Text = "Order successfully processed."
                        }
                    }
                }
            });
        }
Exemple #12
0
        static Activity GetServiceWorkflow()
        {
            Variable <PurchaseOrder>       po        = new Variable <PurchaseOrder>();
            DelegateInArgument <Exception> exception = new DelegateInArgument <Exception> {
                Name = "UnexpectedException"
            };

            // receive the order request
            Receive submitPO = new Receive
            {
                CanCreateInstance   = true,
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                Content             = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po))
            };

            return(new TryCatch
            {
                Variables = { po },
                Try = new Sequence
                {
                    Activities =
                    {
                        // receive the order request
                        submitPO,
//<Snippet1>
                        new If
                        {
                            // check if the order is asking for Widgets
                            Condition = new InArgument <bool>((e) => po.Get(e).PartName.Equals("Widget")),
                            Then = new If
                            {
                                // check if we have enough widgets in stock
                                Condition = new InArgument <bool>((e) => po.Get(e).Quantity < 100),
                                Then = new SendReply
                                {
                                    DisplayName = "Successful response",
                                    Request = submitPO,
                                    Content = SendContent.Create(new InArgument <string>((e) => string.Format("Success: {0} Widgets have been ordered!",po.Get(e).Quantity)))
                                },
                                // if we don't have enough widgets, throw an unhandled exception from this operation's body
                                Else = new Throw
                                {
                                    Exception = new InArgument <Exception>((e) => new Exception("We don't have that many Widgets."))
                                }
                            },
                            // if its not for widgets, reply to the client that we don't carry that part by sending back an expected fault type (POFault)
                            Else = new SendReply
                            {
                                DisplayName = "Expected fault",
                                Request = submitPO,
                                Content = SendContent.Create(new InArgument <FaultException <POFault> >((e) => new FaultException <POFault>(
                                                                                                            new POFault
                                {
                                    Problem = string.Format("This company does not carry {0}s, but we do carry Widgets.",po.Get(e).PartName),
                                    Solution = "Try your local hardware store."
                                },
                                                                                                            new FaultReason("This is an expected fault.")
                                                                                                            )))
                            }
                        }
//</Snippet1>
                    }
                },
                Catches =
                {
                    // catch any unhandled exceptions and send back a reply with that exception as a fault message
                    // note: handling this exception in the workflow will prevent the instance from terminating on unhandled exception,
                    //       however, if you don't catch the exception and the instance terminates while a reply is still pending,
                    //       the WorkflowServiceHost will send the reply automatically with the exception details included
                    new Catch <Exception>
                    {
                        Action = new ActivityAction <Exception>
                        {
                            Argument = exception,
                            Handler = new SendReply
                            {
                                DisplayName = "Unexpected fault",
                                Request = submitPO,
                                Content = SendContent.Create(new InArgument <Exception>(exception))
                            }
                        }
                    }
                }
            });
        }
Exemple #13
0
        public SendRequest()
        {
            // Define the variables used by this workflow
            Variable <ReservationRequest> request =
                new Variable <ReservationRequest> {
                Name = "request"
            };
            Variable <string> requestAddress =
                new Variable <string> {
                Name = "RequestAddress"
            };

            // Define the Send activity
            Send submitRequest = new Send
            {
                ServiceContractName = "ILibraryReservation",
                EndpointAddress     = new InArgument <Uri>(env => new Uri("http://localhost:" + requestAddress.Get(env) + "/LibraryReservation")),
                Endpoint            = new Endpoint
                {
                    Binding = new BasicHttpBinding()
                },
                OperationName = "RequestBook",
                Content       = SendContent.Create(new InArgument <ReservationRequest>(request))
            };

            // Define the SendRequest workflow
            this.Implementation = () => new Sequence
            {
                DisplayName = "SendRequest",
                Variables   = { request, requestAddress },
                Activities  =
                {
                    new CreateRequest
                    {
                        Title   = new InArgument <string>(env => Title.Get(env)),
                        Author  = new InArgument <string>(env => Author.Get(env)),
                        ISBN    = new InArgument <string>(env => ISBN.Get(env)),
                        Request = new OutArgument <ReservationRequest>
                                      (env => request.Get(env)),
                        RequestAddress = new OutArgument <string>
                                             (env => requestAddress.Get(env))
                    },
                    new CorrelationScope
                    {
                        Body = new Sequence
                        {
                            Activities =
                            {
                                submitRequest,
                                new WriteLine
                                {
                                    Text = new InArgument <string>
                                               (env => "Request sent; waiting for response"),
                                },
                                new ReceiveReply
                                {
                                    Request = submitRequest,
                                    Content = ReceiveContent.Create
                                                  (new OutArgument <ReservationResponse>
                                                      (env => Response.Get(env)))
                                }
                            }
                        }
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>
                                   (env => "Response received from " +
                                   Response.Get(env).Provider.BranchName),
                    },
                }
            };
        }
Exemple #14
0
        public ProcessRequest()
        {
            // Define the variables used by this workflow
            Variable <ReservationRequest> request =
                new Variable <ReservationRequest> {
                Name = "request"
            };
            Variable <ReservationResponse> response =
                new Variable <ReservationResponse> {
                Name = "response"
            };
            Variable <bool> reserved = new Variable <bool> {
                Name = "reserved"
            };
            Variable <CorrelationHandle> requestHandle =
                new Variable <CorrelationHandle> {
                Name = "RequestHandle"
            };

            // Create a Receive activity
            Receive receiveRequest = new Receive
            {
                ServiceContractName = "ILibraryReservation",
                OperationName       = "RequestBook",
                CanCreateInstance   = true,
                Content             = ReceiveContent.Create
                                          (new OutArgument <ReservationRequest>(request)),
                CorrelatesWith = requestHandle
            };

            // Define the ProcessRequest workflow
            this.Implementation = () => new Sequence
            {
                DisplayName = "ProcessRequest",
                Variables   = { request, response, reserved, requestHandle },
                Activities  =
                {
                    receiveRequest,
                    new WriteLine
                    {
                        Text = new InArgument <string>(
                            env => "Got request from: " +
                            request.Get(env).Requester.BranchName),
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => "Requesting: " +
                                                       request.Get(env).Title),
                    },
                    new Assign
                    {
                        To    = new OutArgument <Boolean>(reserved),
                        Value = new InArgument <Boolean>(env => true)
                    },
                    new Delay
                    {
                        Duration = TimeSpan.FromSeconds(2)
                    },
                    new CreateResponse
                    {
                        Request  = new InArgument <ReservationRequest>(env => request.Get(env)),
                        Response = new OutArgument <ReservationResponse>
                                       (env => response.Get(env)),
                        Reserved = new InArgument <bool>(env => reserved.Get(env)),
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>(env => "Sending response to: " +
                                                       request.Get(env).Requester.BranchName),
                    },
                    new SendReply
                    {
                        Request = receiveRequest,
                        Content = SendContent.Create
                                      (new InArgument <ReservationResponse>(response))
                    }
                }
            };
        }
        private Activity InternalImplementation()
        {
            Variable <string> correlationKey = new Variable <string>();

            Receive recvBootstrap = new Receive
            {
                OperationName       = "Bootstrap",
                ServiceContractName = Constants.ServiceContractName,
                CanCreateInstance   = true,
                CorrelatesOn        = BuildCorrelatesOn(),
                Content             = ReceiveContent.Create(new OutArgument <string>(correlationKey))
            };

            return(new Sequence
            {
                Variables = { correlationKey },
                Activities =
                {
                    new WriteLine             {
                        Text = "Server: Workflow begins."
                    },

                    // TransactedReceiveScope requires a single Request to "bootstrap" the transactional scope.
                    // The body of a TransactedReceiveScope can have any number of activities including Receive activities
                    // that will all execute under the same transaction.
                    new TransactedReceiveScope
                    {
                        Request = recvBootstrap,

                        Body = new Sequence
                        {
                            Activities =
                            {
                                new WriteLine {
                                    Text = "Server: Transaction started."
                                },

                                new SendReply
                                {
                                    Request = recvBootstrap
                                },

                                // Receives in parallel, often referred to as "parallel convoy"
                                new Parallel
                                {
                                    // all branches must complete
                                    CompletionCondition = false,

                                    // all 3 receives will execute under the context of the same transaction
                                    Branches =
                                    {
                                        new Receive
                                        {
                                            OperationName = "WorkBranch1",
                                            ServiceContractName = Constants.ServiceContractName,
                                            CanCreateInstance = false,
                                            CorrelatesOn = BuildCorrelatesOn(),
                                            Content = ReceiveContent.Create(new OutArgument <string>(correlationKey))
                                        },
                                        new Receive
                                        {
                                            OperationName = "WorkBranch2",
                                            ServiceContractName = Constants.ServiceContractName,
                                            CanCreateInstance = false,
                                            CorrelatesOn = BuildCorrelatesOn(),
                                            Content = ReceiveContent.Create(new OutArgument <string>(correlationKey))
                                        },
                                        new Receive
                                        {
                                            OperationName = "WorkBranch3",
                                            ServiceContractName = Constants.ServiceContractName,
                                            CanCreateInstance = false,
                                            CorrelatesOn = BuildCorrelatesOn(),
                                            Content = ReceiveContent.Create(new OutArgument <string>(correlationKey))
                                        }
                                    }
                                }
                            }
                        }
                    },
                    new WriteLine             {
                        Text = "Server: Transaction commits."
                    },

                    new WriteLine             {
                        Text = "Server: Workflow ends"
                    },
                }
            });
        }
        private static Activity GetPropertyWorkflow()
        {
            // Correlation handle used to link operations together
            Variable <CorrelationHandle> operationHandle = new Variable <CorrelationHandle>();

            // The generated property Id
            Variable <Guid> propertyId = new Variable <Guid>();

            // Variable used to indicate that the workflow should finish
            Variable <bool> finished = new Variable <bool>("Finished", false);

            Variable <string> address     = new Variable <string>();
            Variable <string> owner       = new Variable <string>();
            Variable <double> askingPrice = new Variable <double>();

            // Initial receive - this kicks off the workflow
            Receive receive = new Receive
            {
                CanCreateInstance   = true,
                OperationName       = "UploadPropertyInformation",
                ServiceContractName = XName.Get("IProperty", ns),
                Content             = new ReceiveParametersContent
                {
                    Parameters =
                    {
                        { "address",     new OutArgument <string>(address)     },
                        { "owner",       new OutArgument <string>(owner)       },
                        { "askingPrice", new OutArgument <double>(askingPrice) }
                    }
                }
            };

            // Define the local namespace
            XPathMessageContext messageContext = new XPathMessageContext();

            messageContext.AddNamespace("local", ns);

            // Extracts the guid sent back to the client on the initial response
            MessageQuerySet extractGuid = new MessageQuerySet
            {
                { "PropertyId", new XPathMessageQuery("sm:body()/ser:guid", messageContext) }
            };

            // Extracts the guid sent up with the property image
            MessageQuerySet extractGuidFromUploadRoomInformation = new MessageQuerySet
            {
                { "PropertyId", new XPathMessageQuery(@"sm:body()/local:UploadRoomInformation/local:propertyId", messageContext) }
            };

            // Receive used to indicate that the upload is complete
            Receive receiveDetailsComplete = new Receive
            {
                OperationName       = "DetailsComplete",
                ServiceContractName = XName.Get("IProperty", ns),
                CorrelatesWith      = operationHandle,
                CorrelatesOn        = extractGuid,
                Content             = ReceiveContent.Create(new OutArgument <Guid>(propertyId))
            };

            Variable <string> roomName = new Variable <string>();
            Variable <double> width    = new Variable <double>();
            Variable <double> depth    = new Variable <double>();

            // Receive room information
            Receive receiveRoomInfo = new Receive
            {
                OperationName       = "UploadRoomInformation",
                ServiceContractName = XName.Get("IProperty", ns),
                CorrelatesWith      = operationHandle,
                CorrelatesOn        = extractGuidFromUploadRoomInformation,
                Content             = new ReceiveParametersContent
                {
                    Parameters =
                    {
                        { "propertyId", new OutArgument <Guid>()           },
                        { "roomName",   new OutArgument <string>(roomName) },
                        { "width",      new OutArgument <double>(width)    },
                        { "depth",      new OutArgument <double>(depth)    },
                    }
                }
            };

            return(new Sequence
            {
                Variables = { propertyId, operationHandle, finished, address, owner, askingPrice },
                Activities =
                {
                    receive,
                    new WriteLine {
                        Text = "Assigning a unique ID"
                    },
                    new Assign <Guid>
                    {
                        To = new OutArgument <Guid> (propertyId),
                        Value = new InArgument <Guid> (Guid.NewGuid())
                    },
                    new WriteLine  {
                        Text = new InArgument <string> (env => string.Format("{0} is selling {1} for {2}.\r\nAssigned unique id {3}.",owner.Get(env),                                                                                                            address.Get(env), askingPrice.Get(env), propertyId.Get(env)))
                    },
                    new SendReply
                    {
                        Request = receive,
                        Content = SendContent.Create(new InArgument <Guid> (env => propertyId.Get(env))),
                        CorrelationInitializers =
                        {
                            new QueryCorrelationInitializer
                            {
                                CorrelationHandle = operationHandle,
                                MessageQuerySet = extractGuid
                            }
                        }
                    },
                    new While
                    {
                        Condition = ExpressionServices.Convert <bool>(env => !finished.Get(env)),
                        Body = new Pick
                        {
                            Branches =
                            {
                                new PickBranch
                                {
                                    Variables =           { roomName,                 width,depth                                                                                                                     },
                                    Trigger = receiveRoomInfo,
                                    Action = new WriteLine{
                                        Text = new InArgument <string> (env => string.Format("Room '{0}' uploaded, dimensions {1}W x {2}D",roomName.Get(env),                                                                                                         width.Get(env),   depth.Get(env)))
                                    },
                                },
                                new PickBranch
                                {
                                    Trigger = receiveDetailsComplete,
                                    Action = new Sequence
                                    {
                                        Activities =
                                        {
                                            new Assign <bool>
                                            {
                                                To = new OutArgument <bool>(finished),
                                                Value = new InArgument <bool>(true)
                                            },
                                            new WriteLine {
                                                Text = "Property Details Complete"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    new WriteLine {
                        Text = "Finished!"
                    }
                }
            });
        }
Exemple #17
0
        private static Activity GetApprovePO(Variable <PurchaseOrder> po, Variable <bool> replyPO)
        {
            Receive approvePO = new Receive
            {
                OperationName       = "ApprovePurchaseOrder",
                CanCreateInstance   = true,
                ServiceContractName = "FinanceService",
                SerializerOption    = SerializerOption.XmlSerializer,
                Content             = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po))
            };

            Activity workflow = new CorrelationScope()
            {
                Body = new Sequence
                {
                    Variables  = { po, replyPO },
                    Activities =
                    {
                        approvePO,
                        new WriteLine
                        {
                            Text = new InArgument <string>(env => ("Purchase order approval request received"))
                        },
                        new If
                        {
                            Condition = new InArgument <bool> (env => (po.Get(env).RequestedAmount <= 100)),
                            Then      =
                                new Assign <bool>
                            {
                                Value = true,
                                To    = new OutArgument <bool>(replyPO)
                            },
                            Else =
                                new Assign <bool>
                            {
                                Value = false,
                                To    = new OutArgument <bool>(replyPO)
                            },
                        },

                        new If
                        {
                            Condition = new InArgument <bool> (replyPO),
                            Then      =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Purchase Order Approved")
                            },
                            Else =
                                new WriteLine
                            {
                                Text = new InArgument <string>("Purchase Order Cannot be Approved")
                            },
                        },
                        new SendReply
                        {
                            Request = approvePO,
                            Content = SendContent.Create(new InArgument <bool>(replyPO)),
                        },
                    }
                }
            };

            return(workflow);
        }
Exemple #18
0
        static Sequence PurchaseOrderRequest(Endpoint endpoint)
        {
            Variable <PurchaseOrder> po = new Variable <PurchaseOrder>
            {
                Name = "po"
            };
            Variable <bool> result = new Variable <bool>
            {
                Name = "result"
            };

            Send approveExpense = new Send
            {
                ServiceContractName = XName.Get("FinanceService", "http://tempuri.org/"),
                Endpoint            = endpoint,
                OperationName       = "ApprovePurchaseOrder",
                Content             = SendContent.Create(new InArgument <PurchaseOrder>(po)),
                SerializerOption    = SerializerOption.XmlSerializer
            };

            Sequence workflow = new Sequence
            {
                Variables  = { po, result },
                Activities =
                {
                    new Assign <PurchaseOrder>
                    {
                        Value = new InArgument <PurchaseOrder>((e) => new PurchaseOrder{
                            RequestedAmount = 500, Description = "New PC"
                        }),
                        To = new OutArgument <PurchaseOrder>(po)
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>("Hello")
                    },
                    approveExpense,
                    new ReceiveReply
                    {
                        Request = approveExpense,
                        Content = ReceiveContent.Create(new OutArgument <bool>(result))
                    },

                    new If
                    {
                        Condition = new InArgument <bool> (result),
                        Then      =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Purchase order Approved")
                        },
                        Else =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Purchase order Cannot be Approved")
                        },
                    },
                }
            };

            return(workflow);
        }
Exemple #19
0
        static Activity GetServiceWorkflow()
        {
            Variable <PurchaseOrder>     po          = new Variable <PurchaseOrder>();
            Variable <OrderStatus>       orderStatus = new Variable <OrderStatus>();
            Variable <CorrelationHandle> poidHandle  = new Variable <CorrelationHandle>();
            Variable <bool> complete = new Variable <bool>()
            {
                Default = false
            };

            Receive submitPO = new Receive
            {
                CanCreateInstance   = true,
                ServiceContractName = Constants.POContractName,
                OperationName       = Constants.SubmitPOName,
                Content             = ReceiveContent.Create(new OutArgument <PurchaseOrder>(po)) // creates a ReceiveMessageContent
            };

            return(new Sequence
            {
                Variables = { po, orderStatus, poidHandle, complete },
                Activities =
                {
                    submitPO,
                    new WriteLine {
                        Text = "Received Purchase Order"
                    },
                    new Assign <int>
                    {
                        To = new OutArgument <int>((e) => po.Get(e).Id),
                        Value = new InArgument <int>((e) => new Random().Next())
                    },
                    new SendReply
                    {
                        Request = submitPO,
                        Content = SendContent.Create(new InArgument <int>((e) => po.Get(e).Id)),// creates a SendMessageContent
                        CorrelationInitializers =
                        {
                            new QueryCorrelationInitializer
                            {
                                // initializes a correlation based on the PurchaseOrder Id sent in the reply message and stores it in the handle
                                CorrelationHandle = poidHandle,
                                MessageQuerySet = new MessageQuerySet
                                {
                                    // Here we use our custom LinqMessageQuery for correlatoin
                                    // int is the name of the parameter being sent in the outgoing response
                                    { "PoId", new LinqMessageQuery(XName.Get("int",Constants.SerializationNamespace))                  }
                                }
                            }
                        }
                    },
                    new Assign <OrderStatus>
                    {
                        To = orderStatus,
                        Value = new InArgument <OrderStatus>((e) => new OrderStatus()
                        {
                            Confirmed = false
                        })
                    },
                    new While   // Continue the workflow until the PurchaseOrder is confirmed
                    {
                        Condition = ExpressionServices.Convert <bool>(env => orderStatus.Get(env).Confirmed),
                        Body = new Receive
                        {
                            ServiceContractName = Constants.POContractName,
                            OperationName = Constants.ConfirmPurchaseOrder,
                            CorrelatesWith = poidHandle,       // identifies that the ConfirmPurchaseOrder operation is waiting on the PurchaseOrderId that was used to initialize this handle
                            CorrelatesOn = new MessageQuerySet // the query that is used on an incoming message to find the requisite PurchaseOrderId specified in the correlation
                            {
                                // Id is the name of the incoming parameter within the OrderStatus
                                { "PoId",new LinqMessageQuery(XName.Get("Id",                   Constants.DefaultNamespace))       }
                            },
                            Content = ReceiveContent.Create(new OutArgument <OrderStatus>(orderStatus)) // creates a ReceiveMessageContent
                        }
                    },
                    new WriteLine {
                        Text = "Purchase Order confirmed."
                    },
                    new WriteLine {
                        Text = new InArgument <string>((e) => string.Format("Workflow completed for PurchaseOrder {0}: {1} {2}s",po.Get(e).Id,                                          po.Get(e).Quantity, po.Get(e).PartName))
                    },
                }
            });
        }
Exemple #20
0
        static Sequence ExpenseRequest(Endpoint endpoint)
        {
            Variable <Expense> mealExpense = new Variable <Expense>
            {
                Name = "mealExpense",
            };
            Variable <bool> result = new Variable <bool>
            {
                Name = "result"
            };

            Send approveExpense = new Send
            {
                ServiceContractName = XName.Get("FinanceService", "http://tempuri.org/"),
                Endpoint            = endpoint,
                OperationName       = "ApproveExpense",
                Content             = SendContent.Create(new InArgument <Expense>(mealExpense))
            };

            approveExpense.KnownTypes.Add(typeof(Travel));
            approveExpense.KnownTypes.Add(typeof(Meal));
//<Snippet1>
            Sequence workflow = new Sequence
            {
                Variables  = { mealExpense, result },
                Activities =
                {
                    new Assign <Expense>
                    {
                        Value = new InArgument <Expense>((e) => new Meal{
                            Amount = 50, Location = "Redmond", Vendor = "KFC"
                        }),
                        To = new OutArgument <Expense>(mealExpense)
                    },
                    new WriteLine
                    {
                        Text = new InArgument <string>("Hello")
                    },
                    approveExpense,
                    new ReceiveReply
                    {
                        Request = approveExpense,
                        Content = ReceiveContent.Create(new OutArgument <bool>(result))
                    },

                    new If
                    {
                        Condition = new InArgument <bool> (result),
                        Then      =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Expense Approved")
                        },
                        Else =
                            new WriteLine
                        {
                            Text = new InArgument <string>("Expense Cannot be Approved")
                        },
                    },
                }
            };

//</Snippet1>
            return(workflow);
        }
        private Activity InternalImplementation()
        {
            Variable <string> reply = new Variable <string> {
                Name = "replyString"
            };

            Send send = new Send
            {
                OperationName             = "StartSample",
                Content                   = SendContent.Create(new InArgument <string>("Client side: Send request.")),
                EndpointConfigurationName = "codeServiceEndpoint",
                ServiceContractName       = "ITransactedReceiveService",
            };

            return(new CorrelationScope
            {
                Body = new Sequence
                {
                    Variables = { reply },
                    Activities =
                    {
                        new WriteLine             {
                            Text = "Client workflow begins."
                        },

                        new TransactionScope
                        {
                            Body = new Sequence
                            {
                                Activities =
                                {
                                    // Transaction DistributedIdentifier will be emtpy until the send activity causes the transaction to promote to MSDTC
                                    new PrintTransactionInfo(),
                                    new WriteLine {
                                        Text = new InArgument <string>("Client side: Beginning send.")
                                    },
                                    send,
                                    new WriteLine {
                                        Text = new InArgument <string>("Client side: Send complete.")
                                    },
                                    new ReceiveReply
                                    {
                                        Request = send,
                                        Content = ReceiveContent.Create(new OutArgument <string>(reply))
                                    },
                                    new WriteLine {
                                        Text = new InArgument <string>(new VisualBasicValue <string>()
                                        {
                                            ExpressionText = "\"Client side: Reply received = '\" + replyString.toString() + \"'\""
                                        })
                                    },
                                    new PrintTransactionInfo(),
                                    new WriteLine {
                                        Text = new InArgument <string>("Client side: Receive complete.")
                                    },
                                },
                            },
                        },
                        new WriteLine             {
                            Text = "Client workflow ends."
                        }
                    }
                }
            });
        }