.NET 4: What's New in the CLR and BCL?

basiliskcanoeΛογισμικό & κατασκευή λογ/κού

2 Νοε 2013 (πριν από 4 χρόνια και 8 μέρες)

113 εμφανίσεις

.NET 4: What’s new in the CLR
and BCL?

Pavel Yosifovich

CTO, Matrix Global

pavely@matrix
-
global.net


http://blogs.microsoft.co.il/blogs/pavely


Agenda


Introduction


What’s new in CLR
4
?


What’s new in BCL
4
?


Q & A


Summary


Related Links




Introduction


.NET Evolution

Year

CLR

.NET

Visual Studio

C#

VB

2002

1.0

1.0

2002

1.0

7.0

2003

1.1

1.1

2003

1.0

7.0

2005

2.0

2.0

2005

2.0

8.0

2006

2.0

3.0

.NET 3.0 ext.

2.0

8.0

2007

2.0

3.5

2008

3.0

9.0

2010

4

4

2010

4.0

10.0

What’s New in CLR 4?


Side by side hosting


Type embedding (“No PIA”)


Background GC


AppDomain

Monitoring

Why High Compatibility Is So Hard


.NET Framework 1.1 was highly
compatible with .NET 1.0






What’s wrong with this code?

Thread [] threads = new Thread[8];

for (int i=0; i<8; i++) {


Worker worker = new Worker();


threads[i] = new ThreadStart(worker.Work);


threads[i].Start();


worker.identity = i;

}


Code from an Outlook add
-
in Microsoft executives used

Existing Side
-
By
-
Side (
SxS
)

.NET 2.0

2.0
add
-
in

3.0

3.5

Host Process (e.g. Outlook)

3.0
add
-
in

3.5
add
-
in

1.1
add
-
in

.NET 1.1

In
-
Process Side
-
By
-
Side (
SxS
)

.NET 2.0

.NET 4.0

2.0
add
-
in

3.0

3.5

Host Process
(e.g.
Outlook)

3.0
add
-
in

3.5
add
-
in

4.0
add
-
in

Type Embedding


Problem


Using a COM component generates an
interop

assembly


Registered as Primary
Interop

Assembly (PIA)


Must “carry around” that PIA


Actual code may use just a fraction of the
PIA


Type Embedding


Solution


Embed the actual interfaces and
structs

used in the client assembly


Can only work if those types can be
considered equivalent by the CLR


CLR 4 introduces “Type Equivalence”

Type Embedding (“No PIA”)

GC Recap


Pre CLR 4

GC

Workstation

Server

Concurrent

Collections

“Normal”

Concurrent

Collections

GC in CLR
4

GC

Workstation

Server

Background
GC

“Normal”

CLR 4 Garbage Collection


Workstation GC: Background GC


Can do Gen 0 & 1 GC while doing Gen 2 GC


Replaces Concurrent GC


Server GC


Nothing is changed (full GC notifications)

Ephemeral
Segment

Other Segments

Generation 0, 1 and parts
of Generation 2 live here

Most of Generation 2
lives here

AppDomain

Monitoring


Host applications can monitor resource
consumption of
AppDomains


Set a (one time) global flag


Possible usage


Unloading resource hogging
AppDomains


Comparing resource consumption of
different algorithms

AppDomain

Monitoring

What’s New in the BCL?


Managed Extensibility Framework (MEF)


Code Contracts


System.IO Improvements


Memory Mapped Files


Task Parallel Library & Parallel Extensions

Managed Extensibility Framework


Open/Closed Principle


Software entities should be
open for
extension

but
closed for modification


The
Managed Extensibility Framework
(MEF) enables greater (and easier) reuse
of components


Applications can make the shift from
being statically compiled to
dynamically
composed


Parts

Part

A

public class
SimpleMortgageCalculator

:
IMortgageCalculator

{


public
ILogger

Logger { get; set; }



public float Calculate()


{



Logger.Log
("Calculating Mortgage");




return ...;


}

}

Export
it…

Part

A

[Export(
typeof
(
IMortgageCalculator
))]

public class
SimpleMortgageCalculator

:
IMortgageCalculator

{


public
ILogger

Logger { get; set; }



public float Calculate()


{



Logger.Log
("Calculating Mortgage");




return ...;


}

}

Import
it…

Part

A

[Export(
typeof
(
IMortgageCalculator
))]

public class
SimpleMortgageCalculator

:
IMortgageCalculator

{


[Import(
typeof
(
ILogger
))]


public
ILogger

Logger { get; set; }



public float Calculate()


{



Logger.Log
("Calculating Mortgage");




return ...;


}

}

Compose

it.

Catalogs

provide the parts.

Catalog

Compose

it.

Container

is the matchmaker.

Catalog

Compose

it.

Container

is the matchmaker.

Catalog

TypeCatalog

AssemblyCatalog

DirectoryCatalog

AggregatingCatalog

Managed Extensibility Framework

Code Contracts


Code Contracts

are
a way to specify
contractual information

that is not
represented by a method
signature alone


Runtime and/or compile time


A contract contains


Pre
-
conditions


Must be true before


Post
-
conditions


Must be true after


Object

invariants


Must always hold true




Code Contracts

Code Contracts Tools


CCRewrite.exe


IL injection


CCCheck.exe


Static verification


CCRefGen.exe


Contract reference assembly


CCDocGen.exe


XML documentation

System.IO Improvements


Methods returning collections instead of
arrays


“deferred” LINQ style


Directory.EnumerateFiles
,
DirectoryEnumerateDirectories
,
File.ReadLines
,
File.WriteAllLines


DirectoryInfo.EnumerateDirectories
,
DirectoryInfo.EnumerateFiles


Enables efficient LINQ queries


Memory Mapped Files


In
System.IO.MemoryMappedFiles


Allow sharing of memory between processes


Backed up by a specific file or the paging file


Can create large shared memory (more than
4
GB)


Accessed using logical “Views”


Obtained as a stream (either “normal” or
random access)


Memory Mapped Files

Task Parallel Library


Parallelism is a key trend


Availability of multi
-
core processors


Task parallel library


High level “threading” model


Parallel Extensions


Higher level abstractions


Parallel LINQ


Automatically parallelize LINQ queries

Parallel Extensions

Summary


CLR
4
features


BCL
4
enhancements


Call to action


Related Links


Code Contracts

http://
msdn.microsoft.com/en
-
us/devlabs/dd
491992
.aspx


MEF on
CodePlex

http://
www.codeplex.com/MEF


CLR Team Blog

http://blogs.msdn.com/clrteam
/


My blog

http://blogs.microsoft.co.il/blogs/pavely