Saturday, May 21, 2011

Speeding up WCF Discovery

With the version 4.0 of the .NET framework Microsoft has enriched WCF with an implementation of the WS-Discovery protocol. Discovery allows WCF services to announce their availability and client applications to find these services at runtime without knowing the endpoint addresses. The WCF Discovery supports two modes the managed and the ad-hoc mode. In the managed mode there is proxy server that manages the information about available services. With the ad-hoc mode you don't even need a centralized server! In this mode services and client applications use multicast messages to announce their availability or to query available services. For more information about WCF discovery see WCF Discovery Overview on MSDN. This site also provides some usage examples.

In my opinion the second discovery mode (ad-hoc) is more interesting for loosely coupled distributed scenarios because you don't have to bother about a central point of failure (the discovery proxy). But playing around with WCF discovery I noticed that it takes some time on the client to discover suitable services. It's not really a big problem because you would usually do it only once e.g. during the application startup and cache the proxy or the endpoint address somehow. But if you only need only one service instance with a certain contract and you don't care which instance it is (if there are many of them) you can considerably reduce the time needed to discover the service address. You just tell the framework that you only need one result. To do so you have to setup the FindCriteria accordingly:
// Create DiscoveryClient
var discoveryClient = new DiscoveryClient(
                            new UdpDiscoveryEndpoint());
var criteria = new FindCriteria(typeof(IContract));
criteria.MaxResults = 1; // we only need one instance!
var findResponse = discoveryClient.Find(criteria);
If there is a service instance wich implements IWantedService the Find()-call would return much sooner than without adjusting the MaxResults property.

Monday, May 16, 2011

Unity: passing constructor parameters.

Unity is a dependency injection container from Microsoft. The version 2.0 is now available for download at codeplex: patterns & practices - Unity. You can use it to reduce dependencies in your .NET applications. Say you want to decouple your 'high level' code from the implementation details of the data access. With Unity you can define an interface like IDataContext and register the actual implementation in the Unity container:
var container = new UnityContainer();
container.RegisterType<IDataContext, SqlDataContext>();
In the 'high level' code you don't have to know the concrete type that implements the data access but just the contract: IDataContext. Unity will resolve the implementation you have registered at runtime:
var dataAccess = container.Resolve<IDataContext>();

Pretty easy so far but what if you want to pass a parameter to the constructor of your IDataContext implementation, e.g. a connection string? Just pass the value you want to be injected as a parameter when registering the type:
container.RegisterType<IDataContext, SqlDataContext>(
  new InjectionConstructor(connectionString));
Unity will pass the value you have defined here to a matching constructor of SqlDataContext when you will request an instance of it.

Wednesday, May 11, 2011

Dryad & DryadLINQ - academic release

An academic release of Dryad and DryadLINQ is available for download on Microsoft Research site: Dryad and DryadLINQ Academic Release.
Dryad is a distributed computing engine for clusters running Windows HPC Server 2008. DryadLINQ allows to use LINQ programming model in distributed application running on Dryad.

Friday, May 6, 2011

Parallel programming with .NET 4.0

Microsoft published 12 articles on parallel programming with .NET 4.0. You can download them here: Articles on Parallel Programming with the .NET Framework 4. These articles are mostly about TPL, PLINQ and thread safe data structures introduced with .NET 4.0.

Syntaxhighlighting for Blogger

Added syntax highlighting for the code snippets in my postings. Thanks to Heisencoder for his tutorial!