Web services, WCF services and Multi Threading with Windows Forms
Upcoming SlideShare
Loading in...5

Web services, WCF services and Multi Threading with Windows Forms



What are Web Services?

What are Web Services?
WCF Services
Consuming Web Services
Do the lab



Total Views
Views on SlideShare
Embed Views



1 Embed 3

http://www.linkedin.com 3



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • Click to add notes Peter Gfader Developing Windows and Web applications
  • Task Deploy your homework from week2 via ClickOnce to your local IIS. Homework from week2 = Winforms UI with LINQ 2 SQL  that displays list of employees Show me the screen of ClickOnce:  Properties in WinformsUI, “Publish”, “Application Files”     Question What happens here with ClickOnce... Your current version is 1.5 You rollback a version (in Control panel, uninstall), now you have 1.4   How can you get version 1.5 again? Do you get it automatically?
  • Part 1: Deployment of .NET Winforms apps History Deployment & Updates Security Issues & Warnings Configuration and user settings Part 2: Security Role-based security Authentication and Authorization Impersonation Code Access Security Assembly Strong Naming
  • Java current version 1.6 Update 17 1.7 released next year 2010 Dynamic languages Parallel computing Maybe closures
  • http://prezi.com/tg2kaukw8sez/wcf-intro/
  • The Structure of an Endpoint Each endpoint consists of the following: Address: The address uniquely identifies the endpoint and tells potential consumers of the service where it is located. It is represented in the WCF object model by the EndpointAddress class. An EndpointAddress class contains: A Uri property, which represents the address of the service. An Identity property, which represents the security identity of the service and a collection of optional message headers. The optional message headers are used to provide additional and more detailed addressing information to identify or interact with the endpoint. For more information, see Specifying an Endpoint Address . Binding: The binding specifies how to communicate with the endpoint. This includes: The transport protocol to use (for example, TCP or HTTP). The encoding to use for the messages (for example, text or binary). The necessary security requirements (for example, SSL or SOAP message security). For more information, see Windows Communication Foundation Bindings Overview . A binding is represented in the WCF object model by the abstract base class Binding . For most scenarios, users can use one of the system-provided bindings. For more information, see System-Provided Bindings . Contracts: The contract outlines what functionality the endpoint exposes to the client. A contract specifies: What operations can be called by a client. The form of the message. The type of input parameters or data required to call the operation. What type of processing or response message the client can expect. For more information about defining a contract, see Designing Service Contracts . Behaviors: You can use endpoint behaviors to customize the local behavior of the service endpoint. Endpoint behaviors achieve this by participating in the process of building a WCF runtime. An example of an endpoint behavior is the ListenUri property, which allows you to specify a different listening address than the SOAP or Web Services Description Language (WSDL) address. For more information, see ClientViaBehavior .
  • Show windows search form
  • Demo Windows Search and talk about how this is a threaded application. When you click search a new thread is launched to search for files while the main thread waits for results to display. If it were single threaded, then when you click search the application will become unresponsive until all the search results have returned.
  • STA Model depreciated MTA
  • private void LoadData() { for (int i = 0; i < 30; i++ ) { listBox1.Items.Add("Peter - Gfader - " + DateTime.Now.ToString("s")); Application.DoEvents(); Thread.Sleep(200); } }
  • new Thread(LoadData).Start();
  • Click to add notes Peter Gfader Developing Windows and Web applications

