In order to access the new Team Foundation Server 2010 from your current Team Explorer 2008 you will need to install the Visual Studio 2008 Service Pack 1. Several customers were complaining that even with the service pack installed they couldn't manage to connect.
The problem is the order of the installation. For many customers, the orders was:
1) Visual Studio SP1
2) Team Explorer 2008
but if you have installed Team Explorer 2008 after the SP1 the changes are not in place. In order to sort out this just reinstall the SP1 and it will work.
Another interesting new feature of the CLR 4 comes from the Garbage collection team. On this version, they are adding some performance enhancements on the memory allocation process. The feature is commonly called “Background GC”. But what does it actually mean?
As applications are starting to consume more memory and some of them moving to wider memory spaces under 64bits processes we have started to see some latency issues while allocating memory when the full GC is running. As you may remember, for workstation version of the CLR we use a concurrent GC. This means that the GC thread will run in parallel without blocking the application execution (well, we try to minimize the blocking time). This thread will scan Gen 2 in order to mark dead objects. This operation can take some time if the memory allocation is quite large and this prevents ephemeral collections while is running. Ok, now you may be asking what does it mean?, let me explain it with some graphics.
Let’s analyze how the current concurrency GC works and the scenario that we are improving:

Now, our application needs to perform a full GC, for this it will scan Generation 2 and try to mark the dead objects as free objects. This is executed by the GC thread, the simplified steps that it will take are the following ones:
1) It will start marking the objects, checking the stacks and the GC roots. This operation will allow further allocations, this means that your application may create a new object and this will be allocated in generation 0.

2) Now there are further allocations that the GC needs to suspend the EE (Execution engine) and this will stop all threads on your application. At this stage no allocation is allowed and your application may suffer some latency.
3) The EE is resumed in order to continue working on the heap and other bits and pieces that the GC needs to handle; at this stage the allocation is allowed. But what happen if our ephemeral segment is full while this collection happens?

4) At this stage the ephemeral collection cannot swap segments and the allocation will be delayed, adding latency to your application.
As you can see, the problem is that a single GC thread cannot cope with those two operations at the same time. The current ephemeral segment is 16mb (note that this may change in the future so don’t relay on this value!). This means that you can only allocate up to 16mb or whatever is available at the time of allocation, and as you have seen on the example this space may run out before the GC collection finishes! I hope now you understand why we don’t recommend you to call GC.Collect() without a good reason J.
Ok, now let me introduce you to the background GC. This model has been optimized to reduce the latency introduced by the scenario described above. The solution came from the idea of creating a background GC that works as it has described above and a foreground GC that will be only triggered when the ephemeral segment needs to be collected while performing a generation 2 collection.
Now, if we repeat the scenario above and we try to allocate memory on the ephemeral segment while the background GC is marking the foreground GC will execute an ephemeral collection:

