Tuesday, June 24, 2014

Philly.net - Code Camp 2014 - Day 1

I was really excited to attend the Philly.Net code camp 2014 event as this is my first developer community event after I reached America that too with the reimbursement from my company. My expectations were huge as there are many MVPs taking sessions and can mingle with more intelligent and smart programmers. As of my analysis, ideally if somebody is smart, they eventually end up in United States provided they don't have his old parents who don't want to go US. Another interesting factor is the organizers will distribute more goodies / gifts to the attendees as they are taking registration fees :-)

The biggest difference from the events I had attended India and this code camp is the number of sessions and its schedule. In India the events I had attended conduct only one session at a time because audience and speakers are comparatively low in number. Regardless of the interest in the session, we had to sit until the next session starts. But here in Philly.net code camp there are around 72 sessions to be conducted in 2 days and there are 8 stages which means 8 sessions will go in parallel. In one way its good that we don't need to sit in a session which is boring. But in the other hand, if there are 2 or more sessions we want to attend, we can't.

Anyway just noting down my experience in the format of 'what I expected' and 'what I really got'. Selection of session was really tough as there are at least 2 sessions interesting at any time slot.

JavaScript for Devices by Andrew Duthie

What I expected

It will be purely hardware related where he will explain about boards such as Raspberry Pi / Arduino and show demo using the board. The javascript execution environment needs to be installed into the board where we can execute our javascript code. The javascript to hardware binding would be done using Assembly or C language and those details may also in discussion.

What I got

As per the short description in the schedule, he started with a presentation which tells what is micro controller and what is Espruino board and compared it with other board Tessel. These are the boards which are supporting the javascript out of the box. After 4-5 slides he started demo. Normally I have seen people showing demo about hardware in simulator as it is difficult to show the device to the entire audience. But for the demo he just used a camera which is on the table where he is playing with the device. As its connected to USB port, he was able to project it. Simple solution. Everyone can understand what the board is and how it work.

The board is connected to his laptop via COM3 and he used a program called Web IDE to communicate to the device. The Web IDE supports interactive mode as well as program mode. In program mode we write the program in the Web IDE and just push to the board and it start execution.

The board contains many LEDs, I/O pins which can be programmed using javascript. He also demonstrated how to read temperature using sensor connected to the board, started fan, LED matrix board controlling etc...

It was really nice, I got more than I expected. But only thing worrying me is the price $99 of the Espruino board. 

Refactoring to a S.O.L.I.D foundation by Stephen Bohlen

What I expected?

Ideally I planned to attend another session named "Using Azure to analyze Yammer data". But unfortunately that session got cancelled. So thought of attending this all time favorite SOLID session. I had attended couple of SOLID sessions earlier and myself took SOLID sessions to the developers. Whenever I took sessions the audience had complained about one thing. SOLID is good in theory and easy to talk but difficult to follow especially when we are working in delivery oriented service companies. So thought of getting some practical tips from this session which I can pass to my fellow developers

What I got

The session went well. He started with the 'swiss army knife' image to describe Single responsibility principle and reached to Dependency inversion very quickly. He started with an console app which is printing report and modified the same example to show all the principles throughout the session. I thought 'oh this is yet another session about SOLID". Then he started giving some practical tips. Peter Parker principle, Boys scott principle etc...Finally an advice of "If you have a huge legacy code base, refactor only the areas which are changing / going to change frequently". Why to spend time and money by refactoring the areas which are not going to change at all?

Another interesting information I got is about the .net exception class NotImplementedException. As per him this exception is added into framework to make sure all the auto generated code segments are getting compiled. For example we can use the IDE to implement the interface methods, if this exception is not there, the generated code will not compile. But as per the design of automatic code generation, the auto generated code should not create compilation errors hence they created this exception in framework. But in production code this NotImplementedException should not be present.

Practical machine learning - Recommender engine & anomaly detection by Seth Juarez

What I expected

To be frank I have not paid much attention towards Mathematics in college as I was thinking, that is not required for a computer science student. I concentrated in improving my logical skills and problem solving skills and up to an extent I succeeded in it. There was nobody to show me the practical uses of Maths in programming. If there was one, I should have given more attention towards it. 

I realized the problem when I was asked to draw a spiral 3D image in WPF as part of project requirement. From that time, I started paying attention towards maths. The same problem hit me when I started studying data analysis. It requires a huge amount of statistical knowledge along with other areas of Mathematics. So I was expecting a session where I can learn data analysis techniques very quickly.Kind of hacks. Also he might explain how the analysis can be applied in real life applications.

What I got

He started explaining what is the difference between science and engineering and why data analysis is called data science. As we all know in science, we cannot guarantee the success. We may get a solution or we may not. Its all research. I was happy as it enforced my view about data science.