Web services, WCF services and Multi Threading with Windows Forms Web services, WCF services and Multi Threading with Windows Forms Presentation Transcript

  • Developing Windows and Web Applications using Visual Studio.NET Peter Gfader Senior Software Architect
  • Homework?
    • Task
      • Deploy your homework from week2 via ClickOnce to your local IIS.
      • Show me the screen of ClickOnce:  Properties in WinformsUI, “Publish”, “Application Files”
    • Question
      • How can you get version 1.5 again if you rolled back from version 1.6?
      • Do you get it automatically?
    • N-Tier Application
    • Deployment
      • ClickOnce
    • Security
    Session 4: Last week?
    • What are Web Services?
    • WCF Services
    • Consuming Web Services
    • Threading
    • Do the lab
    • Review
    Session 5: WCF and Multi-Threading
    • SSA @ SSW
    • Loves C# and .NET (Java not anymore)
    • Specializes in
      • Windows Forms
      • ASP.NET
      • TFS testing
      • Automated tests
      • Silverlight
    Peter Gfader
  • Web services
    • Wikipedia
      • A web service is a collection of protocols and standards
      • Used for exchanging data between applications or systems
      • Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks
    What are Web Services?
    • The technologies that underpin Web Services are
      • XML
      • SOAP (Simple Object Access Protocol)
      • This is a way of sending around objects over a network
    • Cross Platform e.g.
      • Java Web Service (AXIS) can be consumed by a .NET application and vice versa
    What are Web Services? Con'd
    • There are many public web services available on the Web, and there are Web Services APIs for popular applications such as:
      • Google http://code.google.com/apis/
      • Amazon http://en.wikipedia.org/wiki/Amazon_Web_Services
      • Facebook http://developers.facebook.com/
      • Virtual Earth http://msdn2.microsoft.com/en-us/virtualearth/default.aspx
      • Microsoft, new to the party with Azure : http://www.microsoft.com/azure/default.mspx
      • More here
        • http://www.service-repository.com/
    Public Web Services
    • Formerly .NET 3 was known as WinFX
    • Consists of
      • Windows Presentation Foundation WPF (Silverlight is a subset of this)
      • Windows Communication Foundation WCF
      • Windows Workflow Foundation WF
      • Cardspace
    Windows Communication Foundation (WCF) Part of the .NET Framework 3 and 3.5
  • WCF
    • Building block for building Connected Systems
    • Designed around messages!
    • Support for all standards/specifications around messaging (particularly driven by WS-*)
    • Consistent API regardless of messaging requirement
    • Supports variety of transport and encoding configurations
    • Messaging standard is SOAP but this is not the only option
    • Extensibility model allows for any custom requirements/encoders/channels to be developed
    Not just “Web Services”
    • Supports transport protocols,
      • HTTP Web services
      • UDP User Datagram Protocol
      • TCP Transmission Control Protocol
    • Supports
      • Queues MSMQ (Microsoft Message Queue)
      • Transactions
      • Reliable sessions
      • Better security model
    • http://prezi.com/tg2kaukw8sez/wcf-intro/
    Windows Communication Foundation (WCF)
    • Endpoint
      • Address where the WCF service is available from
      • Binding how to get access to the WCF service
      • Contract what the WCF service can do
      • Behavior how the service will behave locally (e.g. debugging)
    • Built in WCF Service Project
      • Define the service with ServiceContract
      • Define methods with OperationContract
    Creating a WCF Service
  • Define a contract
    • Contract ( I Service.cs)
    • It means INTERFACE
    Define an Interface contract
    • Contract
    • Implementation
    Implement the contract
    • Automatic - Add a Web Reference to your client
      • Dynamically generates a web service client class with all the exposed methods
      • Synchronous and Asynchronous methods
      • Silverlight: All web service method calls are Asynchronous
    Consuming Web Services
    • Creating
    • Consuming
    • Configuring
    WCF Service Demo (The LAB)
  • Multithreading in .Net
    • Why do we need threading?
      • Blocking calls (e.g. Disk IO, Network) on long running-tasks
      • Responsive User Interface (UI)
        • E.g. Windows Desktop Search
      • Better performance (especially on multi-core processors)
    • Application can spawn one or more processes
    • Processes have isolation, separation of
      • Data
      • Memory
      • Resources
    • Processes contain Threads
    • Windows is a Pre-emptive multitasking operating system
    • The O/S is responsible for managing timeslices (quantums) between different processes
    • The Kernal switches/schedules execution code between threads
    • UNIX is a true cooperative multi-tasking operating system
      • Processes instruct the O/S when to switch contexts
      • This is brittle since a poorly written application can hang the system by never allowing other processes to run
    Threading and the O/S
    • System.Threading
    • Background Worker
    • “ Threadsafe” classes (particularly collections)
    • Thread Synchronisation
    • Interprocess communication
    • AppDomain
    • STA (Single Threaded Apartment) Model
    Threading in .Net
    • Cannot Interact with UI Elements from a thread that it did not create (the UI object is not accessible to the other thread)
    • Some classes are not Threadsafe, meaning that data may be overwritten/corrupted (e.g. Data collections)
    • Cannot predicate the sequence when separate threads will finish their work
    Important Concepts
    • Single thread apartment (STA)
    What is STA (and what happened to MTA?)
  • But MTA was in VB6?
    • Application.DoEvents
    • System.Threading
      • Thread.Start
      • Delegate.BeginInvoke
    • BackgroundWorker component
    Threading in WinForms (bad to good)
    • Call this method inside long running code that is being processed
    • What it does is it issues a command to the UI to process pending events on the message queue e.g.
      • Mouse Clicks
      • Redraw Window etc
  • Application.DoEvents() FetchData SearchGoogle() Return Merge Done Worker 1 SearchYahoo() Return Worker 2 UI
    • Do Events processes all Window Messages in the queue until it has run out of messages (click, paint, focus, etc.)
    • In an example of loading a Listbox with items:
      • What happens if the user clicks the close
      • button? Ooops!  IndexOutOfRangeException! Clicking on the close box has invoked the form’s Dispose method, which has cleared the control collection.  What's worse is that clicking close on the form didn't stop the list box processing from finishing up - while the form was no longer visible, this handler was merrily keeping the application alive.
      • Why wouldn’t this normally happen?
      • The action of the user pressing the close button would not be processed until you’ve returned from the handler for the button click.
    DoEvents is Bad!
    • GOOD
    • BAD usage of DoEvents
    • Application.Run
    • Process mouse up on button
    • Fire ButtonClick event
    • Add bunch of items to listbox
    • Update the first control on the form
    • Process mouse up on close box
    • Dispose the form
    • Application.Run
    • Process mouse up on button
    • Fire ButtonClick event
    • Add some items to listbox
    • Call Application.DoEvents to make it look more responsive
    • Process mouse up on close box
    • Dispose the form
    • Add remaining items to listbox 
    • Update the first control on the form  -- BOOM!  Controls collection has been cleared!
    • BackgroundWorker Component
    • Uses Delegates under the covers
    • Sites to Research
      • http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker(vs.80).aspx
      • http://www.codeplex.com/ExerciseBackgroundW
      • http://www.codeguru.com/columns/vb/article.php/c10755/
    An Easier Way
    • Synchronization problem solved in VS 2005 (and later) by only allowing the main UI thread to access UI controls.
    • Worker threads must tell the UI thread to make a change on their behalf via a delegate.
    Synchronising User Interface
    • Makes life easy in .Net!
    • No complex code needed only need to implement a few methods:
      • DoWork
      • RunWorkCompleted
      • ProgressChanged
    Background Worker Component
  • The One Golden Rule
    • When Multiple threads access a shared resource, only 1 thread can update it at a time.
    • Otherwise we get:
    • Deadlocks
    • Race Conditions
    • Correct use of Synchronisation prevents these situations
    The One Golden Rule
    • Synchronisation involves controlling access to a shared resource so only one thread can access the resource at a time.
    • Resources include:
    • Variables e.g. Arrays
    • User Interface Controls e.g. Progress Bar
    Synchronization & Shared Resources
    • The purpose of the synchronization model implemented by this class is to allow the internal asynchronous/synchronization operations of the common language runtime to behave properly with different synchronization models
    • This model also simplifies some of the requirements that managed applications have had to follow in order to work correctly under different synchronization environments.
    SynchronisationContext Class
    • Windows WorkFlow (WF) parallel task execution
      • WF is explcitly designed for long-running tasks
    • PLINQ (Parallel Linq) .Net 4.0
    • UI Control Virtual Mode
      • You control how the data is retrieved into the control when it needs to display
      • Use this if you have a 1 million rows in memory, but can only display 50 at a time (similar to paging in a webform)
    Other .Net multi-threading Technologies and Concepts
    • SSW Link Auditor
    A Real World Example
    • Threading was used to achieve
      • A responsive UI throughout
      • Optimize the scanning process
    • Techniques that we used
      • Delegates and Callbacks
      • ManagedThreadPool class
    Link Auditor
    • Threading in .NET
      • http://www.albahari.com/threading/
    • Tip! - Keyboard shortcuts
      • http://www.itscodingtime.com/post/Visual-Studio-2010-Keyboard-Mouse-Shortcuts.aspx
    • LINQ to SQL vs LINQ to Entities
      • http://stackoverflow.com/questions/2443836/what-is-the-differnce-between-linq-to-entities-linq-to-sql-and-linq-to-data
      • http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx#WhyLINQtoEntitiesNotSQL
    Resources - N-Tier apps
    • .NET
    • C#
    • Visual Studio 2010
    • LINQ
    • Databinding
    • UX (Usability)
    • Winforms – Best practices
    • N-Tier applications
    • Deployment – Clickonce
    • Security
    • Web services
    • WCF
    • Threading
    Part 1 - Review
  • Lab
    • Sign up on http://www.aspnet4.de/
      • Yes its german!
      • But free ASP.NET 4 hosting with SQL Server backend
      • Use http://translate.google.com/
    • ASP.NET Quick hit videos
      • http://www.asp.net/learn/aspnet-4-quick-hit-videos/
    Part 2 – ASP.NET  The web!!
  • 3 things…
    • PeterGfader @ssw.com.au
    • http:// peitor.blogspot.com
    • twitter.com/ peitor
    • Thank You!
    • Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA
    • ABN: 21 069 371 900
    • Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105
    • [email_address] www.ssw.com.au