Monday, December 28, 2009

ArraySegment<T> Class

Somehow I missed such a useful class in .Net framework this now. This class is useful when we are working on one dimensional arrays and need to segment arrays.

One quick use case is network packets. Where we will allocate buffer upfront and fill segments to form a packet.

Good part of this class is multiple arraysegment objects can refer to same instance of array. Mean what we get is reference to original object rather than a copy. Any updates to segment will automatically update original array.

Isn’t useful? I feel so.

Saturday, December 19, 2009

Hosting IronRuby in MSBuild

This post builds on earlier post in this series and provides a mechanism to host IronRuby in MSBuild.

Basics of MSBuild

A very brief introduction of MSBuild is present here. MSBuild is a modern build file format built on XML technologies.
Important components of MSBuild are

  1. Project
  2. Target
  3. Task
  4. ItemGroup
  5. PropertyGroup
  6. Imports

Project stands as root element with a default namespace declaration. Find declaration in following example

<Project DefaultTargets = "HelloMSBuild"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

In this DefaultTargets attribute is of interest. Target is the set of actions that msbuild will perform against the project by default.

To understand better let us create a sample project by name “test.msproj”.

1) Create a new text file

2) Add Project element as shown above

3) Add target ‘HelloMSBuild’ as shown below

<Project DefaultTargets = "SayHello"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

<Target Name="SayHello">
<Message Text="Hello MSBuild" />
</Target>

</Project>

From visual studio command prompt run “msbuild test.msproj”. That is the simplest helloworld with MSBuild where target executes only task “Message”. “Message” is a built-in task. Built-in tasks are included by default. Thus we don’t find any declaration about where Message task is defined.

<Project DefaultTargets ="Lab02"

xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

<ItemGroup>

<
Items Include="Item1" />

<Items
Include="Item2" />

</
ItemGroup>


<
PropertyGroup>

<
PropertyA>Property1</PropertyA>

</
PropertyGroup>

<
TargetName="Lab02">


<
Message Text="$(PropertyA)"/>

<
Message Text="@(Items)"/>


</Target>

</
Project>

That gives a basic idea about using items and properties. A project can contain more than on ItemGroup elements. All items in ItemGroup must be of same type. In this case they are of “Items”. But a different ItemGroup can contain different kind of elements. We can only refer to items as a group. In this case we referred them @(Items). But properties can be referred individually. Actually we can refer to properties individually only. Format to access a property is $(PropertyName).

So far we have see built-in task “Message”. Now let us see how to build a custom task

Getting started with Custom Task

Here I describe the procedure to build a custom task by name SimpleTask. This task does nothing fancy, but this exercise provides some insight about working with custom tasks. Look at MyTasks01.dll custom task. And we are using UsingTask option to refer to custom task.

<Project DefaultTargets = "CreateDirectory"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<
UsingTask AssemblyFile="D:\MsBuildTasks\MyTask01.dll" TaskName="SimpleTask"/>

<
ItemGroup>
<
Param Include="Param1" />
<
Param Include="Param2" />
</
ItemGroup>

<
PropertyGroup>
<
Param3>Param3</Param3>
</
PropertyGroup>

<
PropertyGroup>
<
Status />
</
PropertyGroup>


<
Target Name ="CreateDirectory">

<
SimpleTask MyProperty ="@(Param)" >
<
Output TaskParameter="MyMessage" PropertyName="Status"/>
</
SimpleTask>

<
Message Text="$(Status)"/>

<
SimpleTask MyProperty ="$(Param3)" >
<
Output TaskParameter="MyMessage" PropertyName="Status"/>
</
SimpleTask>
<
Message Text="$(Status)"/>

</
Target>

</
Project>

Alternative approach for UsingTask is importing external project as a whole. Sometimes this would be useful as the task needs more specific parameters or item groups. In such case all those parameters and item groups can be defined in external project and imported as a whole. To do this create a file by name “MyTasks.Targets” in same directory as task MyTask01.dll.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<
UsingTask AssemblyFile="D:\MsBuildTasks\MyTask01.dll" TaskName="SimpleTask"/>
</
Project>

And to import this file in project file replace UsingTask element with

<Import Project="D:\MsBuildTasks\MyTasks.Targets"/>

Building Custom Task

Now let us see how to write a custom task. Create a library and refer to libraries (You can choose version 2.0 or 3.5)


  1. Microsoft.Build.Framework
  2. Microsoft.Build.Utilities

Then update class file as shown below

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MyTasks
{
public class SimpleTask : Task
{
public override bool Execute()
{
//Parameters are semi column seperated values
string[] parameters = myProperty.Split(';');
foreach (string paramter in parameters)
myMessage += paramter + " - Done\n";
return true;
}

private string myProperty;
public string MyProperty
{
get { return myProperty; }
set { myProperty = value; }
}

private string myMessage;
[Output]
public string MyMessage
{
get { return myMessage; }
set { myMessage = value; }
}
}
}


Here observe that we added ‘Output’ attribute to MyMessage. And that is taken into status property in output element of SimpleTask. Then the value is printed through Message task.

With this background about MSBuild, we can move to hosting IronRuby in MSBuild source.

IronRuby Task

This part creates a new task to host IronRuby Task.

Create a new class library. Add references to MSBuild libraries and IronRuby libraries which include

IronRuby, IronRuby.Libraries, Microsoft.Build.Framework, Microsoft.Build.Utilities.v3.5, Microsoft.Dynamic, Microsoft.Scripting, Microsoft.Scripting.Core.

Then replace source contents with following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IronRuby;
using IronRuby.Runtime;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace ABB.INCRC.MSBuildTasks
{
public class IronRubyTask : Task
{
#region Properties

string script;

[Required]
public string Script
{

get { return script; }

set { script = value; }

}

string status;
[Output]
public string Status
{
get { return status; }
}


#endregion
public override bool
Execute()
{
try
{
ScriptEngine engine = Ruby.CreateEngine();

ScriptSource source = engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);

Object result = source.Execute();

status = result.ToString();
}
catch (Microsoft.Scripting.SyntaxErrorException syntaxError)
{
status = "SyntaxErrorException : " + syntaxError.Message;
return false;
}
catch (MemberAccessException maex)
{
status = "MemberAccessException : " + maex.Message;
return false;
}
return true;
}
}
}

Build this library and copy to folder like D:\MsBuildTasks\IronRubyTask. Note that execution of this task depends on referred libraries. Thus you need to copy all libraries to this folder. In last post we have seen how to refer a task with UsingTask element.

Here we take a better approach by using an external task definition.

External Project Reference

Create a new file by name MyMSBuildTasks and place following content. This script does lot of work


  • Makes a reference to IronRubyTask
  • Creates property group to hold script elements, which increment AssemblyFileVersion. This is done by updating AssemblyInfo.cs file in properties folder. BuildNumber is persisted in a separate BuildNumber.txt file
  • Creates tasks to execute scripts
  • Scripts are concatenated from multiple properties thereby keeping scripts modular
<?xml version="1.0" encoding="utf-8"?>
<
Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<
UsingTask AssemblyFile="D:\MsBuildTasks\IronRubyTask\IronRubyTask.dll" TaskName="IronRubyTask"/>

<
PropertyGroup>
<
GetBuild>
<![CDATA[
#Get Build

def getBuild(buildIndexFile)

begin

file = File.new(buildIndexFile, "r")

while (line = file.gets)
@token = line.split("=")
if (@token[0] == "build")
@build = Integer(@token[1])
end
if (@token[0] == "revision")
@revision = Integer(@token[1])
end
end

file.close

rescue => err

err
end

end

@build = -1
@revision = -1

getBuild("D:\\BuildNumber.txt")

]]>
</
GetBuild>