The ephemeral foreground thread will mark the dead objects and will swap segments (as this is more efficient rather than copying the objects to generation 2. The ephemeral segment with the free and allocated objects becomes a generation 2 segment.

As you can see now the allocation is allowed and your application will not need to wait for the full GC to finish before allowing you the allocation.
Note that this enhancement is only available on workstation, as the server version is a blocking GC per core and we didn’t have enough time to port these enhancements into it, but is definitely in our plans in the near future. We have tested this solution in 64 cores but our future objective is to hit the 128 core mark as the SQL team has provenJ.
I hope this blog post makes a bit clearer how the GC works and what kind of enhancements we are including in .NET 4.
I have been exploring different new features that come with the .NET 4, beyond the most popular ones like dynamic types and covariance; I was interested in performance enhancements. For this reason I am going to publish a couple of blog entries were I explore these different features.
Memory mapped files may sounds alien to the managed code developer but it has been around for years, what is more, it is so intrinsic in the OS that practically any communication model that wants to share data uses it behind the scenes.
So what is it? A memory mapped file allows you to reserve a region of address space and commit physical storage to a region (hmmm, sounds like virtual memory, isn’t it?) but the main difference is that the physical storage comes from a file that is already on the disk instead of the memory manager. I will say that it has two main purposes:
· It is ideal to access a data file on disk without performing file I/O operations and from buffering the file’s content. This works great when you deal with large data files.
· You can use memory mapped files to allow multiple processes running on the same machine to share data with each other.
The memory mapped file is the most efficient way for multiple processes on a single machine to communicate with each other. What is more, if we check other IPC methods we can see the following architecture:
Impressive, isn’t it? Now you have the power of this technology available on the System.IO namespace (instead of using the Pinvoke approach).
Now let’s quickly explore how it works. We have two types of memory mapped files models, one model is using a custom file, this can be any file that the application accesses it and the other one using the page file that we are going to share it with the memory manager (this is the model that most of the technologies above use).
Let’s explore the custom file model. The first thing that we need to do is to create a FileStream to the file that we are going to use, this can be an existing file or a new file (keep in mind that you should open this file as shared, otherwise no other process will be able to access it!). With the stream in place, we can now create the memory mapped file. Let’s see an example:
using System.IO.MemoryMappedFile;
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateFromFile(
new FileStream(@"C:\temp\Map.mp", FileMode.Create), // Any stream will do it
"MyMemMapFile", // Name
1024 * 1024, // Size in bytes
MemoryMappedFileAccess.ReadWrite); // Access type
I have use one of the simplest constructor, we define the stream to use and we provide a name. The object needs to know the size of it in bytes and the type of access that we need. This will create the memory mapped file but to start using it we will need a map view. We can create one using the following syntax:
MemoryMappedViewAccessor FileMapView = MemoryMapped.CreateViewAccessor();
This map covers the file from the first byte until the end. If we need now to write or read information from it we just call the map view methods with the correct offset.
int Number = 1234;
FileMapView.Write(0, Number);
FileMapView.Write<Container>(4, ref MyContainer);
We can see that we can write built in types or custom types with the generic version. The good thing about the memory mapped files is persistence, as soon as you close it the contents will be dumped on the disk, this is a great scenario for sharing cached information between applications.
Now if we want to read from it, the other process needs also to create a memory mapped file, we can use the other static initialize that opens an existing one or creates one if it does not exist.
MemoryMappedFile MemoryMapped = MemoryMappedFile.CreateOrOpen(
new FileStream(@"C:\temp\Map.mp", FileMode.Create), // Any stream will do it
"MyMemMapFile", // Name
1024 * 1024, // Size in bytes
MemoryMappedFileAccess.ReadWrite); // Access type
Create the map view and read it:
using (MemoryMappedViewAccessor FileMap = MemoryMapped.CreateViewAccessor())
{
Container NewContainer = new Container();
FileMap.Read<Container>(4, out NewContainer);
}
That’s it, really simple isn’t it? Now, there is a small drawback with this approach and is related to the size of the memory mapped file. If you don’t know in advance you will need to create a large file
“just in case”, this can be a very large file with a lot of wasted space, it would be nice to have the ability to reserve the space instead of committing all of it, isn’t it?
In order to solve this issue you can use the page file, this has the advantage of allowing you to commit data on the fly but introduces another issue: you don’t own the file and the map will last until the last handle is destroyed. But think about it, for certain scenarios this is absolutely valid.
Now if we revisit the sample we will need to change some initialization parameters, for this particular one I will use the full constructor so I can introduce some other features that are also applicable to the custom files one.
MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity();
MemoryMappedFile PagedMemoryMapped = MemoryMappedFile.CreateNew(
@"Salvador", // Name
1024 * 1024, // Size
MemoryMappedFileAccess.ReadWrite, // Access type
MemoryMappedFileOptions.DelayAllocatePages, // Pseudo reserve/commit
CustomSecurity, // You can customize the security
HandleInheritability.Inheritable); // Inherit to child process
The memory mapped file security allows you to customize who or which process can have access to the resource, this can be quite important when you want to protect sensitive information and you don’t want other processes changing the file map. You can explore that object and see all the different settings that you can change. The reference is here. If we explore the construction of the memory mapped file we can see that there is no stream, we just name the resource. This will create an association between a section of the file based on the size and the map name, this is how both processes will access the file. Note as well that I am setting the property “DelayAllocatePages”, this implements a pseudo reserve/commit model where we will use the space once is needed. Finally, the other interesting parameter is the handle inheritance model, this will allow to share this resource with a child process if is required.
The access to the file uses the same syntax as the previous example, remember that if you close the memory mapped file this will be non accessible, this issue catches many developer.
Finally, another interesting area is the creation of multiple map views, these can work on the same memory mapped file accessing different areas of the files. This will allow you to properly protect the content and allowing you to synchronize the access.

You can do this defining different offsets and lengths when you create your map view.
MemoryMappedViewAccessor WriteMap = MemoryMapped.CreateViewAccessor(0, 1024,
MemoryMappedFileAccess.ReadWrite);
MemoryMappedViewAccessor ReadMap = MemoryMapped.CreateViewAccessor(1025, 1024,
MemoryMappedFileAccess.Read);
Now you can enjoy the power of this high performance data sharing model on your applications when you compile them with .NET 4.0! Note that this blog is based on beta 1 information, I will check the post once is released.
We have been collecting the best articles on the MSDN flash communication and we have compiled them in an eBook format.
Here you can find articles from many Microsoft Consultants, inlcuding myone on the sync framework. You can download you copy here:
XPS Version
PDF Version
Enjoy!
In this second instalment we are going to review another set of frames. This guideline should help you to understand what is involved on each one of them as well as considerations and known patterns.
You can find the first part following this link.
The second part looks like this:

The following guideline can help you to identify the key issues on each area of the frame and some recommendations that we usually share with our customers in the ADC team (Application Development Consultant ). Note that this list is not exhaustive and are general suggestions that can help you on your design process.
Configuration Management
The message here is clear, we need to move configurable options to a configuration file so the user can change it without recompiling the application. Yes, is quite clear but the problem is that developers usually overlook how this information is accessed and secured. We have seen many times user names and passwords on these files without any security! Not only have that, with time these configuration files grow to unmanageable levels that jeopardized the application execution.
Key Issues:
· Lack of or incorrect configuration information
· Sharing configurations around farms
· Not securing sensitive configuration information (exposing passwords/identities)
· Lack of security around the configuration files
Consider the following guideline:
· Use IIS sharing configuration features in order to deploy configuration files over a farm.
· Consider using least-privileged process and service accounts.
· Categorize the configuration items into logical sections, this method can help you secure different areas (features like configuration delegation in IIS are very helpful in this scenario)
· Encrypt sensitive information in your configuration store
· Restrict the access to your configuration information
· Provide a separate administrative application to deal with configuration files.
|
Pattern |
Brief |
|
Provider |
Implement a component that exposes an API that is different from the client API in order to allow any custom implementation to be seamlessly plugged in. |
Coupling and Cohesion
When you are designing components for your application you should ensure that are highly cohesive and loose coupling is used across layers. Coupling refers to the dependency between components and cohesion relies on the functionality that the component provides.
Key Issues:
· Incorrect grouping of functionality
· No clear separation between objects
· Tight coupling across layers
Consider the following guideline:
· Design for loose coupling between layers.
· Consider using abstraction to implement loose coupling between components.
· Consider using common interface definitions
· Use dependency inversion to decouple dependencies, depending on abstractions rather than concrete components.
· Divide your application functionality into logical layers; if you are dealing with customers you shouldn’t include your message binding properties on the same object.
· Design for high cohesion. If a component says “Customers” it should provide only customer functionality.
· While loose coupling requires more code, the benefits include a shortened dependency chain and simplify the building process.
|
Pattern |
Brief |
|
Composite view |
Combine individual views into a composite view. |
|
Inversion of Control |
Populate any dependencies of objects on other objects or components that must be fulfilled before the object can be used by the client application. |
|
Template View |
Implement a common template view, and derive or construct views using the template view. |
|
Two-Step View |
Transform the model data into a logical presentation without any specific formatting, and then convert that logical presentation into the actual formatting required. |
|
View helper |
Delegate business data-processing responsibilities to helper classes. |
Data Access
Separating the data access layer (DAL) is an important decision that improves the application maintainability and extensibility. This will allow you to easily change the repository or the logical structure of your data model without affecting the application functionality. It is recommended to implement an entity model to translate physical designs into business ready entities.
Key Issues:
· Per-user authentication and authorization when not needed
· Chatty database calls interfaces
· Data access code embedded with business logic
Consider the following guideline:
· Avoid coupling your application model to the database schema. Use abstraction models like the entity framework.
· Enforce data integrity in the database, not through data layer code.
· Move business logic to the application layer, avoid long stored procedures as they make applications harder to migrate and control.
· Open connections as late as possible and release them as early as possible.
· Avoid accessing the database from different layers, the DAL should be the only component that knows anything about the repository.
|
Pattern |
Brief |
|
Active record |
Include a data access object within a domain entity. |
|
Data mapper |
Implement a mapping layer between objects and the database structure that is used to move data from one structure to another while keeping them independent. |
|
Data Transfer Object |
An object that stores the data transported between processes, reducing the number of method calls required. |
|
Domain Model |
A set of business objects that represents the entities in a domain and the relationships between them. |
|
Query Object |
An object that represents a database query. |
|
Repository |
An in-memory representation of a data source that works with domain entities. |
|
Row data gateway |
An object that acts as a gateway to a single record in a data source. |
|
Table data gateway |
An object that acts as a gateway to a table or view in a data source and centralizes all of the select, insert, update, and delete queries. |
|
Transactional Script |
Organize the business logic for each transaction in a single procedure, making calls directly to the database or through a thin database wrapper. |
|
Table module |
A single component that handles the business logic for all rows in a database table or view. |
|
|
|
|
|
|
|
|
|
|
|
|
Exception Management
It is important to track all the application exceptions to make the system more reliable and to properly capture application defects. A crash can be very costly for customers and is not something that you want to re-experience in order to get more information. A proper exception management should provide you with enough information when the fault occurs, this should include as much information as possible in order to reproduce the scenario.
Key Issues:
· Unstable state exposure
· Revealing sensitive information to the user
· Using exceptions to control logic flow
· Inaccurate/non-existence logging information
Consider the following guideline:
· Do not reveal sensitive information in exception messages and log files. I have seen many websites showing the full stack when an error occurs!
· Let exceptions flow if they are unexpected, catching everything without proper logging may provide a false end user experience that can lead to corrupted entries.
· Design an appropriate exception propagation strategy.
· Send the information back to a central repository when possible to analyze trends when large user based is expected.
· Log information on common repositories, like log files, event logs or external web services.
|
Pattern |
Brief |
|
Exception Shielding |
Prevent a service from exposing information about its internal implementation when an exception occurs |
|
|
|
|
|
|
|
|
|
|
|
|
Layering
Proper layering design allows you to separate the functionality into different areas of concern. This will help you to group functionality and components that will make your system more predictable and able to scale out. Layers should have a well defined communication flow, for example, layer A can talk with layer B but not the opposite scenario.
Key Issues:
· Incorrect grouping of components within layers
· Not following layering and dependency rules
· Not considering physical boundaries
Consider the following guideline:
· They should be used as logical groups, for example, user interface, process layer, application layer, data layer.
· Components within a layer should be cohesive.
· Consider physical boundaries when designing layers
· Use interfaces to define each layer, this will allow proper communication and different deployment models.
· If you are running web applications consider using message-based interfaces to provide a stateless user interface.
· Provide a facade pattern to the process and application layers.
|
Pattern |
Brief |
|
Remote Facade |
Creates a high-level unified interface to a set of operations or processes in a remote subsystem to make that subsystem easier to use, by providing a course-grained interface over fine-grained operations to minimize calls across the network. |
The next part will complete the set of considerations, watch this space!
At Microsoft we are working hard in putting together the different components that are involved deciding the architecture approach. One of these components is the architecture frame, which together with the architecture style and application type creates the architectural baseline for modern design. This guideline is targeting architects and not developers, therefore the topics and recommendations cover high level decisions.
One of the most common situations when we sit down during reviews is that the team starts choosing the technology first without analysing what they really need. They usually end up in a model that fails to deliver value to the business and trigger a set of failures around the qualities of service. In order to avoid this, a proper review of the architecture frame can help architects and developers to foresee potential problems and chose the right technology and pattern based on the real execution context. The frame is pictured below with the section covered in part 1.

The following guideline can help you to identify the key issues on each area of the frame and some recommendations that we usually share with our customers in the ADC team (Application Development Consultant ). Note that this list is not exhaustive and are general suggestions that can help you on your design process.
Authentication & Authorization
This area is one of the most misunderstood concepts in the frame, as many architects confuse these concepts as they security has been a complex area to understand. Remember that the authentication process validates the user credentials and the authorization involves the roles and actions that the user has on the system. Designing a good strategy on this area helps to improve the security and reliability of the application.
Key Issues:
· Lack of authentication across trust boundaries, as this leaves your application vulnerable to spoofing attacks, dictionary attacks and session hijacking.
· Granular or improper authorization can leave your application expose to vulnerabilities like information disclosure, data tampering and elevation of privileges.
Consider the following guideline:
· The most important consideration is performing a STRIDE analysis that may help to identify risks.
· Identify your trust boundaries; authenticate and authorize users and calls across trust boundaries. Some calls may need to be authenticated on the client and the server (mutual authentication).
· If your system involves multiple applications based on different user subset consider using a single sign-on strategy
· Do not store passwords in plain text; instead consider using hash codes or proper encrypted algorithms.
· Do not transmit passwords in plain text.
· Protect resources based on identity, groups or roles.
· Use role based authorization for business decisions.
· Use resource based authorization for system auditing.
· Use claims based authorization when you need to support federated environments or complex rights models.
· Consider using a trusted subsystem for access to back-end services to maximize the use of pooled connections.
· If the presentation and business layers are on the same machine and you need to keep the original caller’s ACL permission consider using impersonation. On the other hand, if they are on different machines consider using delegation. Note that this may affect performance.
· Implement IP filtering to protect your business layer.
· Consider the implications of using different trust settings for executing service code.
· Ensure that secure protocols such as SSL are used with basic authentication.
· Use secure mechanisms such as WS security with SOAP messages
· Run services under the most restrictive account that is appropriated
Pattern Map:
|
Pattern |
Brief |
|
Data Confidentiality |
Use message-based encryption to protect sensitive data in a message. |
|
Data Integrity |
Ensure that messages have not been tampered with in transit. |
|
Data Origin Authentication |
Validate the origin of a message as an advanced form of data integrity. |
|
Exception shielding |
Prevent a service from exposing information about its internal implementation when an exception occurs |
|
Federation |
An integrated view of information distributed across multiple services and consumers. |
|
Replay protection |
Enforce message idempotency by preventing an attacker from intercepting a message and executing it multiple times |
|
Validation |
Check the content and values in messages to protect a service from malformed or malicious content |
Caching
This is the developer’s favourite, we have seen many applications implementing caching patterns without considering why and where it is needed. Poorly designed caching strategy can lead to degradations on performance and responsiveness; on the other hand, good caching design can really speed up responsiveness and contributes to the concurrency and efficiency factor.
Key Issues:
· Caching invalid content
· Caching sensitive data
· Incorrect choice of caching store
Consider the following guideline:
· You should use caching to optimize reference to lookups and avoid network round trips.
· Do not cache volatile data.
· Try to load the cache asynchronously to avoid call delays.
· Consider ready-to-use objects when storing them in the cache instead of raw database data.
· If you need to cache sensitive data use encryption as a simple memory dump can reveal it.
· If your application is deployed in a farm avoid using local caches that need to be synchronized, instead consider using transactional resource manager or a product that supports distributed caching.
· Do not depend on data still in your cache, as it may have been removed.
Pattern Map:
|
Pattern |
Brief |
|
Dependency Cache |
Use external information to determine the state of the data stored in a cache. |
|
Page Cache |
Improve the response time for dynamic pages that are accessed frequently |
Communication
This area covers how the components on different layers will communicate with each other, the mechanism depends on the deployment scenario but the key issues are usually similar.
Key Issues:
· Incorrect choice of transport protocol
· Chatty communications
· Failure to protect sensitive data
Consider the following guideline:
· When crossing physical boundaries consider using message based communications, on the other hand, for crossing logical boundaries you should use object based communication.
· If you are not concern about the order of the messages consider using asynchronous communication to unblock processing or UI threads
· Consider using message queuing to queue messages for later delivery to cover network interruptions. Remember that that this pattern supports transacted message delivery and supports reliable once-only delivery.
· Consider net pipes for inter-process communication, TCP for local networks and HTTP based protocols for external links.
· Determine how to handle unreliable or intermittent communication.
· Validate endpoint addresses in messages.
· Decide if a message communication needs to be two-way or one-way
Pattern Map:
|
Pattern |
Brief |
|
Intercepting Filters |
A chain of composable filters that implement common pre-processing and post-processing tasks. |
|
Pipes and Filters |
Route messages through pipes and filters that can modify or examine the message as it passes through the pipe. |
|
Service Interface |
A programmatic interface that other systems can use to interact with the service. |
|
Duplex |
Two-way message communication where both the service and the client send messages to each other independently, irrespective of the use of the one-way or the
request/reply pattern. |
|
Fire and Forget |
A one-way message communication mechanism used when no response is expected. |
|
Reliable sessions |
End-to-end reliable transfer of messages between a source and a destination, regardless of the number or type of intermediaries that separate endpoints. |
|
Request Response |
A two-way message communication mechanism where the client expects to receive a response for every message sent. |
|
Channel Adapter |
A component that can access the application's API or data and publish messages on a channel based on this data, and can receive messages and invoke functionality inside the application. |
|
Message bus |
Structure the connecting middleware between applications as a communication bus that enables them to work together using messaging. |
|
Messaging broker |
A component that connects messaging systems and replicates messages between these systems. |
|
Point to point channel |
Send a message on a Point-to-Point Channel to ensure that only one receiver will receive a particular message. |
|
Pub/Sub channel |
Create a mechanism to send messages only to the applications that are interested in receiving the messages without knowing the identity of the receivers |
|
Command Message |
A message structure used to support commands |
|
Event Message |
A structure that provides reliable asynchronous event notification between applications. |
|
Competing consumer |
Set multiple consumers on a single message queue and have them compete for the right to process the messages, which allows the messaging client to process multiple messages concurrently |
|
Durable subscriber |
In a disconnected scenario, messages are saved and then made accessible to the client when connecting to the message channel to provide guaranteed delivery. |
|
Idempotent receiver |
Ensure that a service will only handle a message once |
|
Message dispatcher |
A component that sends messages to multiple consumers. |
|
Messaging gateway |
Encapsulate message-based calls into a single interface in order to separate it from the rest of the application code. |
|
Messaging Mapper |
Transform requests into business objects for incoming messages, and reverse the process to convert business objects into response messages. |
|
Polling consumer |
A service consumer that checks the channel for messages at regular intervals. |
|
Selective consumer |
The service consumer uses filters to receive messages that match specific criteria. |
|
Service activator |
A service that receives asynchronous requests to invoke operations in business components. |
|
Transactional client |
A client that can implement transactions when interacting with a service. |
Composition
This part of the frame is very important an it has a significant impact in the user experience. The composition is the process used to define how the user interfaces are structured in order to provide a consistent look and feel.
Key Issues:
· Cooperating UI modules are coupled by dependencies making development, testing and maintenance more difficult.
· Dependency changes between modules forces code recompilation and module redeployment.
· Dynamic UI layout update and dynamic modules loading difficult due hardcoded dependencies.
Consider the following guideline:
· Use abstraction patterns when possible to avoid issues with dependencies between components.
· Consider creating templates with placeholders, a good approach may be using the template view pattern to compose dynamic screens to ensure reuse and consistency.
· Consider composing views from reusable modular parts like user controls. The composite view pattern is a good approach to build a view from atomic component parts.
· Avoid using dynamic layout as they are difficult to maintain.
· If you need to allow communication between presentation components consider using the pub/sub pattern, this will lower the coupling between components and improve testability.
Concurrency and Transactions
Designing good concurrency and transaction models is a key part of the architecture as it directly affects performance (specially concurrency). Identifying the right requirements in order to select optimistic or pessimistic models can be a critical decision. There are plenty of documentation and best practices around this area but from the architectural perspective we should worry about the general issues that are system may face.
Key Issues:
· Nonexistent or poor protection for concurrent access to static data
· Deadlocks caused by improper locking
· Not choosing the right data concurrency model.
· Long running transactions that hold locks on data
· Using pessimistic/exclusive locks when not necessary
Consider the following guideline around transactions:
· Wrap business critical operations in transactions
· Use connection based transactions when accessing a single data source
· Don’t be afraid of using transaction scopes to manage transactions on multiple data sources.
· Avoid holding locks for long periods.
· If transactions are not an option consider the use of compensation methods to revert the data to the previous state.
Consider the following guideline around concurrency:
· Remember that static data is not thread safe.
· Updates to shared data should be mutually exclusive; this will prevent two threads from attempting to update the data at the same time.
· Locks should be scoped at a very fine grained level, the idea is to lock only when is necessary and release it as soon as possible.
· If you need to update static fields use the check-lock-check pattern as other thread may have changed the value between your check and your lock.
· Never apply a lock to a type definition or the current instance. Using these constructs can lead to deadlock issues that are difficult to locate. Use private static objects as locking reference instead.
· Consider the use of the synchronization support provided by collections when working with shared ones.
Pattern Map:
|
Pattern |
Brief |
|
Capture transaction details |
User DB objects such as triggers and shadow tables to record changes to all tables belonging to the transaction. |
|
Optimistic offline lock |
Ensure that changes made by one session do not conflict with changes made by other session. |
|
Pessimistic offline lock |
Prevents conflicts by forcing a exclusive lock |
|
Coarse Grained lock |
Lock a set of related objects with a single lock |
|
Transaction script |
Organize the business logic for each transaction in a single procedure, making calls directly to the database or through a thin database wrapper. |
|
Implicit Lock |
Use framework code to acquire locks on behalf of code that accesses shared resources. |
On the next blog entry we are going to explore the second subset of considerations using the architecture frame.
You can find part 2 here.
The first thing that ASP.NET developers ask themselves is how to reuse the current existing JavaScript functionality with Silverlight applications. The good news is that the Silverlight team has delivered what they have promised regarding the interaction between these two worlds. This quick post will show you how to expose types and managed instances to be used with your scripted code.
Let’s start with types, the first thing that we need to do is register the type using the System.Windows.Browser.HtmlPage object, this exposes an interesting method called RegisterCreateableType(string scriptAlias, Type type). This method publishes a managed type into the JavaScript scope. In order to allow this operation you need to decorate the type with the attribute [ScriptableTypeAttribute]. Let’s see how this is done:
[ScriptableTypeAttribute]
public class HtmlObject
{
public string Text {get;set;}
}
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
HtmlPage.RegisterCreateableType("HtmlObject", typeof(HtmlObject));
}
}
It is important to understand that this model has some limitations, the most important one is that the constructor has to be public and it has to be parameter less. Now, we can change the script in our HTML page as follows:
<script type="text/javascript">
function OnClick()
{
var SLCtrl = document.getElementById('SilverlightControl');
var ManagedType = SLCtrl.Content.services.createObject("HtmlObject");
ManagedType.Text = "Hello from Javascript";
alert(ManagedType.Text);
}
</script>
Now, what if you want to share an existence instance so you can interact with both worlds in real time? Well, there is another very helpful method on the same namespace, it is called HtmlPage.RegisterScriptableObject(string scriptKey, object instance). This type needs to have the attribute [ScriptableMember()] on the methods and properties that we want to expose. Let’s see how we can do this:
public class SharedObject
{
private Button source;
public SharedObject(Button source)
{
this.source = source;
}
[ScriptableMember()]
public bool IsEnabled
{
set { this.source.IsEnabled = value; }
}
}
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("SharedObject", new SharedObject(cmdButton));
}
}
This instance is now available to the Silverlight content object and can be accessed in a very easy manner. When the following method is executed it will change the “IsEnabled” property of our Silverlight button.
function OnClick()
{
SLCtrl.Content.SharedObject.IsEnabled = false;
}
We can also put everything together and have these shared objects passed back and forward from one context to the other, if we expand our SharedObject class with a new method that process the first object that we have created we will be able to properly cast it.
[ScriptableMember()]
public HtmlObject Process(HtmlObject info)
{
info.Text += " (this has been processed)";
return info;
}
<script type="text/javascript">
function OnClick()
{
var SLCtrl = document.getElementById('SilverlightControl');
var ManagedType = SLCtrl.Content.services.createObject("HtmlObject");
ManagedType.Text = "Hello from Javascript";
ManagedType = SLCtrl.Content.SharedObject.Process(ManagedType);
alert(ManagedType.Text);
}
</script>
As you can see “Process” is receiving a casted managed type. But what if you want to receive a non managed type? Well, there is another way of writing that method in a more generic way:
[ScriptableMember()]
public ScriptObject Process(ScriptObject info)
{
string Text = info.GetProperty("Text").ToString();
Text += " (this has been processed)";
info.SetProperty("Text", Text);
return info;
}
Think about the potential, not only sharing types and instances between your script and Silverlight but also when you need to share objects between different Silverlight applications
An essential part of the SOA Governance is to properly identify the requirements in order to introduce or upgrade a service within your SOA model, this is commonly known and service and integration planning within the SOA lifecycle. This is all good in theory but the nightmares start when architects are trying to put it in practice.
As an application development consultant here at Microsoft we constantly find chaos in the SOA first attempts, the main reason behind this is the lack of services organization. This not only brings problems to the model that are expensive to change but does not shows business value to the stakeholders, as they cannot see any ROI in the implementation. As a result, the efforts are abandoned and the architectural model remains broken.
How do we help our customers to solve this issue? We usually take them a level higher within the abstraction layer, why do you need SOA? Which are the problems that you usually have? Which are the costs involved in changing your system? We really need to understand our driven forces that lead us to a new architectural paradigm.
But what about designing the services model? This is another area where confusion arises as architects ask themselves where they should start and how. The answer to this is enclosed on the services taxonomy. There are many categorizations and naming around taxonomy but I want to present a recommendation based on the Microsoft services experience.