Then he explained about different types of machine learning algorithms such as supervised and unsupervised . Then K-Means algorithm. I was trying hard to learn that for some long time. But after he demonstrated that algorithm using his own sample application, I was able to catch it up very quickly. He nicely explained how the distance calculation is used in these algorithms. Then session moved to recommender engine and finally to anomaly detection.

Debugging production web applications on IIS by Ed Ferron

What I expected

I have taken 'Debugging production apps' session in my company. So I was eager to get more practical oriented tips and tricks instead of how to create memory dump using DebugDiag and analyzing it using WinDbg or Visual Studio itself. I was also interested in tools which can make the debugger's life easier.

What I got

He explained pretty much same things like how to get memory dump and analyze it using WinDbg. I don't think he touched even the PerfView or how to debug the app using ETW logs. The interesting information I got is about intelliTrace and its collector which is the 'rich developer's tool'. When I was working in India VS Ultimate was our ultimate dream, which company never provided. But here in same company's US office, same project everybody has VS Ultimate. So now I can start thinking about intelliTrace.

Create an RDBMS-free app by David Hoerster

What I expected

I was expecting a guy who believes only in open source and he will explain details about how an application can be built without SQL Server or Oracle RDBMS. Also I can learn how transactions can be managed using NoSQL databases such as MongoDB.

What I got

I got an idea how we can introduce non RDBMS into applications which is running RDBMS. It will end up as a system where there will be RDBMS as well as non relational databases. His suggestions was to use non RDBMS for searching and storing logs. Interesting thing was he never mentioned the word NoSQL in any of his slides or sentences even though he talk about NoSQL systems. According to him the logging and search index updation doesn't need any transaction so its good place for non relational databases.

This was the one only session I have seen one person falling with his chair. I think he was sleeping.

Improving your application architecture by Ravi Okade

What I expected

So much expectations such as how to architect systems, how to select programming languages, how to scale etc...

What I got

He started with very basics such as what is data structure and what is algorithms. Explained about the sorting techniques. With each and every sorting technique, he was telling that its already taken care by .Net framework functions. Then he told about how to select serialization method for our applications. Interesting thing I got from this session is, the names of serialization protocols such as Apache Thrift, Microsoft Avro etc...He showed some comparison screens for different serialization techniques. 

The time was already running late and didn't find it much interesting. It sometime felt like he is reading the slides. There was no code too. So thought of searching for his slide sometime later.

Day 1 Overall 

  • 8 sessions are running in parallel so difficult to attend all the session which are interesting to me
  • The stages don't have good separation. So most of the time we can hear 2 speakers. Else the seat should be just opposite to the speaker in the front row.
  • Most of the sessions used less slides and more code. Really oriented towards programmer community.
  • No video recording was happening. If they record the videos, it would be really helpful to view the missed sessions.

Monday, June 16, 2014

You need to install the latest Silverlight Developer runtime before creating a Silverlight project

After a long gap, I installed Silverlight in my development machine to look into a performance issue in an internal company tool. As per the policy the "internal projects don't have the luxury of testers" so the actual testing happens on production hence so many issues especially performance. Its a Silverlight 5 project and I installed the Silverlight 5 tools from the Microsoft site. When I tried to open the solution the first error I got was given below.

You need to install the latest Silverlight Developer run-time before creating a Silverlight project
Download runtime at <url>

I thought, the Silverlight tools version I installed may be 32 bit version where its expecting 64 bit version. So downloaded the version which is mentioned in the error dialog. But when I tried to install it, the next message came.

Newer version of Silverlight is already installed

Interesting. Now no other way remove all the items from control panel by searching the word Silverlight and install only Silverlight 5. This I had did so many times when in active development of Silverlight applications. But this time that too didn't help. So decided to google.

The solutions people provided in internet are different. Some says we need to follow the link provided in the visual studio error message some posted a link from where we should download the run time, another one suggest to install Visual Studio 2010 Service Pack 1 to get Silverlight 5 working.

Installing VS 2010 SP1 takes more time, that is one of the reason why I didn't install it. As there are no other option I had to install VS 2010 SP1 and it started working!!!

These are the occasions, we think about the error messages we coded which is absolutely useless for the users. Thanks Microsoft...

Monday, June 9, 2014

We can't eat all the .Net exceptions using try catch block especially ThreadAbortException

Background

It is generally not recommended in .Net to control the application flow by throwing and catching exception. But in some cases people does it, to safely do the thing. In one of the WCF component in my project there is one scenario where the application flow is controlled via exception. The main intention was to keep the exception inside the thread itself and return a wrapper which says there was exception or not. Below is the stripped down code just for understanding.

