Web Services : Data flow between the UI view and the backend APIs

These two articles helped me a lot in understanding how to handle data passing between the UI view and the backend APIs.

References:

  1. https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data
  2. http://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-1
  3. http://carehart.org/blog/client/index.cfm/2007/1/2/form_self_post

 

Git : Steps to take after getting a sign off in code review branch

Steps to take after getting a sign off in code review branch.  feature/masquerading (say)

— Go back to master

  • [feature/masquerading]> git checkout master

— get latest code from master

  • [master]> git pull origin master
  • [master]> git checkout feature/masquerading

— rebase with master

  • [feature/masquerading]> git rebase master
  • [feature/masquerading]> git add readconfigapp2/Program.cs
  • [feature/masquerading]> git rebase –continue
  • [feature/masquerading]> git checkout master

— merge and push master

  • [master]> git merge feature/masquerading
  • [master]> git push origin master

— delete CR branch

  • [master]> git branch -d feature/masquerading
  • [master]> git push origin –delete feature/masquerading

Azure Machine Learning : Model Retraining

References:

  1. https://azure.microsoft.com/en-us/documentation/articles/machine-learning-retrain-models-programmatically/
  2. https://azure.microsoft.com/en-us/blog/retraining-and-updating-azure-machine-learning-models-with-azure-data-factory/
  3. https://gallery.cortanaanalytics.com/Tutorial/No-code-Batch-Scoring-and-Retraining-1
  4. https://azure.microsoft.com/en-us/documentation/articles/machine-learning-consume-web-services/

Visual Studio : Project Structure

Have always been a bit curious to understand about VS’s project structure. Also, the use of config files and stuff. So here goes :

  • I create a solution ‘vsprojstructure’
    • I eventually wan to create a git repo out of it.
  • class library ‘configlib’
    • It has an app.config file
  • class library ‘algolib’
    • when algolib is independent, it doesn’t know anything about ‘configlib’
    • when i add ‘configlib’ as a reference, 2 things happen:
      • VS automatically updates the ‘Project Dependencies’ for ‘algolib’ project. It marks that ‘algolib’ depends on ‘configlib’
      • when i build the project, it copies ‘configlib’s dll and pdb files inside ‘algolibs’ bin\Debug folder
      • Note : it does not copy the .config file. This is worrisome
  • Now add a console app called ‘demo’
    • ‘demo’ has ‘algolib’
    • when i build the project, it copies ‘algolib’ and ‘configlib’s dll and pdb files inside ‘demos’  bin\Debug folder. However compilation fails (see code why)
    • So i also add ‘configlib’ as a reference. Compilation succeeds now.
    • However, it shows ‘AppSettings is empty’. This makes sense because as we noted before, the .config was never copied
  • It seems the only way to solve this problem is via post build events.
    • See reference.
    • Using build events.
    • This explains why in many cases we see projects having post build events writing to a common folder
  • Also, note by default the ‘demo’ exe will try to read its own exe. The .config file in the configlib directory needs to be crafted to ensure it reads its own .config file

 

Examples:

1.

robocopy $(SolutionDir)configlib\bin\Debug\ $(TargetDir) configlib.dll.config /xo /xn
IF %ERRORLEVEL% GEQ 8 exit 1
exit 0

2.

robocopy $(ProjectDir)dependencies $(SolutionDir)bin\$(Configuration) /xo /xn
IF %ERRORLEVEL% GEQ 8 exit 1
exit 0

Pro Tip:

  • A critical aspect is to understand the OutputPath parameter in the .config file
  • For each project, this is what controls where the output is going to.

Code:

Github

References:

 

 

Git : Pull Requests, Branches

Tips:

 

 

References:

  1. http://www.gitguys.com/topics/switching-branches-without-committing/
  2. https://lostechies.com/joshuaflanagan/2010/09/03/use-gitk-to-understand-git/  
    • Super useful!!

Threading. Singletons. Generics. Locking Bug!

I recently came across this very interesting bug.  Note: the problem requires understanding of threads, generics, inheritance and locks.

In particular look at the Locking Bug code. Specifically, notice how the locking behaviour changes based on the types used for the generic base class.

  • When the types are same, the base class object used for locking is the same.
    • So locking can be enforced by doing something like
      • private static readonly object Padlock = new object();
      • lock (Padlock)
  • However, when the types are different, the compiler creates two completely different memory objects, hence the locking does not come into play
    • A possible solution is to create a separate internal class and enforce locking using the typeof that class
      • internal class ForLocking {}
      • lock (typeof(ForLocking))
    • If you dont prefer using typeof, another option is to create a separate class and have a static lockObject in it.
      • See code.
    • http://stackoverflow.com/questions/8185004/lockx-vs-locktypeofx

Code:

References:

Static Keyword in C#

Static has all these interesting semantics around it.

Basics:

  • The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.
  • A constant or type declaration is implicitly a static member.
  • A static member cannot be referenced through an instance. Instead, it is referenced through the type name
  • While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
  • It is not possible to use this to reference static methods or property accessors.

Static Class:

  • If the static keyword is applied to a class, all the members of the class must be static.
  • A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class

 

Static Members / Methods:

 

Static Constructors:

(I once used a static constructor for my web service, when I had to initialize some of the static data in my class.  The other alternative to not using the static constructor was basically to find a static function in a static class which I knew would have gotten called only once when my web service was getting initialized. See Code for pointers)

  • Classes and static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated
  • A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
  • A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
  • If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

Code:

Reference:

  1. static :
  2. static classes and static class members :
  3. static constructors
  4. singleton:

Async Await in Web Service Design

Have been wanting to understand the Async Await Pattern in all its glory. Recently got the opportunity to finally jump head on

Tips:

  • Async Await & Threading :
    • Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don’t cause additional threads to be created. Async methods don’t require multithreading because an async method doesn’t run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn’t help with a process that’s just waiting for results to become available.
    • https://msdn.microsoft.com/en-us/library/hh191443.aspx

 

  • UI thread of an ASP.NET application needs special handling. Else there could be deadlock issues
    • http://blog.ciber.no/2014/05/19/using-task-configureawaitfalse-to-prevent-deadlocks-in-async-code/
    • Interestingly I found Console Apps can automatically handle this issue by spawning a different thread.  But ASP.NET Apps require you to explicitly put in the .ConfigureAwait(false)
    • e.g. result = await DoSomeTask().ConfigureAwait(false)
    • The reason the Console Apps behave differently is very well explained in the link below :
      • All of the UI application types you can create in Visual Studio will end up having a special SynchronizationContext published on the UI thread. Windows Forms, Windows Presentation Foundation, Metro style apps… they all have one. But there’s one common kind of application that doesn’t have a SynchronizationContext: console apps.
      • http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx

 

  • For debugging : Use Debug.WriteLine and use the ‘Output’ windows in VS

 

Code:

References:

Web Services : Understanding IHttpActionResult

Tips:

References:

 

Code:

DecisionController