<
SetBuild>
<![CDATA[

#Set Build
def setBuild(buildIndexFile)
begin
buildIndex = "build=#{@build}\r\nrevision=#{@revision}"
File.open(buildIndexFile,"w") {|f| f.write(buildIndex) }

"Updated : #{buildIndexFile}"

rescue => err
err
end
end
]]>
</
SetBuild>

<
DefineFileVersion>
<![CDATA[

#update AssemblyVersion

def updateFileVersion(assemblyInfoFile)


if(@build > 65536)
@build = 0
end

if(@revision > 65536)
@revision = 0
end


assemblyInfo = File.read(assemblyInfoFile)

assemblyInfo = assemblyInfo.gsub(/AssemblyFileVersion\(\"1\.0\.(\d+)\.(\d+)\"\)/,
"AssemblyFileVersion(\"1.0.#{@build}.#{@revision}\")")
#puts assemblyInfo

begin
File.open(assemblyInfoFile,"w") {|f| f.write(assemblyInfo) }
"Updated : #{assemblyInfoFile}"
rescue => err
puts err
err
end


end
]]>
</
DefineFileVersion>

<
UpdateFileVersion>
<![CDATA[

#Execution Starts here

if @revision > 0
@revision = @revision + 1
else
@build = @build + 1
end

updateFileVersion("D:\\MsBuildTasks\\Properties\\AssemblyInfo.cs")

]]>
</
UpdateFileVersion>


<
SetBuildNumber>
<![CDATA[

#Execution Starts here

@build = @build + 1
@revision = 0

setBuild("D:\\BuildNumber.txt")

]]>
</
SetBuildNumber>

<
SetRevisionNumber>
<![CDATA[

#Execution Starts here

@revision = @revision + 1

setBuild("D:\\BuildNumber.txt")

]]>
</
SetRevisionNumber>

</
PropertyGroup>

<
Target Name="UpdateFileVersion">
<
IronRubyTask Script="$(GetBuild)$(DefineFileVersion)$(UpdateFileVersion)">
<
Output TaskParameter="Status" PropertyName="Status" />
</
IronRubyTask>
<
Message Text="$(Status)" />
</
Target>

<
Target Name="SetBuildNumber">
<
IronRubyTask Script="$(GetBuild)$(SetBuild)$(SetBuildNumber)">
<
Output TaskParameter="Status" PropertyName="Status" />
</
IronRubyTask>
<
Message Text="$(Status)" />
</
Target>

<
Target Name="SetRevisionNumber">
<
IronRubyTask Script="$(GetBuild)$(SetBuild)$(SetRevisionNumber)">
<
Output TaskParameter="Status" PropertyName="Status" />
</
IronRubyTask>
<
Message Text="$(Status)" />
</
Target>

</
Project>

Referring target file

Now create a test project file and refer above file.

<Project DefaultTargets = "Test"

xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

<
Import Project="D:\MsBuildTasks\ABB.INCRC.MSBuildTasks"/>

<
Target Name="Test" DependsOnTargets="UpdateFileVersion;SetBuildNumber;SetRevisionNumber" />

</
Project>

Here we use “DependsOnTargets” to call multiple targets from referred file.

Hope this post provided a use-case to host dynamic languages like IronRuby for useful tasks. Advantage of this approach is you need to build only one custom task. All other needs can be achieved using scripts. And scripts are part of msbuild project in the form of properties.

Tuesday, December 15, 2009

Hosting IronRuby in .Net applications - Part2

In last post of this series we have seen few examples of IronRuby. That is just a glimpse of IronRuby.
With this background we can take a look at integrating these snippets in a .Net application.

I would like to provide some basic introduction about Dynamic Language Runtime (DLR) and then present an example.

DLR Overview

DLR infrastructure provides solutions for different purposes which include

  1. port dynamic languages to .NET
  2. add dynamic features to existing language
  3. author libraries whose objects support dynamic operations
  4. employ (host) dynamic languages in applications and frameworks
But this post is oriented towards hosting scenarios only.

DLR is built on top of Common Language Runtime (CLR). Microsoft .Net framework is designed to support multiple programming languages on CLR. It provides common services like GC, JIT etc. for static languages like C#, VB.Net etc. The CTS and CLI infrastructure allows sharing libraries written in any language with any other language targeting CLR. And we rarely realize need to host CLR explicitly, as it is done automatically by various CLR executable modules due to modifications done to Portable Executable format.

DLR extends CLR with services required for dynamic languages like python, ruby. In similar lines it first extends type system to be dynamic. Then, incase of statically typed languages, meta data provides necessary relative virtual addresses at compile time for method invocations. These virtual pointers turn into realistic pointers during JIT compilation process and remain as is as long as execution engine is alive. This is not the case with dynamic languages which require a dynamic method dispatching mechanism. Also applications need to explicitly host Dynamic Runtime Engines. But DLR eases some effort here by providing a common hosting API for all dynamic languages targeting DLR. So if we summarize, key components of DLR would include

  1. dynamic type system
  2. dynamic method dispatching system
  3. common hosting API

Dynamic type system and dispatching system provide services for first three purposes mentioned at the beginning of ‘DLR Overview’. Common hosting API provide services for last purpose in that list.

Hosting Scenarios

As such there could be several scenarios in which DLR can be employed (hosted). Such scenarios include

  • executing a script in a given scope using default script-host
  • executing several scripts in multiple scopes using custom script-host
  • controlling on how to pre-compile and improve executing performance, an advanced scenario

This part of the series confines to first scenario in which one script is executed in a given scope.

Execution Overview

As mentioned above, this post is presenting the very basic mode of executing a script.
Steps involved in this process include

  • Create Script Engine for Ruby
  • Create Scope object from engine and set scope
  • Create code
  • Execute code within a given scope

Example

Following step by step procedure makes up a simple application to host IronRuby

  1. Start Visual Studio and create a new console application. Copy required
  2. Refer following dependent assemblies from ~\<Program Files>\IronRuby 0.9.2\bin
    IronRuby.dll
    IronRuby.Libraries.dll
    Microsoft.Dynamic.dll
    Microsoft.Scripting.Core.dll
    Microsoft.Scripting.dll
  3. Add following using statements
  4. using IronRuby;
    using Microsoft.Scripting.Hosting;
    using Microsoft.Scripting;

  5. Add following code in Main

  6. ScriptEngine engine;
    ScriptScope scope;

    String code;

    Int32 i1 = 20;
    Int32 i2 = 10;

    engine = Ruby.CreateEngine();
    scope = engine.CreateScope();

    scope.SetVariable("i1", i1);
    scope.SetVariable("i2", i2);

    code = @"def compareValues()
    return $i1 > $i2
    end

    $i1 = i1;
    $i2 = i2;

    comp = compareValues();

    puts comp";

    engine.Execute(code, scope);

    Console.WriteLine("Press any key to continue");
    Console.ReadLine();


  7. Build and run code.

If everything goes well you should see true on screen.


Friday, December 11, 2009

Wednesday, December 02, 2009

Hosting IronRuby in .Net applications - Part1

This post provides necessary information to host IronRuby in .Net applications. I assume no knowledge of Ruby or DLR from reader. It starts with basic introduction to IronRuby and then provides information on DLR and hosting options.

Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. IronRuby is a Open Source implementation of the Ruby programming language for .NET, heavily relying on Microsoft's Dynamic Language Runtime.

This is the first part of this series. This part familiarizes the ruby language as much required for this series. For better understanding of ruby language reader can refer to Ruby Programming, IronRuby Documentation, Ruby Learning Tutorial, Ruby Manual.

Getting started

Though prior knowledge of Ruby is an added advantage a novice can still follow this post to get started with Ruby.

Before we can start Download IronRuby 0.9.2 msi. After installing the msi you should find “~\IronRuby 0.9.2\IronRuby Console” in Start –> Programs. This is an interactive console. We can also run a script using “ir.exe <script>”. ir.exe can be found at “<installation path>\IronRuby 0.9.2\bin\ir.exe".

Enter following lines in a notepad and save the file with .rb extension.

ex01.rb

puts 'Hello World'
print "Hello World\n"
puts "Hello World"
puts "Hello World";

Now run this example from command prompt. “ir.exe ex01.rb”. You can test these three lines from interactive console also.

Observations from this example are


  • puts or print either one works to print a message

  • semi-colon at the end of a line is optional

  • Single quote or double quotes, either one works. There is subtle difference between them regarding allowed escape sequences line new line etc.

  • There is no need of main or class to start a hello world program

OK we have just written a ruby script. There is no main/Main here. OK that doesn’t mean we can’t have functions. Let us see them.

ex02.rb

def sayHello
puts 'Hello World'
end

sayHello
sayHello()
sayHello();

Place this code in ex02.rb and run “ir.exe ex02.rb”. All three statements produces same result.

Observations from this example are


  • functions are defined as def and end blocks

  • there is no return type specified. In ruby we don’t specify a return type

  • definitions must be declared prior to calling them

  • all definitions should start with small letter. Though a capital letter as starting character works sometimes, that is not a good practice. Avoid it

  • definition can be called is all three forms. It there are no arguments braces are optional. Again last semi-colon is optional

Fine. Let us pass an argument and get a return parameter.

ex03.rb

def getSQRT(arg)
Math.sqrt(arg)
end

sqrt = getSQRT(ARGV[0]);

puts sqrt

Place this code in ex03.rb and run “ir.exe ex03.rb 4”. Square root of 4, 2.0 is printed on screen.

Observations from this example are

  • command arguments are in ARGV array starting from index zero

  • arguments doesn’t take any type parameter

  • return call is optional. If no return statement is coded, result of last statement is passed as return value

  • sqrt parameter is a local variable, but there is no type specified. It hold return value of getSQRT call

  • Math is a module which is implicitly available. We didn’t write anything line using/import Math

Now let us see how comparison works.

ex04.rb

def compareValues()
return $i1 > $i2
end

$i1 = ARGV[0]
$i2 = ARGV[1]

comp = compareValues();

puts comp

Place this code in ex04.rb and run “ir.exe ex04.rb 2 1”. ‘true’ is printed on screen

Observations from this example are

  • return keyword is specified though it is optional

  • global variables as prefixed with $. This ‘$’ sign specifies that the variable is a global variable.

  • Variable scopes are

    • Local variable : Defined with a block. Starts with lower case letter or ‘_’. ex: var1, _var2

    • Instance variable : Defined within a class (classes are covered later) as a member. Starts with ‘@’ ex: @inst1, @inst2

    • Class variable: These are like static variable in a class. Only one instance is allowed and the same is shared among all objects. Starts with ‘@@’. ex: @@cls1, @@cls2

    • Global variables: These are accessible for all definitions. Starts with ‘$’. eg: $g1, $g2

    • Constants are started with a upper case letter. ex: PI, SpecificGravity

  • There are some commonly used built-ins as mentioned below

    • self - Execution context of the current method

    • nil - Expresses nothing

    • true - Expresses true

    • false – Expresses false. nil also is considered to be false, and every other value is considered to be true

Its time to take big bite.

ex05.rb

class ClassA
attr_accessor :dict

def initialize()
@dict = System::Collections::Generic::Dictionary[String,Object].new
self.dict.Add 'Argument', 12.243
end

def addElements
begin
if @dict['Argument'] == 12.243
@dict.Add 'Key1', 1
self.dict.Add('Key2','TWO')
@dict.Add('Key3', 8.765);
end
rescue StandardError => ste
puts 'StandardError occurred : ' + ste
end
end
end

clsA = ClassA.new

clsA.addElements



for i in clsA.dict.Keys
puts clsA.dict[i]
end

Place this code in ex05.rb and run “ir.exe ex05.rb”. Following results are printed on screen

12.243
1
TWO
8.765

We covered lot of ground here.

Let us start with a small pie

  • Class name to start with a capital letter.
  • attr_accessor creates a get set accessors like properties. We can declare multiple accessors in one statement like
    attr_accessor :dict, :hach, :lict
  • def initialize() is a paramterless constructor

Instance variable

  • As mentioned earlier instance variables are declared with prefix ‘@’. They can be accessed using ‘@’ notation or self.<variable> notation. Observe two lines in constructor and usage of either notation

Using CLR types and object creation

  • CLR types can be used in IronPython with syntax mentioned here.

    Observe minor difference in scope resolution operator ‘::’ and generic template notation with square brackets.

  • Also observe the notation to create objects with <Type>.new method. eg: Dictionary[String,Object].new and ClassA.new

  • We can use all methods of CLR type as is. Observe usage of ‘Keys’ property and ‘Add’ methods

Exception handling

  • ‘begin’, ‘rescue’, and ‘else’ sequences are exception handling blocks. Exceptions are raised using ‘raise’ keyword

  • rescue is like catch while raise is like throw. => operator provides a reference to current exception into the variable on right side.

  • there is option of else to catch all other exceptions

    begin
    raise 'Error occured'
    rescue Exception1 => ex1
    rescue Exception2 => ex2
    else
    Other exceptions
    end

for loop

  • Here for loop is used with iterator.
  • There is no need to specify type for iteration variable. eg. for i in clsA.dict.Keys

There is so much in it. Now let us see what ruby offers for delegation of control

ex06.rb

def AuthenticateUser(l)
if l.call('ruby', 'gem')
puts 'Authentication Successful'
else
puts 'Authentication Failed'
end
end

#Function pointer
fp = proc { |username, password|
if username == 'ruby' && password == 'gem'
return true
end
}

AuthenticateUser(fp)

#Method pointer
class Authenticate
def initialize(username, password)
@username = username
@password = password
end
def validateCredentials(username, password)
if username == @username && password == @password
return true
end
end
end

a = Authenticate.new('ruby','gem')

mp = a.method( :validateCredentials )

AuthenticateUser(mp)

Place this code in ex06.rb and run “ir.exe ex06.rb”. Two ‘Authentication Successful’ messages are printed on screen.

Here Authenticate method takes a delegate as argument. This argument is a pointer to a routine on which method can invoke a call. Above code demonstrates passing the routine as function pointer and method pointer within a class.

Observations from this example are

  • Invocation of a routine is done with .call method
  • Function pointers are obtained using ‘proc’ keyword. It can be done using keyword ‘lambda’ also. There is a subtle difference between them regarding invocation context. More details can be found at link
  • The usage within | | is arguments for routine being called followed by implementation using these arguments. It is like anonymous method in C#
  • To get the reference to a method within a class we use ‘.method’ function.

Some more examples in Part-2. And then part-3, I will cover about integrating ruby scripting feature in .Net applications.

Saturday, November 21, 2009

TIOBE Programming Community Index for November 2009

Check TIOBE index for 2009.

I guess this year C will be declared as programming language of the year. And with GO in league we know some dynamics will change by next year.

What I am interested to know what is driving PHP so strong. And between Python and Ruby, ruby is picking up.

Thursday, November 19, 2009

Its 5 years of blogging

I started blogging since 12 Nov 2004. Probably this is the only hobby that I liked so much for so long. Ofcourse after starting twitter mainstream blogging is reduced.
Anyway it gave me so much pleasure in last 5 years and keeping me motivated to persue it further.

Tuesday, November 03, 2009

Just installed VS2010 Beta2

Just installed VS2010 Beta2.

Though the general layout is not new, editor is not looking a bit sluggish. Personally I am impressed with emphasis given UML. But couldn’t understand what is that InstallShield 2010 doing in VS2010 rather I was expecting WIX there.

Yet to explore UML support completely. But first look is OK. It looks like there is way to generate code using extensions. I am still going to through the articles and yet to get a hang it. But I am amused to know the fact that MEF is playing a key role here.

Also I am liking Architecture views, especially the dependency (directed) graph by class and its analyzer features to check circular, hub and unreferenced nodes.

But I am not sure, what are improvements on metrics. I don’t see any changes.

And coming back to XML. New viewer is somewhat OK but I am really missing what we had in VS2005.

Saturday, October 24, 2009

Back on BSNL broadband

End to this long silence. I am back on BSNL braodband at home (Bangalore)
Here goes connection quality.


And to my suprise BSNL gave me a wireless modem WA3002G4. Pretty decent one.
Taking power availability here, I may need to go for a small battery till I decide on Inverter.
Feeling to be connected after long time.

Monday, August 31, 2009

Resuming .....

I chaged my job and relocated to Bangalore. Just settled down and resuming my normal life.

Wednesday, August 05, 2009

Emerson Announces Third Quarter 2009 Results

Source

• Sales of $5.1B, flat with second quarter 2009
• Earnings per share of $0.51
• Free Cash Flow of $800M, Up 19% from third quarter 2008;
  Operating Cash Flow of $916M, Up 11%
• Expects full year earnings per share $2.20 to $2.30

ST. LOUIS, August 4, 2009 – Emerson (NYSE: EMR) announced net sales for the third quarter ended June 30, 2009 of $5.1 billion, a decrease of 22 percent compared with $6.6 billion for the same period last year.  Underlying sales in the quarter declined 19 percent, which excludes a 4 percent unfavorable impact from currency exchange rates and a 1 percent positive impact from acquisitions.  On a sequential basis, sales in the third quarter 2009 were essentially flat with the second quarter, an encouraging sign of stabilization, but below what we had expected a few months ago.

Fiscal Year 2009 Outlook
The bottom of the global economic recession is forming and we expect to see the bottom in the global GFI (gross fixed investment) decline in our fourth fiscal quarter, but we do not expect to see any significant recovery until late 2010.  This will be discussed during our conference call later today.  Based on results for the first three quarters of fiscal 2009 and current order trends, Emerson expects full year earnings per share in the range of $2.20-$2.30.  Underlying sales are expected to decline 12 to 13 percent from 2008 levels, which excludes an estimated 4 percent unfavorable impact from currency translation and a 1 percent favorable impact from completed acquisitions resulting in a net sales decline in the range of 15 to 16 percent to $20.8 to $21.1 billion.  Fiscal 2009 operating cash flow is targeted to be $3 billion and the free cash flow target is $2.4 to $2.6 billion.  We will continue to reduce our global inventory until the global recovery is clearly happening, early in calendar year 2010.  The company expects to incur approximately $280 to $300 million of restructuring expense in fiscal 2009.

Siemens Earnings Release Q3 2009

Source

Financial highlights:

· Siemens’ strong order backlog softened the impact of the global recession on revenue and profit. Backlog for the Sectors totaled €84.3 billion.

· Third-quarter revenue of €18.348 billion was 4% lower than in the prior-year quarter.

· Orders of €17.160 billion were well below the prior-year level, due in part to a high basis of comparison that included exceptionally large orders at Mobility. The book-to-bill ratio was 0.94.

· Lower revenue held back Total Sectors profit at €1.667 billion, despite a 40% increase in Sector profit in Energy.

· Net income was €1.317 billion, an increase from the second quarter but a decline compared to the strong third quarter of fiscal 2008.

· Free cash flow declined due in part to substantial outflows related to previously reported project reviews and initiatives to safeguard profitability.

Tuesday, August 04, 2009

MTOM vs. Streaming vs. Compression – Large attachments over WCF

Source

MTOM is related to Bandwidth while Streaming is related to Buffering.

Compression – WCF’s extensible channel architecture allows us to easily plug-in a compression channel. So, how about not using MTOM or binary, and just applying compression on what we are about to transfer? First compression doesn’t come for free, it costs a lot in terms of CPU. You need to weigh the CPU cost of compression / decompression vs. Latency cost (i.e. is bandwidth a bottleneck?). For Binary encoding, I think it doesn’t make sense (I would encourage you to do your own test, but it didn’t show me much difference), for MTOM encoding I would prefer sending an already offline compressed attachment (i.e. a compressed .bmp instead of .bmp) & for Text encoding, yes, it may make sense. Say, you want to send 10000 customers (though you shouldn’t be doing that) and you need to use Text for interoperability reasons. I recommend to use compression by all means for such scenarios.

Sunday, August 02, 2009

Schneider-Electric Q2, 2009 results

Source

Q2 SALES CONTINUED TO BE IMPACTED BY GLOBALLY WEAK MARKET CONDITIONS

Second quarter 2009 sales reached €3,933 million, down 15.1% on a current structure and exchange rate basis. Organic growth was -19.7%.

Following is sector wise percentage.

Electrical Distribution -16.0%

Automation & Control -27.6%

Critical Power & Cooling -17.6%

Total -19.7%

 

Most affected in the quarter was Schneider’s Automation & Control division, 24% off the prior-year period.

Wednesday, July 29, 2009

Rockwell Results

Source

Rockwell Automation Reports Third Quarter 2009 Results

  • Revenue down 31 percent compared to the third quarter of fiscal 2008, down 4 percent sequentially
  • Diluted EPS of $0.23
  • Company reaffirms fiscal 2009 EPS guidance of $1.40 - $1.70

Segment wise

Architecture & Software

  • Sales decreased 36% YOY, 5% of decrease due to currency translation
  • Sales increased 2% sequentially

Control Products & Solutions

  • Sales decreased 28% YOY, 5% of decrease due to currency translation, partially offset by 1% of growth from acquisitions
  • Sales decreased 8% sequentially

Tuesday, July 28, 2009

Honeywell 2Q Profit Down 38%

Source

Chairman and Chief Executive Dave Cote on Monday called the company's performance good in a tough environment, saying it had several long-term contract wins and strong cash flow. "Economic conditions, however, remain challenging and we are not planning for any recovery in 2009," he added.

Sales for its automation and control systems segment fell 17% as profits declined 11% and margins rose to 11.5% from 10.8% on productivity improvements.

Friday, July 24, 2009

ABB Q2, 2009 Results

Source: Revenues stable as markets continue to challenge, cost take-out supports margins

  • Revenues of $7.9 billion on continued successful execution of the order backlog
  • $1 billion EBIT after ca. $120 million restructuring-related costs – EBIT margin at 13.2%
  • Orders down 27% in local currencies vs record Q2 in 2008 – base orders 25% lower
  • Cash from operations above $1 billion, net income at $675 million
as % of revenues Q2 09 Q2 08
Power Products 19.50% 19.40%
Power Systems 7.60% 7.10%
Automation Products 14.90% 19.60%
Process Automation 9.30% 11.80%
Robotics -21.80% 7.00%

 

Net cash at the end of the second quarter was $5.7 billion compared to $4.8 billion at the end of the previous quarter. Cash flow from operations amounted to $1.1 billion, slightly higher than the second quarter of 2008

 

Demand in ABB’s industrial end markets depends to a large extent on GDP growth and capital spending, together with commodity prices. Customers’ need to steadily improve efficiency and productivity to meet increasing competition also drives orders, along with demand in construction and in general industry.


Therefore, management’s priority for 2009 remains to ensure that the company has the flexibility to respond quickly to changing market conditions, taking advantage of its global footprint, strong balance sheet and leading technologies to improve its cost competitiveness while simultaneously tapping further opportunities for profitable growth.

ABB also confirms its previously published targets for the period 2007 to 2011, with the exception of the Robotics division, which is facing an unprecedented market downturn and requires further restructuring.

Thursday, July 23, 2009

Invensys Outlook and Q1,2009 Performance

Source

Compared with the corresponding period last year, Group order intake in the first quarter of the financial year was up 17% at actual exchange rates and was flat at constant exchange rates.

In our new Invensys Operations Management (IOM) division, the integration of our industrial automation businesses is proceeding well and we have appointed Sudipta Bhattacharya, formerly President of Wonderware, to lead the division. The pipeline of order prospects in projects and solutions remains strong though we are continuing to experience reduced volumes in the short-cycle products business, which will have an impact upon first half performance. However we expect the second half to benefit from our large order pipeline, the IOM integration and the increasing effects of cost reduction initiatives.

Invensys Rail continues to perform well in our core markets and our pipeline of order prospects in export markets remains strong. In addition, we are focussed upon pursuing the opportunities in countries implementing significant rail infrastructure stimulus packages.

Invensys Controls has seen some stabilisation in demand in North America in recent weeks and is experiencing declines within its European operations. Overall we continue to expect that the division will be profitable for the year with positive cash flow.

Financial Position
At 30 June 2009, the Group had net cash and deposits of £277 million, a £32 million reduction in the quarter, with good operating cash conversion of around 100% offset by the effect of currency translation and the payment of non-operating items including restructuring, taxation and part of the Baan minority

ARC's Sixth India Forum

http://www.arcweb.com/Events/Pages/Hyderabad09.aspx#register

Winning Strategies and Best Practices for Process Industries
Driving Performance During Economic Downturn

July 23-25, 2009 - Hyderabad, India

Tuesday, July 21, 2009

Sunday, July 19, 2009

Rockwell - Free Starter Programming Software for MicroLogix 1000 and 1100

Rockwell is offering MicroLogix free of charge. Using MicroLogix users can create, modify and monitor application programs for Allen-Bradley MicroLogix controllers.

http://www.ab.com/programmablecontrol/plc/micrologix/downloads.html

Saturday, July 18, 2009

Cognizant in alliance with Invensys

Cognizant, a leading technology, consulting and business process outsourcing services provider, has entered into a strategic global product research and development alliance with Invensys Operations Management (IOM), a global provider of technology systems, solutions and consulting services in the process and manufacturing space.

Being an employee of Invensys, I am present personally in this townhall. And I can say that mood was quite grim in the beginning, but slowly my colleagues started facing reality. This is the walk of life. Sometimes Business heads take decision and then ask techies to support their vision and mission. I don’t have any viewpoint here to comment about such approach.

Personally I believe in taking confidence of every stakeholder before venturing anything of this nature. My definition of stakeholder is not limited to CEO’s and top level leaders, rather I go with each and every one till last possible lead in the chain.

I would compare it with land acquisition activity by governments. Land owners doesn’t stand a chance but accept the little bit more monetary benefit. Now it is up to Cognizant to keep this pool of talented resources motivated and provide them a vision ahead.

Thursday, July 09, 2009

Automation controller market growth will resume in 2010

As published in automation world,  based on ARC report, automation controller market is expected to have growth in 2010.

http://www.automationworld.com/news-5761

Highlights

a) PLC market is expected to contract in 2009 but is expected to increase in 2010