We define four main categories that really help us to define the high level model. Let me explain each one of them.
· Entities Services: The entities encloses your business entities, these are independent actors within your business model. The entities do not provide business benefit acting alone as they don’t have the expertise around which other entities are around. They should be completely independent and should own the data repository. A good example of an entity service will be customers. Customers are nothing more than what the name says, it has and owns all the information about the entity “customer” but knows nothing about your business.
· Capabilities Services: This is the crucial part of your business; this is what you do to add value to your services. The capabilities know very well the entities and capabilities context where it works, as interacts and consumes services to deliver value. An important thing to understand is that they know about the capability but do not control the entities that consumes, this means that if a capability needs to alter an entity it should use the entity service responsible for that. The capability also owns its own repository and controls all the business logic associated with it. A good example of a capability is an order service. The order service will know everything about placing an order and will need entities in order to generate one, for example, the order will need the customer information that comes from the entity service.
· Application services: These services supports your business model, this means that provides auxiliary services to your entities and capabilities not directly associated with your system. This usually involves wrappers to third party application or bridges to other systems. A good example may be a service wrapper to a Remedy application.
· Bus services: This taxonomy groups all the support and infrastructure services that allow your entities and capabilities to work within the infrastructure policies. For example the security services that validate users can be grouped in this taxonomy.
As you see, with this categorization you can start to add services to your model. Where do you start? The easiest way to do it is asking yourself what your business does, what is the essence of it. Let’s imagine that you are the responsible for designing the SOA model for a large supermarket. The amount of services that you will need is massive but if I ask you about the essence of your business which will be the answer? Believe it or not this sounds easy but architects that are too embedded in the daily duties cannot properly answer the question. My model would be:

Look how simple it is, you just join customers with products and generate orders. Now, if I ask you to add these services in the taxonomy model you now know how to do it.

Remember that upon I have presented orders as the linking service between the entities this does not suggest hierarchy. All the services are at the same level as they can be accessed by an ESB (Enterprise service bus) or ISB (Internet service bus), or even simply your process layer. What is more, what you can do is apply the same model when you enter into the service design scope; this is for example when you design the internal taxonomy of “Orders”. This service will have again a process layer, entities and capabilities, but now only around the orders scope. Does this sound familiar? Well, it may, as it is a common model applied to the object oriented design.

Internally, these new entities and capabilities can be isolated components that are responsible for a solely function, for example, an internal process can be the workflow that triggers a new order and a capability may be altering the order value.
This can show you that applying the SOA governance model around taxonomy can help you and your developers to properly design services and object oriented components properly isolated and encapsulated, as is the same mindset. What is more, the WCF team is working in a “in-process” binding that will allow you to use a WCF communication with objects that today are part of your main process and tomorrow may develop into independent services just changing the configuration file! Isn’t it beautiful?
If your organization is facing these problems and you want Microsoft to support you and help you around your SOA initiative don’t hesitate the contact the “Application Development Consultant team” in Microsoft (http://www.microsoft.com/uk/adc)
After months of writting, testing and understanding the inners of the hidden bugs, myself and my colleagues Jonathan Swift, Dan Wahlin and Chris Barker are finishing the last bits of the next book about Silverlight 2. The title will be "Silverlight 2 for ASP.NET developers" and will be published by Wrox.
The experience has been amazing, you will not imagine how hard is to properly structure the technical message in a book and then playing with Beta technology! But there is something that I gained for sure and is the deep understanding of how Silverlight 2 works. Once the book is out, I will publish some of the tips that we found that will really help you developing outstanding RIA!
I will publish in this space when the book is out and the link to the description.
Silverlight 2 Beta 2 is nearly here (out at the end of this week), I want to highlight key announcements in TechEd and give you an overview on the competitiveness area.
User Interface Changes: Beta 2 includes improvements in animation support, error handling and reporting, automation and accessibility support, keyboard input support, and general performance. This release also provides more compatibility between Silverlight and WPF.
Silverlight 2 Controls: Beta 2 includes a new templating model called Visual State Manager that allows for easier templating for controls. Other features include the introduction of TabControl, text wrapping and scrollbars for TextBox, and for DataGrid additions include Autosize, Reorder, Sort, performance increases and more. Most controls are now in the runtime instead of packaged with the application.
Networking: Beta 2 includes improved Cross Domain support and security enhancements upload support for WebClient, and duplex communications (“push” from server to Silverlight client).
BCL: Beta 2 includes improved threading abilities, LINQ-to-JSON, ADO.NET Data Services support, better support for SOAP, and various other improvements to make networking and data handling easier.
Deep Zoom: Beta 2 introduces a new XML-based file format for Deep Zoom image tiles, as well as a new MultiScaleTileSource that enables existing tile databases to utilize Deep Zoom. Better, event driven notification for zoom/pan state is another improvement in Silverlight 2 Beta 2.
Moonlight: Another interesting areas that I want to highlight is the imminent release of Moonlight for Linux support, check this blog: http://tirania.org/blog/archive/2008/May-13-1.html
Silverlight Mobile: This is quite hermetic but Windows Mobile and Nokia based (S60, S40, N8xx Internet Tablet) devices as a first priority, they will support in the a version of the Silverlight 2 plug-in.
How is Silverlight different than Flash? Flex? Adobe AIR?
Some of the scenarios for Flash and Silverlight usage are similar, such as rich media/video within websites, or interactive rich content for e-commerce, e-learning, or advertising. However, Silverlight uses a dramatically different approach for creating and delivering experiences in a way that aligns more with our customers’ development and deployment needs.
Microsoft’s client/web platform offerings span Windows to the Web, and include emerging surfaces such as the media/living room (Xbox360, Media Center PC), as well as mobile devices. Each of these platforms has shared capabilities and development tooling, but greatly different performance and integration characteristics. By comparison, Flash, Flex, and AIR are all variants of the Flash animation plug-in that Adobe acquired from Macromedia. They share a presentation and programming framework that was first developed for “skip-intro” and other pre-broadband experiences in the browser, and have incrementally evolved to add better programming, but lack the integration, performance, and tooling necessary to build many of the apps and content experiences that will be increasingly of interest to many businesses.
Silverlight, WPF, and ASP.NET AJAX share development and design tooling support with Microsoft Expression and Visual Studio product lines. With these tools, designers and developers can collaborate more effectively than ever before to design and implement superior UX. Adobe’s tooling and application frameworks are very focused on animation and cosmetic design, traditionally for the creative professional and not the application development audience.
Flex: For enterprise line of business (LOB) applications, Microsoft offers a breadth of solutions for building business applications that directly integrate with Microsoft Office, as integral parts of the Excel, Word, PowerPoint, and SharePoint Server experience. Flex is a new technology built on the Flash animation plug-in which allows developers to build richer Web-based UI and connections to server data. However, Flex lacks deeper integration into the environment where most of these LOB applications are used.
Adobe AIR: Web standards based development using AJAX is a proven technology for developing compelling and easy to deploy applications to the desktop with zero-touch requirements for additional client side infrastructure. Microsoft has shown continued innovation and commitment to this space with our Internet Explorer browser, our Live services, and our ASP.NET AJAX scripting capabilities for server code that delivers compliant Web standards to Mac and Windows clients.
With the new entities framework to be released in the next service pack some of our customers and team members are starting to discuss what is the correct pattern. I wanted to extract my position around the choices.
I am big supporter of having options, in this case I don’t consider one better than the other one, the stored procedure option can be the best choice for one scenario and LINQ, with all the different flavours including Entities, for another one.
You can go all the way separating process layers using stored procedures that may sound perfect for your scenario. In this scenario the architects and developers must maintain the complexity inherit from this approach as the data is usually quite tight to the processes. This adds risk to the projects making them more complicated to maintain (you need to involve the DBA for schema changes!). Some people may argue that this is the most performance approach but I am a strong believer that usually performance is not well understood. What is fast enough for an application? Do you need to go to the extreme performance when it may not be needed, sacrifying functionality or simplicity? If that is the case SP approach can be the best for you.
Now on the other side, I love the idea of separating the physical model from the logical model (so I can get rid of the DBAs! J). Business entities are much better represented with the entity framework and gives developers another layer to abstract the schema complexity, using strongly type models that can help detecting errors at early stage. Yes, there is a performance penalty price to pay, but maybe the functional benefits really help the business to develop quality software reducing the project risk. Have you measure how slower is it? You will be surprise with the results if you correctly design the entities.
The LINQ story is quite powerful as well, it is true that you have to consider learning curve and the community is taking longer than expecting to digest it. But with LINQ to entities things are starting to get more sense and I can imagine more people jumping into it.
What I think is cool? ADO.NET Data services (do not confuse with SQL Server Data Services! ), as is a great story for another cool technology like Silverlight. In this model you have your physical database completely abstracted from the model as you use the entity framework, you can use LINQ to query it and then post it using REST. This will send the results using XML, transformed to a struct using Silverlight Data services directly binding the results to your grid! That’s what I call functional abstraction.
To conclude, I still believe that it depends on the scenario, the beauty of this is to have options.
As many questions are usually asked regarding how to interact between Silverlight and JavaScript I am going to spend some lines showing an example so you can keep it as a reference. The sample below works with Silverlight 2 and is based on the Beta 2 that we are dogfooding at Microsoft at the time of this post, I haven’t seen any change on this area on the breaking changes document therefore it seems that the syntax will remain the same.
One of the most interesting namespaces that we have in Silverlight 2 is the System.Windows.Browser, as it allows us to interact with the HTMLPage object that provides access to the current web page that is hosting our plug-in. This class allows us to interact directly with the page, for example we can force navigation:
// We format the destination using a querystring
string Destination = string.Format(@"SecondPage.aspx?MyVar={0}",txtMyContent.Text);
// Sets the new URI with a query string entry
Uri SourceUri = new Uri(HtmlPage.Document.DocumentUri, Destination);
// Navigates to the next page
HtmlPage.Window.Navigate(SourceUri);
As you can see we can interact with the query string as well, indeed, we can read the current one using the following command:
HtmlPage.Document.QueryString;
Not only the navigation is included, also we can post forms to the server using the same class. Beyond this, one of the features that I really like is the ability to call javaScript functions from my managed code; you can use the Invoke method for this:
HtmlPage.Window.Invoke("ExecuteAnimation", txtParams.Text);
To execute the following JavaScript method:
function ExecuteAnimation(params)
{
// Do something useful
}
Now, to go to the other direction, we can use get a reference of our XAML component using the name of it and then call a published method:
var ctrl = $get("Xaml1");
ctrl.Content.navObj.SendMessage(“Hello”);
We will need to decorate our method with the ScriptableMember attribute in order to be published to the JavaScript world as follows:
public Page()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("navObj", this);
}
[ScriptableMember()]
public void SendMessage(string state)
{
// Do something useful
}
In future entries we are going to dig deeper into how this interaction works and we will explore some best practices.
For those that can't wait to print and stick a Silverlight 2 wallpaper to the wall here you can find a high resolution picture:

