advertisement

Web services, WCF services and Multi Threading with Windows Forms

50 %
50 %
advertisement
Information about Web services, WCF services and Multi Threading with Windows Forms
Education

Published on August 27, 2010

Author: PeterGfader

Source: slideshare.net

Description

What are Web Services?
WCF Services
Consuming Web Services
Threading
Do the lab
Review
advertisement

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?

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?

N-Tier Application

Deployment

ClickOnce

Security

What are Web Services? WCF Services Consuming Web Services Threading Do the lab Review Session 5: WCF and Multi-Threading

What are Web Services?

WCF Services

Consuming Web Services

Threading

Do the lab

Review

SSA @ SSW Loves C# and .NET (Java not anymore) Specializes in Windows Forms ASP.NET TFS testing Automated tests Silverlight Peter Gfader

SSA @ SSW

Loves C# and .NET (Java not anymore)

Specializes in

Windows Forms

ASP.NET

TFS testing

Automated tests

Silverlight

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?

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

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

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

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

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/

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

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

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

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)

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/

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

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

Built in WCF Service Project

Define the service with ServiceContract

Define methods with OperationContract

Define a contract

Contract ( I Service.cs) It means INTERFACE Define an Interface contract

Contract ( I Service.cs)

It means INTERFACE

Contract Implementation Implement the contract

Contract

Implementation

 

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

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

Creating Consuming Configuring WCF Service Demo (The LAB)

Creating

Consuming

Configuring

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

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 Processes

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

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

System.Threading Background Worker “ Threadsafe” classes (particularly collections) Thread Synchronisation Interprocess communication AppDomain STA (Single Threaded Apartment) Model Threading in .Net

System.Threading

Background Worker

“ Threadsafe” classes (particularly collections)

Thread Synchronisation

Interprocess communication

AppDomain

STA (Single Threaded Apartment) Model

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

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

Single thread apartment (STA) What is STA (and what happened to MTA?)

Single thread apartment (STA)

But MTA was in VB6?

Application.DoEvents System.Threading Thread.Start Delegate.BeginInvoke BackgroundWorker component Threading in WinForms (bad to good)

Application.DoEvents

System.Threading

Thread.Start

Delegate.BeginInvoke

BackgroundWorker component

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

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!

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.

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!

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

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/

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

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.

Makes life easy in .Net! No complex code needed only need to implement a few methods: DoWork RunWorkCompleted ProgressChanged Background Worker Component

Makes life easy in .Net!

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

DoWork

RunWorkCompleted

ProgressChanged

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

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

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

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

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

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.

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

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)

SSW Link Auditor A Real World Example

SSW Link Auditor

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 was used to achieve

A responsive UI throughout

Optimize the scanning process

Techniques that we used

Delegates and Callbacks

ManagedThreadPool class

Threading in .NET http://www.albahari.com/threading/ Tip! - Keyboard shortcuts http://www.itscodingtime.com/post/Visual-Studio-2010-Keyboard-Mouse-Shortcuts.aspx Resources

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

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

.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

.NET

C#

Visual Studio 2010

LINQ

Databinding

UX (Usability)

Winforms – Best practices

N-Tier applications

Deployment – Clickonce

Security

Web services

WCF

Threading

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!!

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/

3 things… PeterGfader @ssw.com.au http:// peitor.blogspot.com twitter.com/ peitor

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

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

Add a comment

Related presentations

Related pages

WCF Windows Forms Sample

The sample consists of a client Windows Forms ... Foundation (WCF) service that ... Windows Forms application (.exe) and a Windows ...
Read more

c# - WCF Service and Threading - Stack Overflow

I have created a simple WCF (.NET 3.5) service which defines 10 ... How can I use multithreading in WCF to ... Windows service / WCF and threading ...
Read more

multithreading - WCF InstanceContextMode.PerCall Services ...

WCF InstanceContextMode.PerCall Services ... I am running a self-hosted WCF Service (it will be a Windows ... Does a Self-Hosted Windows Forms WCF Service ...
Read more

Hosting WCF services in a Windows Forms Application ...

Demonstrates how to host serveral singleton services in a Windows Forms ... Hosting WCF services in a Windows Forms ... service is called, the window form ...
Read more

Windows-Service for Multithreading with Remote Control GUI ...

hi, i roughly read your article that i could understand you control the windows service by wcf from remotely. i have seen that you are using ...
Read more

How to: Host WCF in a Windows Service Using TCP

Create a simple client to consume your service. Overview. WCF services ... WCF service, create a Windows ... Forms application. Step 8: Add a WCF Service ...
Read more

WCF Workflow Service multi-threading issue

WCF Workflow Service multi-threading ... If I call the Windows Service (that hosts the WCF ... if I start 3 instances of my Windows Forms ...
Read more

Multi-threading in Windows Service - C# Corner ...

GDI+ and DirectX Web Services Site Spams ... I have created multithreading inside windows service. ... created 30 windows services and each windows service ...
Read more

A Simple Example of WCF Service - C# Corner - Developers ...

Algorithms in C# Dynamics CRM Machine Learning Robotics & Hardware Web Services ... the Edit WCF Configuration ... in Windows Form Using WCF Service;
Read more