b) Growth is expected in building automation business as commercial, industrial, and residential building projects resume

c) Once China and India resume their gross domestic product (GDP) growth, it will create demand for a broad range of products

d) Services continue to be the fastest-growing segment of the overall DCS market.

e) After market services segment is much greater than that of project services

f) Main Automation Contractor (MAC) is expected to take full responsibility for all the automation related aspects of a project

g) Demand has been strong for outsourced maintenance and performance-related services such as loop monitoring

It is 11 years now

As on today my professional career completed 11 years. Longest vacation I ever had was for 11 days.

Somehow I am feeling like that is not enough to reset the garbage that I collected. I wish giving myself a break for couple of months to do something that is close to heart.

I just want to kick start my Bullet and hit roads till I reach a mountain peak where I can live at least a day without having a connection to anything in this world.

Tuesday, July 07, 2009

We will do original designs locally

Honeywell’s worldwide chairman and CEO David M Cote was in Bangalore recently to inaugurate the firm’s second research facility. The head honcho spoke about why Honeywell is bullish on India, research areas it is focusing on

Wednesday, July 01, 2009

Honeywell Introduces Improved Ballistic Material

Source: Honeywell Introduces Improved Ballistic Material for Military, Police Vests and Soft Armor Applications

I guess India is potential market for this business. Typical armor that our police and military personnel use weighs almost 12 KGs. HAL with its expertise in composite materials could attempt to make a better solutions. Sometime back I heard from a senior executive of Bangalore based aerospace company about their interest in this area. In my opinion application of technology for life saving needs like this is a unique opportunity.

