1. Trang chủ
  2. » Công Nghệ Thông Tin

Introducing dot NET 4 0 with Visual Studio_2 pdf

45 548 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 45
Dung lượng 1,9 MB

Nội dung

CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 147 Figure 6-13. New flowchart workflow 3. The design view for flowcharts looks slightly different than sequential workflows. The green circle indicates where the workflow starts. We need to create a new activity to read input from the user. Create a new class called ReadInput. 4. Enter the following using statement: using System.Activities; 5. Now enter the following code: public class ReadInput : CodeActivity<Int32> { protected override Int32 Execute(CodeActivityContext context) { return Convert.ToInt32(Console.ReadLine()); } } 6. Save the class and compile the application. 7. Open Workflow1.xaml. 8. Drag a WriteLine activity beneath the green circle and change the Display Name to “What is your age?” and set the Text to “What is your age?” CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 148 9. Drag the new ReadInput activity beneath the “What is your age?” activity and change the display name to “Read input.” 10. Create a new variable called age of type Int32. 11. On the ReadInput activity, set the Result property to age. The next thing to do is determine if the customer is old enough to see the film (which in this case will always have an 18 rating). Flow chart workflows have a new type of activity not found in sequential workflows, called FlowDecision. 1. Drag a FlowDecision activity beneath the read input block and change the condition to Age >= 18. There are obviously two possibilities to this expression: • Customer is old enough so they can see the film (FlowDecision condition = true). • Customer is too young, so shouldn’t be seeing any movies (FlowDecision condition = false). 2. To simulate the customer failing age verification, drag a WriteLine activity to the right of the flow decision and change the display name and text to “Sorry not old enough.” 3. Drag another WriteLine activity beneath the flow decision and change the display name and text to “Age validation successful.” 4. We now need to link up the activities we have just created. Move the mouse over the green circle that indicates the start of the flow chart workflow, and three grey dots will appear around it. Click the one on the bottom of the circle and then drag the mouse down to the ReadInput activity. 5. When you near the WriteLine activity, three grey dots will appear around it. Drag the line to one of these dots and then release the mouse button to link up the start of the workflow with our read line activity. 6. Link up the “What is your age?” and ReadInput activities. 7. We need to join the FlowDecision up to the workflow. FlowDecision activities have two nodes, true or false, that surprisingly indicate the path to take when the condition specified is true or false. Drag the false node to the “Sorry not old enough” WriteLine activity and then drag another line from “Sorry not old enough” back round to the ReadInput activity. 8. Drag the true node on the FlowDecision activity to the “Age validation successful” activity. 9. Finally drag a line between the “What is your age?” and ReadInput activity. Your final work flow should look like Figure 6-14. 10. Open Program.cs and add a Console.ReadKey(); beneath the invoke command so the application doesn’t close immediately. 11. That’s it; your workflow is ready to run. Press F5 to run it. 12. Try entering different ages and note that unless you enter at least 18 the workflow will write “Sorry not old enough.” CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 149 Figure 6-14. Final age validation work flow WCF/Messaging Improvements A number of enhancements have been introduced in WF4 to improve integration with WCF and to ease messaging scenarios. Correlation Correlation functionality first appeared in WF3.5 and allows you to route incoming messages to specific workflow instances based on their content or protocol used. For example if you have a very long running workflow where replies take weeks or months to return it is important that when a reply is received it is sent to the correct individual workflow. ReceiveAndSendReply and SendAndReceiveReply are the new activities discussed in the following sections that provide a correlated send and receive activities with a number of new methods of correlation such as xpath and correlation scope. WCF Workflow Service Applications WCF Workflow Service applications are a new type of project in VS2010 that make it very easy to create workflows for sending and receiving data. They essentially provide a declarative WCF service defined CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 150 using workflow activities. WCF Workflow Service applications have all the benefits of WF such as support for long-running services, GUI interface, and also the additional benefits that as they are declared declaratively so are easy to deploy and version. VS2010 comes with a WCF Workflow Service Application template that you can adapt for your own needs. The sample application simply echoes a number you send to it back to you. Let’s take this for a spin now. 1. Create a new WCF Workflow Service project called Chapter6.WFService. The template will contain a sequential activity looking very similar to Figure 6-15. Figure 6-15. WF Service project 2. This sequential activity is defined in the file Service1.xamlx. If you open this up with the XML editor you will see the XAML that defines this service (boring bits removed as it's pretty long): <p:Sequence DisplayName="Sequential Service" sad:XamlDebuggerXmlReader.FileName="D:\wwwroot\book\Chapter6_WF\Chapter6.WFService\Cha pter6. WFService\Service1.xamlx"> <p:Sequence.Variables> <p:Variable x:TypeArguments="CorrelationHandle" Name="handle" /> <p:Variable x:TypeArguments="x:Int32" Name="data" /> </p:Sequence.Variables> CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 151 <Receive x:Name="__ReferenceID0" DisplayName="ReceiveRequest" OperationName="GetData" ServiceContractName="contract:IService" CanCreateInstance="True"> <Receive.CorrelationInitializers> <RequestReplyCorrelationInitializer CorrelationHandle="[handle]" /> </Receive.CorrelationInitializers> <ReceiveMessageContent> <p:OutArgument x:TypeArguments="x:Int32">[data]</p:OutArgument> </ReceiveMessageContent> </Receive> <SendReply Request="{x:Reference Name=__ReferenceID0}" DisplayName="SendResponse" > <SendMessageContent> <p:InArgument x:TypeArguments="x:String">[data.ToString()]</p:InArgument> </SendMessageContent> </SendReply> </p:Sequence> </WorkflowService> 3. As the template service doesn’t do anything apart from echo a value back, we are going to modify it slightly so we can see a change. In the SendResponse box click the Content text box and amend the Message data property to the following: data.ToString() + " sent from WF service" 4. Click OK. 5. Save the project. 6. Now add a new console application to the solution called Chapter6.WFServiceClient. 7. Add a service reference to Chapter6.WFService (click Add Service Reference then DiscoverServices in Solution; it will be listed as Service1.xamlx). 8. Leave the namespace as ServiceReference1. 9. In Chapter6.WFServiceClient modify Program.cs to the following: ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient(); Console.WriteLine(client.GetData(777)); Console.ReadKey(); 10. Set Chapter6.WFServiceClient as the startup project and press F5 to run. You should see the message “777 sent from WF Service” output to the console. If you wanted to deploy this service, you could simply copy the the Service1.xamlx and Web.config file to a web server or even host it using “Dublin.” Activities A number of new activities are introduced in WF4, and some activities from WF3 have been dropped. Note the Microsoft upgrade documents mentioned at the start of this chapter contain more detail on these changes and suggest an upgrade path. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 152 WF3 Activity Replacements Some existing WF3 activites have now been dropped. The suggested replacements are listed below: • IfElse becomes If or Switch. • Listen becomes Pick. • Replicator becomes ForEach or ParallelForEach. • CodeActivity is gone and you should use activity customization as described above. New Activities WF4 introduces a number of new activities. AddToCollection, RemoveFromCollection, ExistsInCollection & ClearCollection Activities for working with collections in your workflows. Assign Assign allows us to assign values to variables and arguments and has been used extensively in the previous examples. CancellationScope CancellationScope allows you to specify activities to be run should an activity be cancelled. The body section surrounds the code you may wish to cancel and the cancellation handler section specifies code to run if an activity is cancelled. See Figure 6-16. Figure 6-16. CancellationScope CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 153 CompensatableActivity An advanced activity used for long running workflows that allows you to define compensation, confirmation, and cancellation handlers for an activity. This is used in conjunction with the compensate and confirm activities. DoWhile DoWhile continues to run code until the condition specified is true. The code inside it will be run at least once. See Figure 6-17. Figure 6-17. DoWhile ForEach An activity for looping round a collection. Interop Interop allows you to use your existing WF3 activities and workflow in a WF4 application. Interop can wrap any non-abstract types inherited from System.Workflow.ComponentModel.Activity. Any properties are exposed as arguments to WF4. Interop can help migration from WF3 or allow you to use existing WF3 workflows you don’t possess the source to. InvokeMethod InvokeMethod allows the calling of an existing static method. You can pass generic types, pass parameters by reference and also call it asynchronously. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 154 Parallel Parallel activity was present in WF3, but didn’t truly run activities in parallel (it used time slicing). In WF4 the Parallel activity and ParallelForEach the activities now run truly in parallel subject to suitable hardware. Persist Persist allows you to persist the workflow instance using the current workflow configuration settings. You can also specify areas where state should not be persisted with no-persist zones. Pick Provides functionality for using events and replaces WF3s listen activity. ReceiveAndSendReply and SendAndReceiveReply WF4 has improved support for messaging scenarios by introducing a number of new activities for sending and receiving data between applications and improved support for correlating messages. WF4 introduces the ReceiveAndSendReply (Figure 6-18) and SendAndReceiveReply (correlated versions of Send and Receive) activities that allow you to specify code to run in between the initial Send or receive. Figure 6-18. ReceiveAndSendReply CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 155 These are advanced activities so please see the WF SDK for an example of their usage. The messaging activities can operate with the following types of data: • Message • MessageContracts • DataContracts • XmlSerializable TryCatch TryCatch allows you to specify activities to be performed should an exception occur and code that should always run in a Finally block similar to C# or VB.NET. See Figure 6-19. Figure 6-19. TryCatch Switch<T> and FlowSwitch Switch is similar to the switch statement in C# and contains an expression and a set of cases to process. FlowSwitch is the flowchart equivalent of the switch statement. See Figure 6-20. CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 156 Figure 6-20. Switch Powershell and Sharepoint Activities In the preview versions of WF4, you may have seen Powershell, Sharepoint, and Data activities. These are now moved to the workflow SDK because Microsoft has a sensible rule for framework development that .NET should not have any dependencies on external n external technologies. Misc Improvements WF 4 also contains a number of other enhancements. • WF version 4 is 10 to 100 times faster than WF3, and performance of the WF designer is much improved. • Programming model has been simplified. • Expressions now have intellisense (which can be utilized by activities you create as well). • WF4 has support for running workflows in partial trust environments. • Improved support for declarative (XAML only) workflows. • Add breakpoints can be added to XAML directly. If breakpoints are added in design view and you switch to XAML view they will be shown (and vice versa). See Figure 6-21. [...]... making our intentions for future innovation clear and to call out the fact that as of NET 4. 0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios “ http://blogs.msdn.com/adonet/archive/ 200 8/ 10/ 31/clarifying-the-messageon-l2s-futures.aspx LINQ to SQL changes In VS 201 0/ .NET 4. 0 LINQ to SQL has a number of welcome performance enhancements and bug fixes It is slightly... http://blogs.msdn.com/endpoint/archive/ 200 9 /06 /19/workflow-tracking-profiles-in -net- 4- 0- beta-1.aspx • Run your workflows on “Dublin” or Azure platform (see Chapters 7 and 16) I talked to an experienced WF user John Mcloughlin, a freelance NET consultant specializing in WF and coordinator of the user group Nxtgen Southampton John Mcloughlin http://blog.batfishsolutions.com/ With NET 3 .0 Microsoft introduced the... works at Microsoft within the data programmability team) at http://damieng.com/blog/ 200 9 /06 /01 /linq-to-sql-changes-in -net- 40 Why Use EF? The sections below cover the benefits of using an ORM solution, although please note that some of these advantages are not EF specific—excellent (and more mature) alternatives do exist Probably the bestknown and one of the most mature ORM in the NET world is NHibernate... as: • ASP .NET MVC • Silverlight/WPF (many controls have inbuilt EF support) • ASP .NET (many controls have built-in support for binding to EF objects) 177 CHAPTER 8 ENTITY FRAMEWORK EF 101 Many developers may be unfamiliar with EF as it was released in between VS 200 8 and VS 201 0, or others may have heard bad things about it (not all of which are true) Let’s take a quick look at how to work with EF before... utilizing the NET Framework that would not be possible with standard SQL N-Tier Application Development EF has a number of features (particularly in the latest release), such as support for POCO (Plain old CLR object e.g .net classes!) and self-tracking of change templates, that make it a great solution for the development of n-tier applications Where is EF Used? Several areas of NET 4. 0 and VS 201 0 support... http://msdn.microsoft.com/en-gb/library/dd456789(VS. 100 ).aspx • http://blogs.thinktecture.com/cweyer/default.aspx • http://msdn.microsoft.com/en-us/library/ee3 543 81.aspx • http://blogs.msdn.com/drnick/ • http://blogs.msdn.com/endpoint/ • http://msdn.microsoft.com/en-us/netframework/aa6633 24. aspx 173 CHAPTER 8 Entity Framework Entity Framework (EF) is Microsoft’s Object Relational Mapping (ORM) solution and was first released with NET 3.5SP1... began net. tcp://localhost: 808 1/greeting a netTcpBinding would be used This is a huge step forward from WCF 3.5 that made you create endpoints and would throw an exception if you didn’t Table 7-1 shows the bindings that are used for different addresses Table 7-1 Default Protocol Mappings for Different Types of Addresess Addr es s Bindi ng http basicHttpBinding net. pipe netNamedPipeBinding net. msmq netMsmqBinding... Manager, LINQ to SQL and LINQ to Entities) stated: “We’re making significant investments in the Entity Framework such that as of NET 4. 0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.” http://blogs.msdn.com/adonet/archive/ 200 8/ 10/ 29/update-on-linq-to-sql-and-linq-toentities-roadmap.aspx 175 CHAPTER 8 ENTITY FRAMEWORK However, after feedback from a large... Entity Data Model in Visual Studio The easiest way to create an EDM is by using the ADO .NET data model wizard in Visual Studio 1 Open up Visual Studio 2 Create a New C# Console application and call it Chapter8.HelloEF 3 Right-click on the project and select Add New Item 4 Select ADO .NET Entity Data Model, and name it Chapter8Model.edmx (Figure 8-1) 5 Click Add Figure 8-1 Adding an ADO .NET Entity Data Model... Caching wasn’t too easy to implement prior to WCF4 however it is very easy in WCF4 with the simple addition of the AspNetCache profile attribute to your methods: [AspNetCacheProfile("MyCachingProfile")] You then need to create a caching profile using the following configuration: . more information please refer to: http://blogs.msdn.com/endpoint/archive/ 20 0 9 /06 /19/workflow-tracking-profiles-in -net- 4- 0- beta-1.aspx. • Run your workflows on “Dublin” or Azure platform (see. development that .NET should not have any dependencies on external n external technologies. Misc Improvements WF 4 also contains a number of other enhancements. • WF version 4 is 10 to 100 times faster. statement. See Figure 6- 20 . CHAPTER 6  WINDOWS WORKFLOW FOUNDATION 4 156 Figure 6- 20 . Switch Powershell and Sharepoint Activities In the preview versions of WF4, you may have seen Powershell,

Ngày đăng: 18/06/2014, 17:20

TỪ KHÓA LIÊN QUAN