Upload
others
View
17
Download
0
Embed Size (px)
Citation preview
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
.NET Framework 3.0
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
.NET Framework 3.0
Objectives
After completing this unit you will be able to:
Gain High Level overview of .NET 3.0
Challenges addressed by the .NET 3.0
Know Core Components of .NET 3.0
Understand .NET 3.0 Programming Model
Build basic Windows applications using WPF - : A
Unified Approach to UI, Documents, and Media
Build basic Work Flow applications using WWF.
Build basic Service Oriented Applications using
WCF.
Understand Infocard – Identity Metasystem
Summarize additional important new features
introduced in .NET 3.0.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Overview of .NET Framework 3.0
The Microsoft .NET Framework 3.0 (formerly
WinFX), is the new managed code programming
model for Windows.
.NET Framework 3.0 includes .NET Framework 2.0
API’s along with four new Windows Presentation
Foundation, Windows Communication Foundation,
Windows Workflow Foundation, and Windows
CardSpace.
Windows Presentation Foundation (WPF, formerly
―Avalon‖) – The unified programming model to build UI,
graphics, documents and media. Microsoft’s unified
presentation subsystem for Windows.
Windows Workflow Foundation (WF) – New programming
model build on .NET Framework to develop workflow based
applications.
Windows Communication Foundation (WCF, formerly
―Indigo‖) - The unified programming model to build service-
oriented applications on the Windows platform.
Windows CardSpace (WCS, formerly ―InfoCard‖) - Unified
framework for ―identity metasystem‖.
The Microsoft .NET Framework 3.0 is an extension to
the existing .NET Framework 2.0 with new set of
API’s in Presentation, Data, Communication and
Identity.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Overview of .NET Framework 3.0
.NET 3.0 is an integral part of the Vista Operating
system. It ships as part of the Windows Vista
operating system.
It is also available separately for the operating systems
Windows Server 2003; Windows Server 2003 R2 Standard
Edition (32-bit x86); Windows Server 2003 R2 x64 editions;
Windows Server 2003 Service Pack 1; Windows Server 2003
x64 editions; Windows Vista; Windows XP Service Pack 2.
.NET Framework 3.0 coexists along with 2.0, 1.1 and 1.0.
.NET Framework makes use of CLR 2.0!
WPF, WCF, WF and Windows CardSpace are all
technologies exposed through the.NET Framework 3.0 APIs.
.NET Framework 3.0 is a natural evolution to existing
framework to provide more set of features to solve
the challenging problems of today world.
Ok, wait…. you may wonder “I am already
convenient with .NET Framework 1.1 and am
learning 2.0, why should I burden with one more!”
Do I really need to learn .NET 3.0? YES, if any of the
following points in the next slide applies to you!
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Benefits of .NET Framework 3.0
Differentiated User Experience (WPF / Avalon)
Programming model to build rich, attractive UI applications
user experiences that incorporate UI (2D, 3D, Vector-based
graphics), media (audio, video, and animation) and
documents.
Looking forward a Unified Framework to build varied User
Interfaces to target both Windows client and Web browsers.
Increase Developer-Designer Productivity.
Want to deliver an enhanced User Experience (UX).
Service-Oriented Application Development (WCF /
Indigo)
Programming model to develop better connected systems
starting from enterprise SOA applications to P2P
applications.
Programming model to build interoperable WS-* Web
services, provides rich communication options (HTTP, TCP,
Named pipes, etc), different message exchange patterns (one-
way, two way, request/response), and with a wide range of
enterprise features (security, reliability, scalability, etc).
Build Service-Oriented Applications.
Want to build Interoperate using Web Services Standards.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Benefits of .NET Framework 3.0
Business Logic Modeling (WWF)
Programming model to build efficient long running,
workflow-based applications to automate business process.
Build a Range of Workflow Styles.
Implement Business Process in Software.
Digital Identity Management (Card Space/ InfoCard)
Provides safe, simple secure way to login to Web sites
(alternative to using usernames and passwords) to protect
against identity-related attacks such as phishing. No need to
memorize the big list of User names / Passwords!
Simplify Online Authentication.
Increase Web Site Security.
Protect Against Identity Theft.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Core Components of .NET 3.0
The five components that composite .NET
Framework 3.0: the .NET Framework 2.0, Windows
Presentation Foundation, Windows Workflow
Foundation, Windows Communication Foundation,
and InfoCard.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Setting up the Environment
Before deep dive into the core concepts and demos,
let’s quickly see the environment setup for WinFX
development. Need to install the following
components:
1. Microsoft .NET Framework 3.0 Redistributable
Package
The Microsoft .NET Framework version 3.0 redistributable
package installs the common language runtime and
associated files required to run applications developed to
target the .NET Framework 3.0.
2. Microsoft Windows Software Development Kit for
Windows Vista and .NET Framework 3.0 Runtime
Components
The Windows SDK includes content for application
development with the APIs in Windows Vista, including the
.NET Framework 3.0 technologies
3. Visual Studio 2005 extensions for .NET
Framework 3.0 (WCF & WPF), November 2006 CTP
The Visual Studio 2005 extensions for.NET Framework 3.0
(WCF & WPF), November 2006 CTP provides developers
with support for building .NET Framework 3.0 applications
using the released version of Visual Studio 2005.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Setting up the Environment
4. Visual Studio 2005 extensions for .NET
Framework 3.0 (Windows Workflow Foundation)
This installs the Visual Studio Designer and WWF Debugger.
Windows SDK is the core component for the WinFX
development which installs the Documents, Samples
and Developer Tools:
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WPF Overview
WPF is the new presentation subsystem for Windows.
Unified programming model to build smart windows client
applications including UI, Documents, and Media (audio,
video, sound, animation, 2D, 3D, speech etc).
Presentation Foundation Services
User Interface Services: Application Services, Deployment,
Controls, Layout, Data Binding
Base Services: XAML, Property System, Input and Eventing,
Accessibility.
Media Services: 2D, 3D, Audio, Video, Text, Imaging,
Animation, Effects, Composition Engine.
Document Services: XPS Documents, Open Packaging
Conventions.
Right now we have island of technologies to develop
rich smart windows applications having
differentiated User experience.
For example, to develop User Interface we need to use
Windows Forms or USER32.
Present GDI and GDI+ is thin wrapper to Win32 API’s and
targeting 2D graphics.
To incorporate 3D, we need to use Direct 3D or OpenGL.
To incorporate media, we need to use DirectShow.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WPF Overview
WPF provides all the above features as single
technology with out changing our gears between the
diverse choices of different technologies.
One single technology for incorporating variety of features
starting from UI, data binding, Style, Animation etc,
regardless of whether we are targeting 2D, 3D or text
content.
Integrated, vector-based composition engine
Utilizing the power of the PC and the powerful Graphical
Processing Units.
The rendering architecture uses Direct3D for all output.
WPF takes advantage of hardware capabilities using a
DirectX-based model. If required hardware is not available, it
will make use of software rendering.
WPF provides Vector based rendering, rather than bitmap-
based, supports high-dpi, double buffering using powerful
composition engine.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WPF Overview
Ease of deployment
WPF provides the best features of both Web (Broad Reach,
Easy change maintenance, Easy Deployment) and Windows
(Rich User Experience, Developer Productivity,
Responsiveness) world through smart client Click Once
technology.
WPF supports both stand alone and web-browser
applications.
Web-browser applications run in a partial trust sandbox for
security. Web-browser applications also make use of the
local client hardware and use 3D and media services for the
richest Web experience.
Windows Presentation Foundation also provides a
new set of document and print technologies.
WPF builds on fixed documents by defining a new type of
document, known as an XML Paper Specification (XPS)
document.
XPS is an open, cross-platform document format that enables
users to create, share, print, and archive paginated
documents.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Declarative Programming – XAML
Declarative programming - XAML
Windows Presentation Foundation introduces XAML
(eXtensible Application Markup Language), an XML-based
way for instantiating and populating nested object hierarchies
to write user interfaces.
Actually XAML internally maps XML markup tags to
objects in the .NET Framework. End of the day, XAML gets
compiled into an object definition.
During runtime, it instantiates each of the elements and nodes
described in the XAML document and creates an equivalent
object model in memory.
XAML is not Avalon, and Avalon is not XAML! XAML is
just another declarative XML based language (Similar to C#,
VB .NET etc) to build UI in Windows.
For example, a XAML code which has a Text block and a
Button.
<StackPanel
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/
presentation
xmlns:x="http://schemas.microsoft.com/winfx/2006/xa
ml">
<TextBlock> Hello, World! </TextBlock>
<button> Click me, If you can! </button>
</StackPanel>
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Developer - Designer collaboration
Developer - Designer collaboration with the introduction of
the XML Application Markup Language (XAML).
Trust me! Mostly Good Developer can’t be a Good
Designer!
With XAML, the developer-designer productivity and
collaboration increases with the usage of tools like Visual
Studio 2005 (Developer) and Expression Interactive Designer
(Designer).
XAML act as a communication channel between designers
and developers!
With introduction of XAML, Designer can use tools like
Expression "Sparkle" or any third-party tools including ZAM
3D, Mobiform Aurora etc to design user interfaces including
2-D and 3-D graphics, video and animation in XAML format
Developers consume the designer generated XAML with
their code for better UI.
Therefore one can split off presentation UI and client-side
logic in the same way as with a Web application.
Interoperability between Windows Presentation
Foundation and Win Forms applications.
You can host WPF controls in a Windows Forms application,
and a Windows Forms control in a WPF application to
leverage the existing code base and to facilitate incremental
approach for migration.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Which one to choose for UI?!
Now we have a set of technologies like Windows
Forms, ASP.NET, DirectX, WPF etc for building user
interfaces. Which one should I use?
Windows Forms – Better for building smart client application
which didn’t need high end graphics, rich differentiated User
experience.
ASP.NET/Atlas – If you want to build dynamic thin client
web applications.
DirectX - Highest-intensity graphics applications (Mainly for
developing Games!)
WPF – Better for building differentiated User Interfaces
which require features like 2-D, 3-D graphics, video,
animation, media, complex data visualization etc
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World (C# Code) – WPF Demo
Once you installed the Windows SDK, you can see the
templates for WPF and WCF in the Visual Studio
2005 as shown in the following figure.
The template has following four project types:
Windows Application (WPF) - A project for creating an
application with a .NET Framework 3.0 user interface.
XAML Browser Application (WPF) - A project for creating a
.NET Framework 3.0 application that runs in a web browser.
Custom Control Library (WPF) - A project for creating
custom controls for use in .NET Framework 3.0 applications.
WCF Service Library (WCF) - A project to define and
implement a WCF service library.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World – WPF Demo
By default, the Windows Application (WPF) project
contains one XAML file and a XAML.cs file as show
in the figure below.
Using both markup and code-behind is the best approach, as
one can take advantage of both worlds.
Similar to an ASP.NET Web application ―ASPX‖, XAML
markup is used to design the appearance of the application's
user interface (UI).
C# or VB.NET code to implement the application's behavior.
You can add controls and layout from the Tool box to
compose the application UI.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
A Window in XAML+C#
Let’s implement a window by using both markup and
code-behind.
<Window x:Class="HelloWorldWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xa
ml" Title="HelloWorld" Height="175" Width="360" >
</Window>
using System.Windows;
public partial class HelloWorldWindow : Window
{
public HelloWorldWindow()
{
InitializeComponent();
}
}
In the above markup code, the Window element must include
the x:Class attribute, which instructs compiler to create a
partial class for the markup file when the project is built with
the name specified by the x:Class attribute.
Partial class is to split the definition of a class or a struct, or
an interface over two or more source files.
Each source file contains a section of the class definition.
While compiling the application, the partial classes will get
compiled into one single class file.
The partial generated partial class implements
InitializeComponent, which is called to register the events
and set the properties that are implemented in markup.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
A Window in XAML+C#
The markup code also requires the addition of an XML
namespace declaration for the XAML schema
(xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml").
In code-behind, the class must be a partial class with the
same name that is specified by the x:Class attribute in
markup, and needs to derive from Window.
In code-behind, the class must implement a constructor that
calls the InitializeComponent method; otherwise the markup
will not be applied.
Let’s implement a window with a button by using
markup and to handle the click event in the code-
behind.
XAML CODE:
<Window x:Class="HelloWorldWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml
/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xa
ml" Title="HelloWorld" Height="175" Width="360" >
<Button Click="button_Click" Height="75"
Width="60">Click Me!</Button>
</Window>
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
A Window in XAML+C#
using System.Windows;
public partial class HelloWorldWindow : Window
{
public HelloWorldWindow()
{
InitializeComponent();
}
void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello World.");
}
}
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
XAML browser applications (XBAPs)
XAML Browser Applications run in an IE sandbox
with "Internet Permissions".
XAML browser applications (XBAPs) provide the
advantage of both web applications and rich-client
applications.
XAML browser applications (XBAPs) have access to the rich
features of the Windows Presentation Foundation (WPF)
runtime. In addition, XAML browser applications can be
deployed in a web server similar to a web application.
To build XBAPs, we need to use the subset of WPF
elements that is compatible with Internet zone
permissions.
XAML browser applications (XBAPs) can be deployed from
a server with Microsoft Internet Information Services (IIS) or
later.
It is not necessary to have the WPF runtime on the server to
deploy WPF applications from it. However, it requires the
WPF Multipurpose Internet Mail Extensions (MIME) types
and file extensions to be registered.
In the above page, you can see the same Hello World
application running in IE.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WWF Overview
Windows Workflow Foundation (WF) is a
programming model, engine and tools for defining,
building, executing, debugging and managing
workflows on Windows.
WWF consists of namespace (System.Workflow), an in-
process workflow engine, and designers for Microsoft Visual
Studio 2005.
Windows Workflow Foundation is a programming
framework to build workflow enabled applications.
System.Workflow.ComponentModel is the name space in
which most of the abstractions used in WWF are defined.
A workflow is a set of activities that are stored as a
model.
A workflow is a set of activities that are stored as a model.
Workflow is nothing but a set of activities that are organized
in a tree structure. Activities are the fundamental building
blocks of workflows.
Workflow instance is created and maintained by an in-
process workflow runtime engine.
The Workflow runtime engine is responsible for creating and
maintaining running workflow instances.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WWF Overview
Core Components of WWF:
Base Activity Library: Out-of-box activities which provides
functionality for control flow, conditions, event handling,
state management and invoking web service. One can build
their own custom domain-specific activities using the base
activity.
Runtime Engine: Workflow execution and state management
Runtime Services: Hosting flexibility and communication
Visual Designer: Graphical and code-based construction
Workflows run within a Host Process.
Once a workflow model is compiled, it can be executed
inside any Windows process including console applications,
Windows Forms applications, Windows Services, ASP.NET
Web sites, and Web services.
Traditional programming languages won’t support
program durability, persistence and long running
process. Windows Workflow Foundation is flexible,
extensible and supports long running process.
Work Flow also separates business logic from the
code. Developers can build their own custom
activities and reuse the same.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WWF Overview
A workflow can be expressed in either declarative
XAML or in imperative code using any .NET
language like C# and VB.NET.
You can build workflows directly in code, in markup, or in a
combination of both. Pure XAML has an advantage of
loading into memory with out compilation.
Work flow application can be authored in three ways
including Code-only (C# and VB.NET), Code-separation
(XAML+ C#/VB.NET) and No-Code (XAML).
Visual workflow designer to design workflows, a
visual debugger to debug the workflow designed and
project system to compile their workflows inside
Visual Studio 2005.
WWF supports three kind of workflows including
sequential, state-based and data driven workflows
involving both human and system interaction.
Sequential workflow is a map of activities with a Start and an
End, in series or parallel.
We can use Sequential work flow, if activities are predictable
and repeatable.
State-based workflow is a state machine where events cause
transitions.
In data-driven workflow, activities are run based on a data
state.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
“Hello World” WWF
To start with let’s create a very simple “Hello World”
sequential workflow using the Visual Studio 2005
designer for Windows Workflow Foundation.
The Hello World workflow includes a single code activity.
The code activity will be used to write the phrase ―Hello,
World!‖ to the console at runtime.
In the New Project dialog window, expand Visual C# |
Workflow in the Project Types tree on the left side.
Select the template named ―Sequential Workflow Console
Application‖.
In Visual Studio 2005, select the File | New | Project menu
command.
The project template has a Workflow1.cs. Remove the same
and Right click on the HelloWorld Workflow project in
Solution Explorer and select Add |New Item from the context
menu.
In the Add New Item dialog window, select the item template
named Sequential Workflow (with code separation) and
clicks the Add button.
The new Sequential Workflow project will now
contain two files: Workflow1.xoml and
Workflow1.xoml.cs. Workflow1.xoml contains the
XML markup that represents the workflow type.
Workflow1.xoml.cs contains the code-beside class for
the workflow.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
“Hello World” WWF
Double click on the Workflow1.xoml file in Solution
Explorer to open the Visual Studio workflow designer.
In the workflow designer, select the View | Toolbox menu
command to display the toolbox with available activities.
Select the Code activity from the toolbox. Drag and drop the
Code activity to the design surface for the Sequential
Workflow.
Notice the red exclamation mark on the new Code
activity. Click the exclamation mark to view the
smart tag and the reason for why it’s being displayed.
Selecting the smart tag will cause the Properties tool window
to be displayed with the ExecuteCode property highlighted.
Enter the value code1_CodeHandler for the ExecuteCode
property and press enter. This will cause a new method or
handler to be added to the code-beside class for the
workflow.
Visual Studio will automatically open the
Workflow1.xoml.cs code file and display the new
code1_CodeHandler method.
The code-beside class is a partial class that inherits from the
SequentialWorkflow base class. This code-beside class will
have the same name as the workflow. You should have the
following code in your code-beside class:
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
“Hello World” WWF
Insert the following code in the code1_CodeHandler
method to output the string “Hello, World!” to the
console
Open the Visual Studio workflow designer by double
clicking on workflow1.xoml in the solution explorer.
Click on the code1 activity to select it and set a breakpoint on
it. To set the breakpoint right-click on the code1 activity and
choose Breakpoint | Insert Breakpoint.
You should now see a red circle to the left of the code1
activity.
For this exercise we are using the simple host in
program.cs that comes with the project template that
was selected.
Alternatively, you could build your own workflow
host application by using the workflow runtime APIs
defined in the System.Workflow.Runtime namespace.
Compile and run the solution under the Visual Studio
debugger by pressing F5 (or selecting the Debug | Start
Debugging menu command).
The ConsoleTestHost will start a workflow instance and that
instance will break in the debugger when it gets to the code
activity. The yellow box indicates the activity that the
debugger is stopped at.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
“Hello World” WWF
Choose Debug | Step Into a couple of more times and you
should see ―Hello, World!‖ written out into the test host’s
console window.
Windows Workflow Foundation provides a command-line
workflow compiler named wfc.exe.
wfc.exe <Xoml file list> /target:assembly [<vb/cs file list>]
[/language:...] [/out:...] [/reference:...] [/library:...]
[/debug...] [/nocode...] [/checktypes...] [/resource:<resource
info>]
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
\
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WCF Introduction
One of the biggest challenges / opportunities in the
industry is the integration and reuse of the existing
systems.
Connectivity (Connect people, organizations, and existing
systems) through connected applications becomes the key
factor in the software development.
Solution to this challenge lies on developing interoperable,
secure distributed application using Service Orientation.
For the last few years, Web Services assist the developers to
build distributed applications.
The Windows Communication Foundation (WCF),
Microsoft’s next generation Web services technology,
simplifies development of connected applications
through a new service-oriented managed
programming model.
WCF takes Web services to the next level by providing
developers with a highly productive framework for building
secure, reliable and interoperable applications that send
messages between services and clients.
Windows Communication Foundation (formerly code-named
"Indigo") is a set of .NET technologies which for building
and running connected systems.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WCF Key Features
WCF is the Microsoft service-oriented
communication infrastructure and programming
platform and runtime system for building,
configuring and deploying network-distributed
services.
WCF is implemented in managed code as an extension of the
Microsoft .NET Framework 2.0.
WCF provides key characteristics like Transport Neutral
(Http, TCP, MSMQ and Named Pipes), various security
features, multiple messaging patterns (simplex, duplex and
request reply), encodings (text, binary or MTOM), network
topologies, and hosting models (Windows, Windows NT
Service, Console and IIS).
WCF Key Features
Unification of existing .NET Framework communication
technologies like ASMX, Remoting, Enterprise Services,
WSE, MSMQ etc...
Implementing latest Web Services Standards / WS*
Specifications and to support interoperability, including
reliability, security, and transactions.
Implicit use of service-oriented development principles.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Unified Programming Model
Today we had distributed stacks of technologies like
ASMX, WSE, Remoting, System.Messaging,
Enterprise Services etc…
AMX (ASP.NET Web Services) – To achieve cross-vendor
interoperability (Interoperable, ASP.NET Integration)
.NET Remoting - Tightly coupled .NET-to-.NET
communication to achieve performance (Extensible and CLR
Integration)
Web Services Enhancements (WSE) – To implement WS-*
specifications along with ASMX
System. Messaging – Communication with Windows-based
applications that require guaranteed data delivery. (Queuing,
Reliable Messaging and MSMQ Integration),
Enterprise Services - This handles things like components,
activation, transactions and so on.
Developers have had to use multiple technologies to
build connected systems.
Each stack has its own strengths and once you choose one
technology, it is difficult to switch gear to another one.
WCF combines and extends the functionality of existing
Microsoft technologies (ASMX, .NET Remoting, .NET
Enterprise Services, Web Services Enhancements, and
System.Messaging) to deliver a single, highly-productive
development framework that improves developer
productivity.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Unified Programming Model
Indigo provides all these features through one unified
programming model including
Message oriented programming (System. Messaging)
Implement WS* specifications (WSE)
Achieve Extensibility and performance (Remoting)
Service oriented programming and Interop (ASMX)
Attribute based programming, object life time management,
distributed transactions (Enterprise services).
Wondering how? Will see one by one!
ASMX
Interoperable
Web Services x
.NET –.NET
Communicationx
Support for WS-*
Specificationsx
.NET
Remoting
xQueued
Messaging
Distributed
Transactions, etc.x
Enterprise
ServicesWSE MSMQ WCF
x
x
x
x
x
WCF
x
x
x
x
x
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Interoperability with Applications
Right now, XML Web services provide support for
basic interoperability between applications running
on different platforms.
WCF delivers secure, reliable, transacted interoperability
through built-in support for the WS-* specifications.
For developers, this greatly reduces the amount of
infrastructure code required to achieve interoperability along
with other enterprise features.
An application built on WCF can communicate with
WCF-based applications running in a different
process on the same Windows machine, WCF-based
applications running on another Windows machine
and applications built on other technologies, such as
IBM WebSphere, BEA WebLogic, and other Web
services built in J2EE that are standards compliant.
These applications can be running on Windows machines or
on machines running other operating systems, such as Sun
Solaris, IBM z/OS, or Linux.
WCF support various specifications/standards, including
basic standards (XML, XSD, XPath, SOAP, WSDL) as well
as advanced standards and specifications that comprise the
WS-* architecture. These include: WS-Addressing, WS-
MetadataExchange, WS-Policy, WS-Security, WS-Trust,
WS-SecureConversation, WS-ReliableMessaging, WS-
AtomicTransaction, WS-Coordination, WS-Policy, and
MTOM.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Support for Service-Oriented
Development
Building adaptable solution which is flexible enough
to the fast moving business and technological change
is one of the biggest challenges faced by software
industry.
WCF is the programming model build up from scratch for
building service-oriented application development.
WCF completely supports the four tenants of Service
Orientation.
Boundaries are Explicit
Services are Autonomous
Services share schema and contract, not class
Service compatibility is determined based on policy
Applications based on the above four tenants provide
benefits in maintainability, reusability, and
manageability of connected systems.
Service-oriented development complements object-
oriented (OO) development. Still Object-oriented
concepts will be used to implement internal design of
services.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
WCF Fundamentals
Before switch to Programming gear, let’s have a
quick look on some fundamental concepts.
WCF programs communicate through the exchange
of messages.
A message is a self-contained unit of data consists of header
and a body.
A message should contain one body and any number of
headers.
All Indigo messages are represented as XML, specifically
SOAP envelopes containing XML Infosets.
There are three types of Messaging programs:
Clients, Services and Intermediaries.
Client program initiate and send the request message to
Service program.
Service program respond to the client message. The input
message may cause the service to perform some action like
code execution or reply back to the client with an output
message.
Intermediary is a program between Service and Client which
do some tasks like routing, acting as gateway, monitoring
etc…
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
ABC’s of WCF
Out of the box, WCF provides the following
encodings:
Text encoding, an interoperable encoding.
Message Transmission Optimization Mechanism (MTOM)
encoding, this is an interoperable way for efficiently sending
unstructured binary data to and from a service.
Binary encoding for efficient transfer.
One can have more encoding mechanisms like a
compression encoding using the built-in extension
points of WCF.
WCF supports the following messaging patterns
Simplex – One Way Messaging (Fire and Forget).
Duplex – Asynchronous two-way messaging. (Ex: Remotely
controlled robot).
Request: Reply – Synchronous two-way messaging. (Ex:
XML Web services)
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Internally
Internally, Indigo service contains contracts,
bindings, endpoints and implementation code.
Address: Where is the service?
An address defines where messages can be sent. The
format of an endpoint address depends on the
transport mechanism.
For instance, for HTTP Transport it looks like
http://www.objectinnovations.com:8080/nf3
For TCP, it looks likes
net.tcp://www.objectinnovations.com:9000/nf3
The endpoint address enables you to create unique endpoint
addresses for each endpoint in a service.
Binding: How do I communicate to the service?
Binding provides information on how a service can be
accessed including transport method (HTTP /
HTTPS / TCP / Named pipe / MSMQ), encoding
format (Text / Binary /MTOM), security mechanism,
reliability requirement etc...
A service to be accessible, at least it should be associate with
either one or more bindings.
A contract can support many bindings and a binding can
support many contracts.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Internally
Out of the box, Indigo support following bindings
which covers most of the common scenarios.
Developers can create their own custom binding based on
their requirement with the mix of functionality.
Contract: What can the service do for me?
A contract explains a service’s behavior, structures, or
message formats.
There are three types of contracts in Indigo Services
including Service contracts, Data contracts and Message
contracts.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Internally
You define service contracts, data contracts, and
message contracts by using declarative attributes.
The class that needs to be exposed as a WCF service
should be marked with ServiceContract attribute.
Service contracts can be defined by annotating an
interface with [ServiceContract].
Identify service operations by annotating methods
with [OperationContract].
A service contract defines service-level settings, such
as the namespace of the service, a corresponding
callback contract, etc.
In comparison to object oriented programming, the service
contract is nothing but an interface.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Internally
All methods that need to be invoke using SOAP message
should be marked with OperationContract attribute.
An operation contract defines the parameters and return type
of an operation.
A WCF service can have multiple service contracts.
For instance, a service contract looks as follows:
[ServiceContract]
public interface IBook
{
[OperationContract]
bool BookAvailable(string bookName, string
authorName, int bookID)
[OperationContract]
bool AddCart(Book book)
[OperationContract]
void OrderBook(int bookID)
}
By default service contracts handle simple service
calls. Data contract defines custom data structures
like object or struct.
If the service is using only simple types, there is no
need to explicitly use data contracts. In Data
Contracts, the data can be passed to and from
services in XML Schema form.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Internally
For instance, a data contract looks as follows:
[DataContract]
public class Book
{
[DataMember]
public string bookName;
[DataMember]
public string authorName;
………
}
Contrast to ASMX Web services, in WCF you need to
decorate a class with Data contract attributes, to
allow serialization.
Data Contract specifies how the data is serialized and
de-serialized.
A message contact describes the format of a message.
For example, it declares whether message elements
should go in headers versus the body, what level of
security should be applied to what elements of the
message, and so on.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Externally
Externally, Indigo service exposes a service
description (WSDL, WS-Policy) and one or more
endpoints, with each endpoint exposing one or more
service operations.
An endpoint connects a contract and a binding with
an address. At least one endpoint needs to be there to
access that service. A service can have multiple
endpoints.
For instance,
Endpoint1 = Address1
(http://www.objectinnovations.com/WCF/) + Binding1 (Http
/ X.509 Cert / MTOM Encoding) + Contract1 (IContract1
interface)
Endpoint 2 = Address 2
(net.tcp://www.objectinnovations.com/WPF/) + Binding2
(TCP / Windows Security / Binary Encoding) + Contract2 (IContract2 interface)
Endpoint 3 = Address3
(net.msmq://www.objectinnovations.com/WF/) + Binding3
(MSMSQ / Binary Encoding) + Contract2 (IContract2 interface)
A service can have many endpoints (contract bound to
address) coexisting and available at the same time.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Service – Externally
If you want to expose your service via HTTP and use
SOAP 1.1 for maximum interoperability, and also
want to expose it via TCP using a binary wire
encoding for maximum performance, the two
resulting endpoints can reside side-by-side on top of
the very same service!
Endpoints: Addresses, Bindings, and Contracts!
An endpoint consists of four properties:
An address that specifies where the endpoint can be found.
A binding that specifies how a client can communicate with
the endpoint.
The contract specifies what functionality the endpoint
exposes to the client.
A set of behaviors that specify local implementation details
of the endpoint.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Indigo Programming Model
Indigo programming involves the use of an object
model, declarative attributes, and configuration
settings.
Declarative programming. Usually Declarative attributes are
used to define contracts and specify service behaviors.
Imperative programming, in which you work with the Indigo
object model in code.
Configuration-based programming, in which you specify
behavior in application configuration files. Configuration-
based development helps to modify behaviors like addresses,
bindings, security details, service behavior, tracing etc
without recompile the program code.
It’s not mean that you can do three different way of
Indigo Programming. It is to say that WCF provide
features to do few things in more than one way.
For instance, An endpoint defined in a configuration
file:
<endpoint
address="http://localhost:8000/MyService/"
bindingSectionName="wsProfileBinding"
contractType="IMyContract" />
The same endpoint definition in code:
serviceHost = new ServiceHost<MyService>();
serviceHost.AddEndpoint(typeof(IMyContract),
new WSProfileBinding(),
"http://localhost:8000/MyService/");
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Service
Let’s first see, how to develop a simple WCF service
which simply returns “Hello World” when client
connects to it.
Service programs contain four elements: Contract
definitions, implementation code, hosting code and
Endpoint definitions.
namespace HelloService
{
class Infrastructure
{
static void Main(string[] args)
{
// A Uri, which represents the address of the
service.
Uri baseURI = new
Uri("http://localhost/helloworld");
ServiceHost HelloWCF = new
ServiceHost(typeof(HelloService), baseURI);
//Binding (BasicHttpBinding) provides how to
communicate with the endpoint.
HelloWCF.AddServiceEndpoint(typeof(HelloService),
new BasicHttpBinding(), baseURI);
HelloWCF.Open();
Console.WriteLine("Hello World Service Started.");
Console.ReadKey();
HelloWCF.Close();
}
}
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Service
[ServiceContract]
class HelloWorld
{
[OperationContract]
string Hello()
{
return ("Hello World!");
}
}
}
The [ServiceContract] attribute specifies that class will be
exposed as a WCF service.
The attribute [OperationContract] specifies that the Hello()
function which returns the string ―Hello World!" will be
exposed as a method on that service.
The class Infrastructure contains the basic infrastructure
including "ServiceHost" which provides the hosting
infrastructure for the service and the endpoint definition.
BasicHttpBinding uses HTTP as the transport for sending
SOAP 1.1 messages.
A service can use this binding to expose endpoints that
conform to WS-I BP 1.1, such as those that ASMX clients
consume.
Both classes and interfaces can be used to define a WCF
service contract. It’s better to use Interfaces, because they
directly model service contracts.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Service
Now, let's move the endpoint definition of the
“HellowWorld” service from the code to the config
file.
The config file will be read at runtime. Therefore we
can modify the binding details with out rebuild and
restart of the application.
Add a configuration file “app.config” and comment
the code:
HelloWCF.AddServiceEndpoint(typeof(HelloService),
new BasicHttpBinding(), baseURI);
Add the following configuration details in the
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService">
<endpoint address="http://localhost/hello"
binding="basicHttpBinding"
contract="HelloService.HelloService" />
</service>
</services>
</system.serviceModel>
</configuration>
If you run the service now, you will see an error
message “Metadata publishing for this service is
currently disabled.”
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Service
By default, the framework does not expose any
metadata.
To expose the metadata required to generate the proxy, you
must add a <serviceMetadata> Element and set its
httpGetEnabled attribute to True.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService"
behaviorConfiguration="MyServiceTypeBehaviors" >
<endpoint
contract="HelloService.HelloService"
binding="basicHttpBinding"
address="http://localhost/hello"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Client
To test our Service, let’s create a client. A client must
create a proxy, which establishes a channel to the
service.
To generate client code from a service, we need to use
the Svcutil tool. Svcutil not only generates contract
code, but it also provides a proxy class for accessing
the service.
Clients create a new instance of the proxy, and they
can then access the service through the proxy.
svcutil http://localhost/hello?wsdl
Then SvcUtil will get the WSDL file for the service,
download the metadata, and generate the service proxy for
you. Along with client code from a running service it also
generates a config file.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Hello World WCF Client
Add the proxy class and config file in the project and
you can access the service using the following code:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost/hello"
binding="basicHttpBinding"
contract="HelloService" />
</client>
</system.serviceModel>
</configuration>
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Service Configuration Editor
Windows Communication Foundation (WCF) Service
Configuration Editor enables administrators and
developers to create and modify configuration
settings for WCF services using a graphical user
interface (GUI).
With this tool, you can manage settings for WCF
bindings, behaviors, services, and diagnostics without
having to directly edit XML files.
The file SvcConfigEditor.exe present in the location:
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Service Trace Viewer
Windows Communication Foundation (WCF) Service
Trace Viewer helps you analyze activity tracing
(*.svclog) diagnostic traces that are generated by
WCF listeners.
Service Trace Viewer provides a way to easily merge,
view, and filter trace messages so that you can
diagnose, repair, and verify WCF service issues.
To configure tracing for WCF messages, use Service
Configuration Editor (SvcConfigEditor.exe).
Try changing the binding to wsHttpBinding from
basicHttpBinding in config file. You can see
encrypted CipherValue rather than plain “Hello
World” in the trace.
<s:Body>
………
<t:KeySize>256</t:KeySize>
<t:BinaryExchange
ValueType="http://schemas.xmlsoap.org/ws/2005/02/tr
ust/spnego" EncodingType="http://docs.oasis-
open.org/wss/2004/01/oasis-200401-wss-soap-message-
security-
1.0#Base64Binary">YG0GBisGAQUFAqBjMGGgJDAiBgorBgEEA
YI3AgIKBgkqhkiC9xIBAgIGCSqGSIb3EgECAqI5BDdOVExNU1NQ
AAEAAAC3shjiBwAHADAAAAAIAAgAKAAAAAUCzg4AAAAPVi1ESkF
DT0JGQVJFQVNU</t:BinaryExchange>
</t:RequestSecurityToken>
</s:Body>
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Rules for smooth migration to WCF
For smooth migration to WCF from existing
technologies, follow the below basic rules:
Build services using the ASP.NET (ASMX) Web service
model.
Enhance your ASMX service with WSE if you need the WSE
feature set (including end-to-end security).
Use object technology in a service’s implementation.
Use System.Messaging if you need the reliable messaging
and queuing features in MSMQ.
In ASMX, avoid or abstract using low-level extensibility
such as the HTTP Context object.
In .NET Remoting, avoid or abstract using low-level
extensibility such as .NET Remoting sinks and channels.
In Enterprise Services, avoid passing object references inside
of ES.
Do not use COM+ APIs—instead, use
System.EnterpriseServices.
Do not use MSMQ APIs—instead, use System.Messaging.
G. Gnana Arun Ganesh – http://arunmvp.blogspot.com
Rev. 1.0.0 Copyright © 2010 – Arun Ganesh
Example – Example Title