ABB’s 3D animation solutions for Azipod at Nor-Shipping

Source : ABB in 3D at Nor-Shipping

ABB’s 3D animation solution for its 1 billion order revenue offering (Azipod) is an attraction at Nor-shipping.

ABB is the world leader in electric propulsion, and offers a full range of systems, from variable speed electric machinery for shaftline propulsion and mechanical thusters to the unique family of Azipod podded propulsion systems.

An extensive development project within ABB Marine was established in 2007 with the objective of developing a next generation, further enhanced Azipod® propulsion unit family.

The next generation Azipod is a centerpiece of ABB's world famous electric propulsion system for ships, which usually consists of an Azipod, a frequency converter and a power supply.

Azipod units increase fuel efficiency by up to 15 percent in vessels using an efficient design that delivers enhanced maneuverability in harbors and confined areas.

Reduced Life Cycle Costs through Improved Operational Efficiency,Availability and Maintainability

Thursday, June 25, 2009

Is twitter an alternative for opinion polls?

Looking at http://fixoutlook.org/, I feel it is as another facet of twitter as a tool for collecting opinion poll. Regular radio buttons, checkboxes wont allow you to express your view. They force you to choose from what is asked for. I guess twitter approach is giving better handle to express the opinions. Any idea how to parse live twitter updates in public timeline?

