Chapter 2 WF 2.1 WF 2.2 2. XAML 2.
2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF
WF WF WF WF EDI API WF Visual Studio Designer 1 2.1 WF Windows Workflow Foundation
2 WF 1 WF Domain-Specific Language DSL WF DSL Object AccountReceivable Bill AccountReceivable Bill AccountReceivable AccountReceivable AccountReceivable DSL
2.2 WF WF 1 WF Microsoft SQL Server.NET Framework 2.0 ADO.NET Windows WF Microsoft Visio 2-1 WF 1 2.2 2-1 Windows Workflow Foundation 7
2 WF 1 RFQProcess RFQFlow RFQ Request For Quotation WF Code IfElse Code 2-2 Code 2-2 WF 8
RFQRequest RFQResponse Quotation DeliveryMethod Email Fax PostMail Visual Studio 200 2- RFQBusinessEntity 1 2.2 2-2.2.1 RFQRequest Windows Workflow Foundation 9
2 WF 1 1 WF Dictionary System.ArgumentException OutputParameters R F Q P r o c e s s RFQBusinessEntity RFQFlow.xoml Private _QuotationRequest As RFQBusinessEntitiy.RFQRequest Public WriteOnly Property RuotationRequest() As _ RFQBusinessEntitiy.RFQRequest Set(ByVal value As RFQBusinessEntitiy.RFQRequest) _QuotationRequest = value End Set End Property Private _QuotationResponse As RFQBusinessEntitiy.RFQResponse Public ReadOnly Property QuotationResponse() As _ RFQBusinessEntitiy.RFQResponse Get Return _QuotationResponse End Get End Property Private _Quotation As RFQBusinessEntitiy.Quotation 0 QuotationRequest QuotationResponse _Quotation
2.2.2 Code Activity Code ReceiveQuotationRequest ExecuteCode ExecuteCode ExecuteCode Code XXXX_ExecuteCode XXXX ReceiveQuotationRequest Private Sub ReceiveQuotationRequest_ExecuteCode (ByVal sender As _ System.Object, ByVal e As System.EventArgs) Sysem.EventArgs ReceiveQuotationRequest RFQResponse 1 2.2 Private Sub ReceiveQuotationRequest_ExecuteCode (ByVal sender As _ System.Object, ByVal e As System.EventArgs) _QuotationResponse = New RFQBusinessEntitiy.RFQResponse _QuotationResponse.DeliverMethod = _QuotationRequest.PreferDeliverMethod _QuotationResponse.RFQNo = _QuotationRequest.RFQNumber Windows Workflow Foundation 1
2 WF 1 CreateCustomer RFQBusinessEntity DBHelper Private Sub CreateCustomer_ExecuteCode(ByVal sender As System.Object, _ ByVal e As System.EventArgs) DBHelper.CreateCustomerInDB(_QuotationRequest.Customer, QuotationRequest.FaxNumber, QuotationRequest.Address, _QuotationRequest.Email) WF Code IfElse While CreateQuotation Private Sub CreateQuotation_ExecuteCode(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim QuotationID As Int2 Dim ProductPrice As Decimal Dim CustomerID As Int2 CustomerID = DBHelper.GetCustomerId(_QuotationRequest.Customer) DBHelper.CreateRFQInDB(_QuotationRequest.RFQNumber, CustomerID, QuotationRequest.ProductName, _QuotationRequest.Quantity, QuotationRequest.RequireDate, _QuotationRequest.CustomerRFQNo) ProductPrice = DBHelper.GetProductPrice(_QuotationRequest.ProductName) 2
QuotationID = DBHelper.CreateQuotationInDB(CustomerID, QuotationRequest.RFQNumber, _QuotationRequest.ProductName, QuotationRequest.Quantity, _ ProductPrice, Today.Add(New TimeSpan(0, 0, 0, 0))) _Quotation = New RFQBusinessEntitiy.Quotation With _Quotation.Customer = _QuotationRequest.Customer.ExpireDate = Today.Add(New TimeSpan(0, 0, 0, 0)).Price = ProductPrice.ProductName = _QuotationRequest.ProductName.Quantity = _QuotationRequest.Quantity.QuotationNo = QuotationID.ToString("00000").RFQNumber = _QuotationRequest.RFQNumber End With WF Code MailQuotation 1 2.2 Private Sub MailQuotation_ExecuteCode(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Dim SMTPMessage As New Mail.MailMessage("WF@demo.local", QuotationRequest.Email, _ "Quotation of " & _Quotation.RFQNumber, _ "Dear " & _Quotation.Customer & ":" & ControlChars.NewLine & _ "The price of " & _QuotationRequest.ProductName & " is : " & Quotation.Price.ToString) Windows Workflow Foundation
2 WF 1 Dim MailClient As New Mail.SmtpClient("localhost") Try MailClient.Send(SMTPMessage) Catch ex As Exception Throw New Exception(ex.Message) End Try.NET Framework System.Net.Mail SMTP Private Sub MailQuotation_ExecuteCode(ByVal sender As System.Object, _ ByVal e As System.EventArgs) SendMail("WF@demo.local", QuotationRequest.Email, _ "Quotation of " & _Quotation.RFQNumber, _ "Dear " & _Quotation.Customer & ":" & ControlChars.NewLine & _ "The price of " & _QuotationRequest.ProductName & " is : " & Quotation.Price.ToString) Private Sub SendMail(ByVal From As String, ByVal [To] As String, _ ByVal Subject As String, ByVal Body As String) Dim SMTPMessage As New Mail.MailMessage(From, [To], _ Subject, Body) Dim MailClient As New Mail.SmtpClient("localhost") Try MailClient.Send(SMTPMessage) Catch ex As Exception Throw New Exception(ex.Message) End Try SendMail Code
SendMail 2.2. IfElse IfElse Visual Basic C# Select Case, switch, If ElseIf IfElse IfElse Condition 2-1 2.2 2- Windows Workflow Foundation
2 WF 1 _QuotationRequest PreferDeliverMethod DeliveryMethod. Fax 2-2- IsNewCustomer Private Sub IsNewCustomer(ByVal sender As System.Object, _ ByVal e As System.Workflow.Activities.ConditionalEventArgs) If DBHelper.IsCustomerExist(_QuotationRequest.Customer) Then e.result = False Else e.result = True End If S y s e t m. Workflow.Activities.ConditionalEventArgs Result WF
2.2. WF Code RFQWinClient Window Form RFQForm 2-1 2.2 2- WF WF Private _WFRuntime As WorkflowRuntime Load WF StartRuntime Windows Workflow Foundation 7
2 WF 1 Private Sub RFQForm_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load _WFRuntime = New WorkflowRuntime _WFRuntime.StartRuntime() AddHandler _WFRuntime.WorkflowCompleted, AddressOf OnWorkflowCompleted RefreshRFQ() WF WorkflowRuntime W o r k f l o w A b o r t e d WorkflowInstance Abort WorkflowAborted WorkflowCompleted WorkflowCreated WorkflowRuntime CreateWrokflow WorkflowIdled WorkflowLoaded WorkflowPersisted WorkflowSuspended Suspend WorkflowResumed WorkflowStarted 8
WorkflowTerminated WorkflowTe rminatedeventargs WorkflowUnloaded Submit Submit Click WF WorkflowInstance InstanceID Guid InstanceID Submit Click 1 2.2 Private Sub btnsubmit_click(byval sender As System.Object, _ ByVal e As System.EventArgs) Handles btnsubmit.click Dim WFInstance As WorkflowInstance Dim RFQ As RFQBusinessEntitiy.RFQRequest Dim DeliveryMethod As RFQBusinessEntitiy.DeliveryMethod If rdbemail.checked Then DeliveryMethod = RFQBusinessEntitiy.DeliveryMethod.Email Else DeliveryMethod = RFQBusinessEntitiy.DeliveryMethod.Fax End If RFQ = New RFQBusinessEntitiy.RFQRequest(txtRFQNo.Text, _ txtcustomerrfqno.text, txtcustomer.text, _ txtaddress.text, CDate(txtRequireDate.Text), _ txtemail.text, txtfaxno.text, txtproductname.text, _ txtquantity.text, DeliveryMethod) Dim WFParameters As New Dictionary(Of String, Object) WFParameters.Add("QuotationRequest", RFQ) WFInstance = _WFRuntime.CreateWorkflow(GetType(RFQProcess.RFQFlow), _ WFParameters) WFInstance.Start() txtmessage.text &= "New Quotation Submitted, ID of instance : " & _ Windows Workflow Foundation 9
2 WF 1 WFInstance.InstanceId.ToString & ControlChars.NewLine RefreshRFQ() C l i c k AutoResetEvent WF AutoResetEvent InstanceID Workflow CpmpletedEventArgs WorkflowInstance WorkflowInstance InstanceId Private Sub OnWorkflowCompleted(ByVal sender As Object, _ ByVal e As System.Workflow.Runtime.WorkflowCompletedEventArgs) Dim Response As RFQBusinessEntitiy.RFQResponse Response = CType(e.OutputParameters("QuotationResponse"), _ RFQBusinessEntitiy.RFQResponse) SetText(String.Format("Instance : {0} return response, " & _ "Quotation No is {1}, Send date is {2}", _ e.workflowinstance.instanceid.tostring, _ Response.QuotationNo, Response.SendDate.ToString)) Form_Load StopRuntime FormClosed Private Sub RFQForm_FormClosed(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed _WFRuntime.StopRuntime() 0
WorkflowRuntime IsStarted False StartRuntime 2. XAML WF RFQProcess RFQFlow.xoml Visual Studio 200 2-7 XML 1 2. X AML 2-7 XAML SequentialWorkflow Activity Windows Workflow Foundation 1
2 WF 1 CodeActivity XML XML XAML extendsible Application Markup Language X M L W F.NET Framework.0 Windows Presentation Foundation XAML WPF Hello World! <Panel> <Button> Hello World! </Button> </Panel> XAML XAML XML.NET XML.NET 2..1 XAML XML.NET 2
<SequentialWorkflowActivity x:class="ch02.quotation.rfqprocess.rfqflow" x:name="rfqflow" xmlns:x="http://schemas.microsoft.com/winfx/200/xaml" xmlns="http://schemas.microsoft.com/winfx/200/xaml/workflow"> <CodeActivity x:name="receivequotationrequest" ExecuteCode="ReceiveQuotationRequest_ExecuteCode" /> </SequentialWorkflowActivity> xmlns WF.NET Framework System.Workflow.Activities x XAML x:name XAML x:class XAML WF CH02.Quotation.RFQProcess.RFQFlow WF.xoml WF XAML WF XAML 1 ToGetMax XOML <SequentialWorkflowActivity x:class="firstwf.workflow2" x:name="workflow2" xmlns:ns0="clr-namespace:firstwf" xmlns:x="http://schemas.microsoft.com/winfx/200/xaml" xmlns="http://schemas.microsoft.com/winfx/200/xaml/workflow"> <ns0:togetmax x:name="togetmax1" FirstNumber="0" SecondNumber="0" Result="0" /> </SequentialWorkflowActivity> 1 2. X AML ns0 clr-namespace:firstwf ToGetMax XAML Windows Workflow Foundation
2 WF 1 clrnamespace.net XML.NET XAML <SequentialWorkflowActivity x:class="firstwf.workflow2" x:name="workflow2" xmlns:wfn="http://schemas.wfn.idv.tw/winwf/activities" xmlns:x="http://schemas.microsoft.com/winfx/200/xaml" xmlns="http://schemas.microsoft.com/winfx/200/xaml/workflow"> <wfn:togetmax x:name="togetmax1" FirstNumber="0" SecondNumber="0" Result="0" /> </SequentialWorkflowActivity> <Assembly: XmlnsDefinition( _ "http://schemas.wfn.idv.tw/winwf/activities", _ "CH01.FirstWF.Activities")> XAML XML.NET.xoml http://schemas.wfn.idv.tw/winwf/activities.net CH01.FirstWF.Activities CH01.FirstWF.Activities. ToGetMax 2..2 XAML XAML Visual Studio 200 RFQProcess RFQFlow.xoml RFQFlow.xoml.vb 2-8
.xoml.xoml ExecuteCode WF 1 2. 2-8 Code Behind 2-8 RFQFlow.xoml.vb RFQFlow.rules Visual Studio XML XAML.xoml Visual Studio X AML Visual Studio.xoml XML CDATA Windows Workflow Foundation
2 WF 1 <SequentialWorkflowActivity xmlns:x="http://schemas.microsoft.com/winfx/200/xaml" xmlns="http://schemas.microsoft.com/winfx/200/xaml/workflow" x:class="purexaml"> <CodeActivity ExecuteCode="SayHello"> <x:code> <![CDATA[ Private Sub SayHello(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("Hello, Pure XAML!") ]]> </x:code> </CodeActivity> </SequentialWorkflowActivity> CDATA ExecuteCode XAML 2.. WF WFC.exe XAML WF wfc.exe Windows Vista SDK XOML PureXAMLFlow.xoml wfc purexamlflow.xoml -language:vb -out:purexamlflow.dll WF Type GetType GetType PureXAML XOML
x:class PureXAMLFlow 1 Shared Sub Main() Using workflowruntime As New WorkflowRuntime() AddHandler workflowruntime.workflowcompleted, _ AddressOf OnWorkflowCompleted AddHandler workflowruntime.workflowterminated, _ AddressOf OnWorkflowTerminated Try Dim WorkflowType As Type WorkflowType = Type.GetType("PureXAML, PureXAMLFlow") Dim workflowinstance As WorkflowInstance workflowinstance = _ workflowruntime.createworkflow(workflowtype) workflowinstance.start() WaitHandle.WaitOne() Catch ex As Exception Console.WriteLine(ex.Message) Console.ReadLine() End Try End Using XOML 2. 2. RFQ Windows Workflow Foundation 7
2 WF 1 WF WF WF WF WF 8