Post on 29-Nov-2014
description
Delivering Awesome Web Applications
Developing Windows and Web Applications using Visual Studio.NET
Peter Gfader
Senior Software Architect
Delivering Awesome Web Applications
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?
Delivering Awesome Web Applications
N-Tier Application
Deployment
ClickOnce
Security
Session 4: Last week?
Delivering Awesome Web Applications
What are Web Services?
WCF Services
Consuming Web Services
Threading
Do the lab
Review
Session 5: WCF and Multi-Threading
Delivering Awesome Web Applications
SSA @ SSW
Loves C# and .NET (Java not anymore)
Specializes in Windows Forms ASP.NET TFS testing Automated tests Silverlight
Peter Gfader
Delivering Awesome Web Applications
Web services
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 More here
• http://www.service-repository.com/
Public Web Services
Delivering Awesome Web Applications
WCF
Delivering Awesome Web Applications
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
Delivering Awesome Web Applications
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”
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
http://prezi.com/tg2kaukw8sez/wcf-intro/
Windows Communication Foundation (WCF)
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
namespace wcfRemoteCalc { // Note this is derived from Calculator.cs public enum FunkType { Unknown , Add , Subtract , Multiply, Divide, SafeDivide } [ServiceContract] public interface IService1 { [OperationContract] int calculate( FunkType aFunction , int aNumb1, int aNumb2 ); } }
Delivering Awesome Web Applications
Contract(IService.cs)
It means INTERFACE
Define an Interface contract
Delivering Awesome Web Applications
Contract
Implementation
Implement the contract
Delivering Awesome Web Applications
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
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)• E.g. Windows Desktop Search
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 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 (e.g. 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.StartDelegate.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
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!
Delivering Awesome Web Applications
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!
Delivering Awesome Web Applications
BackgroundWorker Component
Uses Delegates under the covers
Sites to Research http://msdn2.microsoft.com/en-us/library/system.compone
ntmodel.backgroundworker(vs.80).aspx http://www.codeplex.com/ExerciseBackgroundW http://www.codeguru.com/columns/vb/article.php/c10755/
An Easier Way
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
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 e.g. Arrays
User Interface Controls e.g. Progress Bar
Synchronization & Shared Resources
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
Threading in .NET
http://www.albahari.com/threading/
Tip! - Keyboard shortcuts
http://www.itscodingtime.com/post/Visual-Studio-2010-Keyboard-Mouse-Shortcuts.aspx
Resources
Delivering Awesome Web Applications
LINQ to SQL vs LINQ to Entities http://stackoverflow.com/questions/2443836/what-is-the-di
ffernce-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
Delivering Awesome Web Applications
.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
Delivering Awesome Web Applications
Lab
Delivering Awesome Web Applications
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!!
Delivering Awesome Web Applications
3 things…
PeterGfader@ssw.com.au
http://peitor.blogspot.com
twitter.com/peitor
Delivering Awesome Web Applications
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
info@ssw.com.auwww.ssw.com.au