Tuesday, June 23, 2009

Wonderware Hardware Offerings

Wonderware is offering range of hardware platforms to compliment their software.

Portifolio includes equipment for hazardous area, touch panels tablet PC etc.
Find range of offerings

Managed Extensibility Framework - Part2

This is in continuation with my previous post, Part1 of this series. Part1 covered the problem MEF tried to solve. I will start this part directly with example and then I will explain various sections in detail.

As of now MEF is all in one dll named System.ComponentModel.Composition. You can download this framework from codeplex.

It is currently in Preview 5. OK to get a quick grasp of this framework let us create three solutions.

  1. Console application which can consume extensible parts
  2. Class library which contains contract, rather interfaces
  3. Class library which contains one of the extensible parts that implements contract

Console application which can consume extensible parts

  1. Create a console application and call it “meflab1”
  2. Add reference to System.ComponentModel.Composition.dll (from bin directory of downloaded zip file)
  3. Replace code in Program.cs with following snippet
    using System;
    using System.IO;
    using System.Reflection;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Primitives;
    namespace meflab1
    {
    class Program
    {
    [Import]
    public IGreetings Greetings { get; set; }

    static void Main(string[] args)
    {
    Console.WriteLine("Enter 0 to quit, any other number to continue");
    while (Console.ReadLine() != "0")
    {

    Program program = new Program();
    program.Compose();
    program.Run();
    }
    }

    void Compose()
    {
    try
    {
    DirectoryCatalog catalog =
    new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);

    AggregateCatalog agcatalogue = new AggregateCatalog(new ComposablePartCatalog[] {catalog,
    new AssemblyCatalog(Assembly.GetExecutingAssembly())});

    CompositionContainer container = new CompositionContainer(agcatalogue);

    CompositionBatch batch = new CompositionBatch();

    batch.AddPart(this);

    container.Compose(batch);
    }
    catch (FileNotFoundException fnfex)
    {
    Console.WriteLine(fnfex.Message);
    }
    catch (CompositionException cex)
    {
    Console.WriteLine(cex.Message);
    }
    }

    void Run()
    {
    if (Greetings != null)
    {
    Console.WriteLine(Greetings.SayHello());
    }
    Console.Read();
    }
    }
    }

  4. Add a class by name SimpleGreeting and replace code in SimpleGreeting.cs with following snippet
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;

    namespace meflab1
    {
    [Export(typeof(IContextInfo))]
    public class UserInfo : IContextInfo
    {
    public IDictionary<string, string> GetContextInfo()
    {
    return new Dictionary<string, string> { { "UserName",
    Environment.UserDomainName + "\\" + Environment.UserName } };
    }
    }
    }
  5. Save this solution.