You can download the high resolution one here
If you have missed one of my sessions during the EMEA Windows Mobile Briefing you can find the recordings of our last Finland event. During this event I was covering also the first session and we had a large attendance, upon the demo using the orientation aware control didn't work :) (I had cut and paste the application blocks instead of copy and paste!) but we had a lot of fun. For the material and demos please check my other post here: http://blogs.msdn.com/salvapatuel/archive/2008/02/24/emea-windows-mobile-tour.aspx
Session 1 - Ready, Set, Go! |
Silverlight presentation
This first session sets the scene for the day and kicks off with a quick look at the mobile developer landscape. Among other things, we will see what resources are available to the device programmer and how the pieces fit within the bigger Windows desktop and server platforms. A key piece is an overview of the recently released Windows Mobile 6 operating system. The session wraps up with a look at and demos of the brand new Visual Studio 2008 development environment which promises even better integration and powerful new features for device developers.
Session 2 - Windows Mobile in Practice |
Silverlight presentation
Features are great but we live in the real world. This session takes a realistic scenario and shows how Windows Mobile 6 and the upcoming .NET Compact Framework 3.5 can be leveraged to provide a compelling Proof-of-Concept in very few lines of code. See a wide variety of features being demonstrated, from location awareness to making phone calls programmatically. The session will provide an understanding of how everything can be tied together in an end-to-end solution.
Session 3 - It's the Business |
Silverlight presentation
Enterprise solutions are increasingly being mobilized for the new technically-savvy workforce. However, these solutions need industry-strength quality and features that will support the complex requirements these organizations have. This session will begin by looking at SQL Server Compact Edition, the sophisticated data storage solution that allows data synchronization with back-end databases. We then show how you can leverage industry design patterns and application blocks to provide a lower-risk, tested approach to solve common problems. Finally, a quick review of the Microsoft Dynamics mobile offerings will round off the options available for enterprise use.
Note: This code has been updated using Silverlight 2 RTM
With Silverlight 2 a new set of components have been introduced into the equation, the most important one is the CLR Execution engine, that support in this case the dynamic language runtime (DLR) that we are going to host in this post. The following chart shows the items that have been changed since version 1.0
The interesting area regarding the DLR is the interoperability options between the silverlight components and scripted languages. In the release 2.0 we are including support for Iron Python, Ruby and Managed JavaScript. These languages are currently offered through codeplex here. The support for VBScript has been replaced by the future version of Visual Basic, called VBX, but it won’t be supported until the language is released. This shows one of the other beauties of the DLR, the ability to add new languages on the fly. These languages can be used in two directions, script to silverlight and silverlight to script.
This post will focus on hosting the scripting engine in your Silverlight application adding dynamic code to the execution, allowing the inclusion of instances into the scripted code. The first thing that we need to do is including the namespaces:
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.JScript.Runtime;
With these namespaces we have access to the DLR different engines, in this scenario we are going to use the JavaScript engine.
ScriptRuntime MyRuntime = JScript.CreateRuntime();
With the instance in place now we can retrieve the script engine as follows:
ScriptEngine JSEngine = MyRuntime.GetEngine("js");
In this case, my option was managed JavaScript. This object now focuses on the language per se, exploring the class will fascinate you, as you have access to the core execution of a full program, being able to compile code, define scope and execute it. Now the internals of the execution needs to be explored, I have mentioned the scope; you create a new scope using the following command:
IScriptScope Scope = JSEngine.CreateScope();
Each execution scope represent the environment where your code will run, it allows you to set variables and their values. We can add an object instance that the scripted code will run (similar to executing COM components in the past but now linked to your silverlight object). Let’s suppose that I expose a method that calls a WCF method:
IRemoteInterface TransparentProxy = MyFactory.CreateChannel();
Scope.SetVariable("WCFProxy", TransparentProxy);
Now that we have the scope, we can start defining our code. In order to use the code we should use the ScriptSource object.
ScriptSource Code = JSEngine.CreateScriptSourceFromString(“WCFProxy.MyMethod();”);
The script engine allows you to load external files as well, just explore the options that it offers. The ScriptSource object allows you to play with the code, like getting individual lines. All this information can be dynamically changed, giving you the flexibility that you need. Now, the final step, we can compile the code to make sure that is correct and execute it (note that you can execute it directly and will be compiled on the fly).
source.Compile() ; // Optional
Code.Execute(Scope);
You can shutdown the engine when is not needed using the Shutdown() command. You can see the power of injecting code into your application on the fly and the simplicity of the model. Explore the DLR!