internal class LongRunningProcessCaller
{
    private delegate ResultWrapper InvokeAsync(bool parameter);
    public void Invoke()
    {
        InvokeAsync invoker = new InvokeAsync(CallInvokerThreadHandler);
        invoker.BeginInvoke(truenew AsyncCallback(InvokeAsyncCompleted), null);
    }
    private ResultWrapper CallInvokerThreadHandler(bool test)
    {
        try
        {
            LongRunningMethodWhichMayThrowThreadAbortException();
            return new ResultWrapper();
        }
        catch (Exception ex)//All the exceptions should be caught and pass via Error property
        {
            return new ResultWrapper() { Error = ex };
        }
    }
    private void InvokeAsyncCompleted(IAsyncResult result)
    {
        InvokeAsync asyncDelegate = (result as AsyncResult).AsyncDelegate as InvokeAsync;
        //This method will hang, if there is any exception in CallInvokerThreadHandler
        ResultWrapper invokerResult = asyncDelegate.EndInvoke(result);
        if (invokerResult.Error == null)
        {
            Console.WriteLine("Errored");
        }
        else
        {
            Console.WriteLine("Worked");
        }
    }
    private void LongRunningMethodWhichMayThrowThreadAbortException()
    {
        System.Threading.Thread.CurrentThread.Abort();
    }
}
class ResultWrapper
{
    public Exception Error { setget; }
}

The problem

It went good for some time in production and slowly we started getting inconsistent issues from production. As it is inconsistent, we were not able to reproduce in dev machines and fix it. If we look at the code we can understand that inconsistent issue may happen, in case the catch inside CallInvokerThreadHandler() didn't eat and convert the exception properly.

The debug process

Since its using async delegate invocation mechanism we started thinking about exception behavior in threads and find nothing much. Then we put instrumentation code in catch block and pushed to environments where this issue is reproducing. This leads to investigation of ThreadAbortException which we were not expecting in normal course.

Further google about how to handle ThreadAbortException in IIS hosting environment, gives information only regarding the ASP.Net web sites related to Respose.Redirect. Some sites suggests to join the threads.
One says to resolve this exception, we can just increase maxConnections in web.config.

The Root cause

But our problem is different we have WCF service which calls another WCF service in different thread. The different thread is needed because the second WCF call is time consuming say 2-3 hours. Since the new thread is long running, there are chances for that to be aborted due to various reasons. Our aim should be to handle the failures properly. But what was the problem in the code listed above as it seems to be doing the purpose?

The root cause is, The ThreadAbortException  cannot be eaten by the catch block and we were relying on exception eating behavior.

So if the ThreadAbortException occurs the wrapping of the error result for returning to the completed handler will not work. The runtime will re throw the exception at the end of the catch which will make the EndInvoke in InvokeAsyncCompleted() to wait infinitely.

So rewrote the code as follows by avoiding the async completed handler.

internal class LongRunningProcessCaller
{
    private delegate ResultWrapper InvokeAsync(bool parameter);
    public void Invoke()
    {
        InvokeAsync invoker = new InvokeAsync(CallInvokerThreadHandler);
        invoker.BeginInvoke(truenullnull);
    }
    private ResultWrapper CallInvokerThreadHandler(bool test)
    {
        try
        {
            LongRunningMethodWhichMayThrowThreadAbortException();
            Console.WriteLine("Worked");
        }
        //All the exceptions should be processed here.
        //This is because the runtime rethrows ThreadAbortException from catch automatically.
        catch (Exception ex)
        {
            Console.WriteLine("Errored");
        }
    }    
    private void LongRunningMethodWhichMayThrowThreadAbortException()
    {
        System.Threading.Thread.CurrentThread.Abort();
    }
}

Happy coding...

Monday, June 2, 2014

Project Type GUIDs - Why I am not able to open some project files in VS?

Sometime back I got a solution from one of my research team mate. That was related to using Roslyn for improving coding experience in Visual Studio. It was working fine until I got new machine from new office. The error is famous as the VS. All the .Net developers will be familiar with the below error message.

The sad thing is VS doesn't tell that which project type is missing. Either we need to ask the creator what he meant, or look at his blog to get details. In my case he is in our offshore team and as of my knowledge he don't have any blog. At least if the VS tells that, this is the project type you are missing we could install it easily. 

So now what can I do?

Finding out Visual Studio project type from project file.


  • Open the .csproj / .vbproj file in a text editor (notepad.exe)
  • Find out the <ProjectTypeGuids> tag
  • Then google the GUIDs present in the element, if you have internet access.
  • Once the project type is known install that feature. eg: If its ASP.Net MVC install MVC
  • There are some web site where people keep a list of Project Type GUIDs and corresponding project type.
    • http://www.mztools.com/articles/2008/mz2008017.aspx
    • http://onlinecoder.blogspot.com/2009/09/visual-studio-projects-project-type.html
    • http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs

But I don't think Microsoft is providing any list. Its very sad.