Class library which contains contract, rather interfaces


  1. Create a class library and call it “meflibrary”
  2. Rename Class1.cs to Contract.cs
  3. Now replace contents of Contract.cs with following snippet. We are just defining two interfaces here
    using System.Collections.Generic;
    namespace meflab1
    {
    public interface IContextInfo
    {
    IDictionary<string, string> GetContextInfo();
    }

    public interface IGreetings
    {
    string SayHello();
    }
    }
  4. Build it and add reference of this class library to application we built in last step

Class library which contains one of the extensible parts that implements contract

  1. Create a class library and call it as “mefpart”
  2. Add reference to System.ComponentModel.Composition.dll (from bin directory of downloaded zip file)
  3. Rename Class1.cs to SimpleGreeting.cs
  4. Now replace contents of SimpleGreeting.cs with following snippet. Here we are implementing one of the extensible part
    using System.ComponentModel.Composition;

    namespace meflab1
    {
    [Export(typeof(IGreetings))]
    public class SimpleGreeting : IGreetings
    {
    [Import(typeof(IContextInfo))]
    public IContextInfo ContextInfo { get; set; }

    public string SayHello()
    {
    string userName;
    var props = ContextInfo.GetContextInfo();
    props.TryGetValue("UserName", out userName);
    return "Hello " + (userName ?? "<null>") + " from Visual Studio 2010";
    }
    }
    }
  5. Build it

Having this infrastructure let us do two exercises

  1. Run application without dependent assembly
  2. Run application without dependent assembly during start up but make it available before JIT compiling

Run application without dependent assembly

Now build first solution and try to run it.

  1. Open command prompt
  2. Navigate to output folder of first solution
  3. start mefpart.exe. Application will be waiting at “Enter 0 to quit, any other number to continue”
  4. Enter any non-zero value. We end up with following error
The composition produced a single composition error. The root cause is provided below.
Review the CompositionException.Errors property for more detailed inf
ormation.

1) No exports were found that match the constraint
'((exportDefinition.ContractName = "meflab1.IGreetings") && (exportDefinition.Metadata.ContainsKey("Expor
tTypeIdentity") && "meflab1.IGreetings".Equals(exportDefinition.Metadata.get_Item
("ExportTypeIdentity"))))'.

Resulting in: Cannot set import 'meflab1.Program.Greetings
(ContractName="meflab1.IGreetings")' on part 'meflab1.Program'.
Element: meflab1.Program.Greetings (ContractName="meflab1.IGreetings")
--> meflab1.Program

This is expected as we don’t have any library implementing IGreetings interface.

Run application without dependent assembly during start up but make it available before JIT compiling

  1. Open command prompt
  2. Navigate to output folder of first solution
  3. start mefpart.exe. Application will be waiting at “Enter 0 to quit, any other number to continue”
  4. Copy mefpart.dll from output directory of mefpart’s solution to output directory of meflab1. That is nothing but current directory of command prompt.
  5. Enter any non zero value
  6. Observe that application could seamlessly load part and execute code in it

So what didn’t work in part-1 works now. I will leave explanation of various sections of this code to next post, i.e. Part-3.

Managed Extensibility Framework - Part1

I wanted to make this post since last two weeks. For some reason it didn’t happen. Today I was asked by one of my colleagues so I couldn’t take another exception.

First let me introduce you what is officially told about this framework

“The Managed Extensibility Framework (MEF) is a new library in .NET that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed. If you are building extensible applications, extensible frameworks and application extensions, then MEF is for you.”

So take that middle sentence “statically compiled to dynamically composed”. It is all about that.

Building reusable code into libraries and using them in applications is normal. In legacy Win32 applications if a library is linked to an application, linker used to add reference to that library in PE header. When runtime tries to load this application loader used to perform following operations

  1. resolve all dependencies
  2. load them into memory
  3. map them to respective pointers
  4. then start main routine

But with CLR, and JIT compilation in the middle, things are slightly different. Now loader tries to load those libraries which are needed to execute just the Main method. As other methods are called from Main, IL for those methods are JIT compiled on demand and loaded into memory. These things can be simplified with an example. [By the way, if you are thinking that I am going away from main subject of MEF; you are thinking correctly. But I am doing this internationally. Because I feel understanding about the problem that MEF is trying to solve makes you an informed user of MEF, so that you will be able clearly distinguish when and where MEF can be used. So I will keep this part of the series just for introduction.]

Now coming back to example. Let us start with a simple console application. Name it “WOMEF”. (without MEF.)

Replace the code in Program.cs with following snippet.

using System;
using WOMEFLib;

namespace WOMEF
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter 0 to quit, any other number to continue");
while (Console.ReadLine() != "0")
{
new Program().SayHelloToUser();
}
}

public void SayHelloToUser()
{
Console.WriteLine("In SayHelloToUser");
IWOMEFLib womeflib = new CWOMEFLib();
womeflib.SayHello(Environment.UserDomainName + Environment.UserName);
Console.ReadLine();
}
}
}
Let us define IWOMEFLib and CWOMEFLib in a library. So add a class library project to this solution by name “WOMEFLib”.

