UTSSession5_WCFAndThreading_V5x - SSW

fortnecessityusefulDéveloppement de logiciels

14 déc. 2013 (il y a 3 années et 5 mois)

82 vue(s)

Delivering Awesome Web Applications

Developing Windows
and Web Applications
using Visual Studio
.NET

Session 5:

WCF

and a little Threading

Adam Ezekiel and Sumesh Ghimire

Delivering Awesome Web Applications


What are Web Services?


WCF Services


Consuming Web Services


Threading


Do the lab


Review

Session 5:

WCF and a Multi
-
Threading

Delivering Awesome Web Applications


What are Web Services?


WCF Services


Consuming Web Services


Threading


Do the lab


Review

Agenda

Delivering Awesome Web Applications


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?

Delivering Awesome Web Applications


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

Delivering Awesome Web Applications


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




Public Web Services

Delivering Awesome Web Applications


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

Windows Communication Foundation (WCF)

WCF isn’t just a “web” service

Delivering Awesome Web Applications


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)


Definitions

Delivering Awesome Web Applications


Delivering Awesome Web Applications


Built in WCF Service Project


Define the service with

ServiceContract


Define methods with

OperationContract

Creating a WCF Service

Delivering Awesome Web Applications

Define a contract

Delivering Awesome Web Applications




Contract

(
I
Service.cs)


I means

INTERFACE


Define an Interface contract

Delivering Awesome Web Applications




Contract



Implementation

Implement the contract

Delivering Awesome Web Applications


Web Services

Delivering Awesome Web Applications


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

Delivering Awesome Web Applications


WCF Sites for more info:


http://netfx3.com/content/WCFHome.aspx

Windows Communication Foundation (WCF)

Delivering Awesome Web Applications


Creating


Consuming


Configuring


WCF Service Demo (The LAB)

Delivering Awesome Web Applications

Multithreading in
.Net

Delivering Awesome Web Applications


Why do we need threading?


Blocking calls (e.g. Disk IO, Network) on long running
-
tasks


Responsive User Interface (UI)


Better performance (especially on multi
-
core processors)



Threading

Delivering Awesome Web Applications


Application can spawn one or more processes


Processes have isolation (separation of data, memory and
resources)


Processes contain Threads

Processes

Delivering Awesome Web Applications


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

Delivering Awesome Web Applications


System.Threading


Background Worker



Threadsafe
” classes (particularly collections)


Thread Synchronisation


Interprocess

communication


AppDomain


STA (Single Threaded Apartment) Model

Threading in
.Net

Delivering Awesome Web Applications

1.
Cannot Interact with UI Elements from a thread that it did not
create (the UI object is not accessible to the other thread)

2.
Some classes are not
Threadsafe
, meaning that data may be
overwritten/corrupted (
eg

Data collections)

3.
Cannot predicate the sequence when separate threads will
finish their work


Important Concepts

Delivering Awesome Web Applications


Application.DoEvents


System.Threading


Thread.Start


Delegate.BeginInvoke


BackgroundWorker

component




Threading in
WinForms

(bad to good)

Delivering Awesome Web Applications


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()

Delivering Awesome Web Applications

Application.DoEvents()

FetchData

SearchGoogle()

Return

Merge

Done

Worker 1

SearchYahoo()

Return

Worker 2

UI

Delivering Awesome Web Applications


BackgroundWorker Component


Uses Delegates under the covers


Sites to Research


http://msdn2.microsoft.com/en
-
us/library/system.componentmodel.backgroundworker(vs.8
0).aspx


http://www.codeplex.com/ExerciseBackgroundW


http://www.codeguru.com/columns/vb/article.php/c10755/


An Easier Way

Delivering Awesome Web Applications


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!

Bad use of
DoEvents

GOOD


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



BAD


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!

Delivering Awesome Web Applications

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

Delivering Awesome Web Applications


Makes life easy in
.Net
!


No complex code needed only need to implement a few
methods:


DoWork


RunWorkCompleted


ProgressChanged


Background Worker Component

Delivering Awesome Web Applications


Demo

Delivering Awesome Web Applications




The One Golden Rule

Delivering Awesome Web Applications

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

Delivering Awesome Web Applications

Synchronisation involves controlling access to a shared resource
so only one thread can access the resource at a time.


Resources include:


Variables



eg. Arrays


User Interface Controls

eg. Progress Bar


Synchronization & Shared Resources

Delivering Awesome Web Applications


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

Delivering Awesome Web Applications


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

Delivering Awesome Web Applications