Rename Class1.cs to CWOMEFLib.cs. Then replace code with following snippet.


using System;
namespace WOMEFLib
{
public interface IWOMEFLib
{
int SayHello(string username);
}
public class CWOMEFLib : IWOMEFLib
{

#region IWOMEFLib Members

public int SayHello(string username)
{
Console.WriteLine("\"" + "Say Hello to " + username + "\"");
return 0;
}

#endregion
}
}
Now in WOMEF project add a project reference to WOMEFLib project. Change configuration to Release, build and test application.

So nothing great about this. But we are going to witness some interesting facts with these two assemblies.

  1. When loader loads CWOMEFLib.dll

  2. What happens if CWOMEFLib.dll is missing when application is starting

  3. When SayHelloToUser methods is JIT compiled

  4. What happens if CWOMEFLib.dll is copied to application directory while it is waiting for user input

In order to check these scenarios we are going to view method table and descriptors of application WOMEFLib.exe in runtime.

We are going to use Windbg here. If you don’t have it installed download it at link.

When loader loads CWOMEFLib.dll

Here we are going to check when loader loads CWOMEFLib.dll.

  1. Start Windbg.

  2. In Windbg, click on File –> Open executable

  3. Navigate to Release folder of solution and select WOMEF.exe

  4. Windbg stops with interrupt 3. Press F5 to continue.
  5. ntdll!DbgBreakPoint:
    7c90120e cc int 3


  6. Now application starts and waits for user entry with message “Enter 0 to quit, any other number to continue”

  7. In Windbg go to Debug menu and click on break. This is the ideal time to break into application as it is up and running.

  8. Now load sos.dll in windbg by entering following command in Windbg “.loadby sos.dll mscorwks”
  9. ntdll!DbgBreakPoint:
    7c90120e cc int 3
    Missing image name, possible paged-out or corrupt data.
    0:003> .loadby sos.dll mscorwks


  10. Now dumpdomains by entering command “!dumpdomain”

  11. 0:003> !dumpdomain
    *** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\WINDOWS\Microsoft.NET\... -
    PDB symbol for mscorwks.dll not loaded
    --------------------------------------
    System Domain: 7a3bd058
    LowFrequencyHeap: 7a3bd07c
    HighFrequencyHeap: 7a3bd0c8
    StubHeap: 7a3bd114
    Stage: OPEN
    Name: None
    --------------------------------------
    Shared Domain: 7a3bc9a8
    LowFrequencyHeap: 7a3bc9cc
    HighFrequencyHeap: 7a3bca18
    StubHeap: 7a3bca64
    Stage: OPEN
    Name: None
    Assembly: 001b0628
    --------------------------------------
    Domain 1: 0016d298
    LowFrequencyHeap: 0016d2bc
    HighFrequencyHeap: 0016d308
    StubHeap: 0016d354
    Stage: OPEN
    SecurityDescriptor: 0016e5c0
    Name: WOMEF.exe
    Assembly: 001b0628 [C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll]
    ClassLoader: 001b06a8
    SecurityDescriptor: 001ae190
    Module Name
    033e1000 C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll


    Assembly: 001b9800 [D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe]
    ClassLoader: 001b9e68
    SecurityDescriptor: 001b96c8
    Module Name
    00a72c5c D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe


    Assembly: 001bcb30 [D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEFLib.dll]
    ClassLoader: 001bbc98
    SecurityDescriptor: 001bcd28
    Module Name
    00a730c8 D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEFLib.dll

  12. As expected there are three app domains. In application specific appdomain three assemblies are loaded. First one (mscorlib.dll) is pretty standard one. Second one (WOMEF.exe) is application itself. And third one (WOMEFLib.dll) is referenced assembly.

  13. So loader loaded referenced assembly though we have not yet used any type from it so far. We are just at first line in Main method. Also we don’t use any type from WOMEF.dll in Main method. Still loader loaded this assembly because it is there in manifest. Now let us see what happens if this assembly is not available when we start WOMEF.exe. Kill application by pressing Control+C. If needed close Windbg and reopen.

What happens if CWOMEFLib.dll is missing when application is starting

Here we are going to check what happens if CWOMEFLib.dll is missing when application is starting. Move CWOMEFLib.dll from Release folder to some other folder outside solution directory (say to Desktop. Don’t copy cut it.)

  1. Start Windbg.

  2. In Windbg, click on File –> Open executable

  3. Navigate to Release folder of solution and select WOMEF.exe

  4. Windbg stops with interrupt 3. Press F5 to continue.
  5. ntdll!DbgBreakPoint:
    7c90120e cc int 3

  6. Now application starts and waits for user entry with message “Enter 0 to quit, any other number to continue”.

  7. Though one of the dependent assembly is not available application runs normally till this point. This makes an interesting point. Why loader didn’t complain that CWOMEFLib.dll is not available? Answer is SayHelloToUser is not yet JIT compiled. And we will verify this fact in next section. Kill application by pressing Control+C. If needed close Windbg and reopen.

When SayHelloToUser methods is JIT compiled

Here we are going to check when SayHelloToUser methods is actually JIT compiled. Move back CWOMEFLib.dll to Release folder.

  1. Start Windbg.

  2. In Windbg, click on File –> Open executable

  3. Navigate to Release folder of solution and select WOMEF.exe

  4. Windbg stops with interrupt 3. Press F5 to continue.
  5. ntdll!DbgBreakPoint:
    7c90120e cc int 3
  6. Now application starts and waits for user entry with message “Enter 0 to quit, any other number to continue”.

  7. In Windbg go to Debug menu and click on break. This is the ideal time to break into application as it is up and running.

  8. Now load sos.dll in windbg by entering following command in Windbg “.loadby sos.dll mscorwks”
  9. ntdll!DbgBreakPoint:
    7c90120e cc int 3
    Missing image name, possible paged-out or corrupt data.
    0:003> .loadby sos.dll mscorwks


  10. Now dumpdomains by entering command “!dumpdomain”

  11. 0:003> !dumpdomain
    *** ERROR: Symbol file could not be found. Defaulted to export symbols for C:\WINDOWS\Microsoft.NET\... -
    PDB symbol for mscorwks.dll not loaded
    --------------------------------------
    System Domain: 7a3bd058
    LowFrequencyHeap: 7a3bd07c
    HighFrequencyHeap: 7a3bd0c8
    StubHeap: 7a3bd114
    Stage: OPEN
    Name: None
    --------------------------------------
    Shared Domain: 7a3bc9a8
    LowFrequencyHeap: 7a3bc9cc
    HighFrequencyHeap: 7a3bca18
    StubHeap: 7a3bca64
    Stage: OPEN
    Name: None
    Assembly: 001b0628
    --------------------------------------
    Domain 1: 0016d298
    LowFrequencyHeap: 0016d2bc
    HighFrequencyHeap: 0016d308
    StubHeap: 0016d354
    Stage: OPEN
    SecurityDescriptor: 0016e5c0
    Name: WOMEF.exe
    Assembly: 001b0628 [C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll]
    ClassLoader: 001b06a8
    SecurityDescriptor: 001ae190
    Module Name
    033e1000 C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll


    Assembly: 001b9800 [D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe]
    ClassLoader: 001b9e68
    SecurityDescriptor: 001b96c8
    Module Name
    00a72c5c D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe


    Assembly: 001bcb30 [D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEFLib.dll]
    ClassLoader: 001bbc98
    SecurityDescriptor: 001bcd28
    Module Name
    00a730c8 D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEFLib.dll

  12. Now we are just at first line of Main method. Let us get address if method table of assembly WOMEF.exe by using command !dumpmodule –mt <module address 00a72c5c>. See module address in red color.
  13. 0:003> !dumpmodule -mt 00a72c5c
    Name: D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe
    Attributes: PEFile
    Assembly: 001b9780
    LoaderHeap: 00000000
    TypeDefToMethodTableMap: 00a700c0
    TypeRefToMethodTableMap: 00a700cc
    MethodDefToDescMap: 00a70128
    FieldDefToDescMap: 00a70138
    MemberRefToDescMap: 00a7013c
    FileReferencesMap: 00a701a4
    AssemblyReferencesMap: 00a701a8
    MetaData start address: 004020c0 (1864 bytes)

    Types defined in this module

    MT TypeDef Name
    ------------------------------------------------------------------------------
    00a73010 0x02000002 WOMEF.Program

    Types referenced in this module

    MT TypeRef Name
    ------------------------------------------------------------------------------
    03650508 0x01000001 System.Object
    03654258 0x01000012 System.Console
    036508ec 0x01000013 System.String
    00a73484 0x01000016 WOMEFLib.IWOMEFLib


  14. Using method table address let us get module descriptions by using command !dumpmt –md <method table 00a73010>. See mt address in red color.
  15. 0:003> !dumpmt -md 00a73010
    EEClass: 00a712f4
    Module: 00a72c5c
    Name: WOMEF.Program
    mdToken: 02000002 (D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe)
    BaseSize: 0xc
    ComponentSize: 0x0
    Number of IFaces in IFaceMap: 0
    Slots in VTable: 7
    --------------------------------------
    MethodDesc Table
    Entry MethodDesc JIT Name
    035a6a70 03424934 PreJIT System.Object.ToString()
    035a6a90 0342493c PreJIT System.Object.Equals(System.Object)
    035a6b00 0342496c PreJIT System.Object.GetHashCode()
    036172f0 03424990 PreJIT System.Object.Finalize()
    00a7c019 00a73008 NONE WOMEF.Program..ctor()
    00de0070 00a72ff0 JIT WOMEF.Program.Main(System.String[])
    00a7c015 00a72ffc NONE WOMEF.Program.SayHelloToUser()

  16. Here observe the JIT status of SayHelloToUser method as NONE. Means that SayHelloToUser is not yet JIT compiled. Let us run the application by pressing F5 in Windbg and entering a non zero value in console. As soon as you enter a non-zero value in console CLR compiles SayHelloUser, loads it from with reference to SayHello method in WOMEFLib.dll. Now console waits for input because of ReadLine.

  17. Now let us break into debugger again by clicking on Break menu option of Debug menu in Windbg.

  18. Let us check status of method description again by using same command !dumpmt –md <method table 00a73010>. You can even use up arrow twice in windbg to get there.
  19. 0:003> !dumpmt -md 00a73010
    EEClass: 00a712f4
    Module: 00a72c5c
    Name: WOMEF.Program
    mdToken: 02000002 (D:\My Documents\Visual Studio 2008\Projects\MEF\WOMEF\WOMEF\bin\Release\WOMEF.exe)
    BaseSize: 0xc
    ComponentSize: 0x0
    Number of IFaces in IFaceMap: 0
    Slots in VTable: 7
    --------------------------------------
    MethodDesc Table
    Entry MethodDesc JIT Name
    035a6a70 03424934 PreJIT System.Object.ToString()
    035a6a90 0342493c PreJIT System.Object.Equals(System.Object)
    035a6b00 0342496c PreJIT System.Object.GetHashCode()
    036172f0 03424990 PreJIT System.Object.Finalize()
    00a7c019 00a73008 NONE WOMEF.Program..ctor()
    00de0070 00a72ff0 JIT WOMEF.Program.Main(System.String[])
    00de00d0 00a72ffc JIT WOMEF.Program.SayHelloToUser()


  20. Now observe status of SayHelloToUser method as JIT. This makes it clear that SayHelloToUser method would have never been JIT compiled if a zero is entered in console window as the first option. So when control goes inside while loop CLR started JIT compiling called method.

  21. Let us see a tricky case now. Assume that we wont make WOMEFLib.dll available to WOMEF.exe when WOMEF.exe is starting but we will copy this assembly while application is waiting for user input with message “Enter 0 to quit, any other number to continue” for the first time. Kill application by pressing Control+C. For this last case we don’t need windbg.

What happens if CWOMEFLib.dll is copied to application directory while it is waiting for user input

Here we are going to check what happens if CWOMEFLib.dll is copied to application directory while it is waiting for user input for the first time. Move CWOMEFLib.dll from Release folder to some other folder outside solution directory (say to Desktop. Don’t copy cut it.)
  1. Open command prompt

  2. Navigate to Release folder of solution and start WOMEF.exe

  3. Application starts and waits for user entry with message “Enter 0 to quit, any other number to continue”. At this point move back CWOMEFLib.dll to Release folder.

  4. Enter a non-zero value and press enter

  5. Now when application tries to compile SayHelloToUser, it tries to refer type CWOMEF in WOMEFLib.dll and fails to resolve assembly. It comes out with an exception

  6. Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'WOMEFLib, ..


  7. That means, even though the assembly is made available when it is actually being referred, loader ignores to reload.

  8. It is this particular aspect that MEF tries to solve.

Existing possibilities

OK with that background I hope you are clear on what MEF is trying to solve. But before looking at MEF let us see how we can solve this particular problem with existing technology. With CLR 2.0 we have an option of loading assembly in runtime. After loading assembly in runtime we can create objects of types defined in it and we can even invoke them using reflection techniques. This is quite straight forward. I will just provide a working sample. Do following changes to our solution
  1. In WOMEF project remove reference to WOMEFLib.dll.

  2. Then replace code in Program.cs with following snippet

  3. using System;
    using System.Reflection;
    namespace WOMEF
    {

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Enter 0 to quit, any other number to continue");
    while (Console.ReadLine() != "0")
    {
    new Program().SayHelloToUser();
    }
    }

    public void SayHelloToUser()
    {
    Console.WriteLine("In SayHelloToUser");
    InvokeMethod("WOMEFLib", "CWOMEFLib", "SayHello", new object[] { Environment.UserDomainName+Environment.UserName });
    Console.ReadLine();
    }

    public object InvokeMethod(string assemblyName, string className, string methodName, object[] parameters)
    {
    System.Type[] parametersTypes;
    int parametersCount = 0;
    object returnObject = null;
    try
    {
    Type type = System.Reflection.Assembly.LoadFrom(assemblyName + ".dll").GetType(
    assemblyName + "." + className);

    parametersTypes = new System.Type[parameters.GetUpperBound(0) + 1];

    foreach (object parameter in parameters)
    {
    if (parameter != null)
    parametersTypes[parametersCount] = parameter.GetType();
    parametersCount++;
    }

    MethodInfo mi = type.GetMethod(methodName, parametersTypes);
    ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
    object objectinstance = ci.Invoke(null);
    //Invoke
    returnObject = mi.Invoke(objectinstance, parameters);
    }
    catch (TargetException tex)
    {
    throw tex;
    }

    return returnObject;
    }
    }
    }

  4. Here Invoke method loads assembly (WOMEFLib) in runtime, constructs class object (CWOMEFLib) and invokes a method (SayHello) in it.

In next post we will see how MEF framework not only simplifies this process but also provides many more useful options for tihis kind of scenarios.