Apr 13, 2018

Kendo Grid Binding Issue.. Multiple issues..


I was working on a Asp.Net MVC App on my current assignment where I stumbled on to a queer issue and though I should blog it for future reference which will be helpful for me or others. I had a Kendo Grid which I have to bind to a collection of Client objects sent from the Controller Action. The Controller Name is "ClientController" and the Action is "GetClients". Since it will be a the client ajax call, I am returning a JSon serialized collection. The codes looked like this.. 1. The cshtml code:
  @(Html.Kendo().Grid()
          .Name("grid")
          .Events(model => model.DetailInit("detailInit"))
          .Columns(columns =>
          {
              columns.Bound(model => model.ClientID).Width(25);
              columns.Bound(model => model.Name).Width(120);
              columns.Bound(model => model.Alias).Width(50);
          })
          .DataSource(dataSource =>
              dataSource.Ajax().Read(read => read.Action("GetClients", "Client"))
          )
          .ClientDetailTemplateId("client-template")
    )



    


    
2. The Controller code:
 public ActionResult GetClients([DataSourceRequest]DataSourceRequest request)
        {
           using (var db = new CMSDBEntities())
            {
                IQueryable clients = db.Clients
                    .Include(c => c.ClientContacts )
                    .Include(c => c.Projects)
                    .OrderBy(c => c.Name);
                DataSourceResult result = clients.ToDataSourceResult(request);
                return Json(result);
            }
        }
I was getting this error when I ran the code

Calling a Async method Synchronously


There was a need to call a async method in my code synchronously. All the various documented ways of calling it synchronously were failing.. I was struggling badly.. and finally stumbled into a piece of code which finally worked.. i will document all the failed and then the successful attempts.. Lets say, I have the following methods to be called

private void DoSomethingAsync(string text)
{
    ////some code here.....
}

private async Task DoSomethingAndReturnSomethingAsync(string text)
{
    ////some code here.....

    return "something something";

}

The various methods that I used to make the asynchrous from my API controller code are as follows: ====================Attempt 1:========================================

private void WriteToLog(string text)
{
    Task task = DoSomethingAsync(text);
    task.Wait();
}

This looks correct, but it does not work. The whole program freezes forever.
====================Attempt 2:========================================
Hmm.. Maybe the task was not started?

private void WriteToLog(string text)
{
    Task task = DoSomethingAsync(text);
    task.Start();
    task.Wait();
}

This throws InvalidOperationException: Start may not be called on a promise-style task.
====================Attempt 3:========================================
Hmm.. Task.RunSynchronously sounds promising.

private void WriteToLog(string text)
{
    Task task = DoSomethingAsync(text);
    task.RunSynchronously();
}

This throws InvalidOperationException: RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method.
====================Attempt 4:========================================
Used the most commonly prescribed procedure advocated in the net, i.e., to use the .reslt property of the Task object, which effective blocks the call and makes it synchronous

private void WriteToLog(string text)
{
    MyClass myObj = await DoSomethingAsync(text).result;
    or,
    Task task await DoSomethingAsync(text);
    MyClass myObj = task.result;
}

It too does not work. The whole program freezes forever like the .wait call().
====================Attempt 5:========================================
Next I used another oft recommended method in the net

private void WriteToLog(string text)
{  
    MyClass myObj = DoSomethingAsync(text).GetAwaiter().GetResult();
}

Unfortunately it resulted in the same, the code was unresponsive for a long time. Looking at the methods, I believe they are just wrappers to the wait() method and the Result Property, and hence behaved no different than them.
====================Attempt 6:========================================

private void WriteToLog(string text)
{
    var task = Task.Run(async () => { await DoSomethingAsync(text); });
    task.Wait();
}

This was the final piece which finally worked... Also, for receiving value back from the async method I used the following, a little variation of the same code..

private void WriteToLog(string text)
{
    var result = Task.Run(async () => { return await DoSomethingAndReturnSomethingAsync(text); }).Result;
    
}


====================================================================

Aug 3, 2016

Passing data from the container (cshtml or a partial view) to the inner partial view

If you just render a partial with just the partial name:
@Html.Partial("_SomePartial")

It will actually pass your model as an implicit parameter, the same as if you were to call:

@Html.Partial("_SomePartial", Model)

Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example: @model Namespace.To.Your.Model

@Html.Action("MemberProfile", "Member", new { id = Model.Id })

Alternatively, if you're dealing with a value that's not on your view's model (it's in the ViewBag or a value generated in the view itself somehow, then you can pass a ViewDataDictionary

@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });

And then:

@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })

As with the model, Razor will implicitly pass your partial the view's ViewData by default, so if you hadViewBag.Id in your view, then you can reference the same thing in your partial.

Passing data from the container (cshtml or a partial view) to the inner partial view

If you just render a partial with just the partial name:
@Html.Partial("_SomePartial")

It will actually pass your model as an implicit parameter, the same as if you were to call:

@Html.Partial("_SomePartial", Model)

Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example: @model Namespace.To.Your.Model

@Html.Action("MemberProfile", "Member", new { id = Model.Id })

Alternatively, if you're dealing with a value that's not on your view's model (it's in the ViewBag or a value generated in the view itself somehow, then you can pass a ViewDataDictionary

@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });

And then:

@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })

As with the model, Razor will implicitly pass your partial the view's ViewData by default, so if you hadViewBag.Id in your view, then you can reference the same thing in your partial.

Aug 1, 2016

Update a Auto Increment Column in SQL Server



set identity_insert [WorkFlow].[WorkFlowMaster] on

INSERT INTO [WorkFlow].[WorkFlowMaster]
           (
      [WorkFlowID]
     ,[WorkFlowCategoryID]
           ,[Name]
           ,[ShortName]
           ,[StepNumber]
           ,[IsFinalStep]
           ,[DocumentsRequired])
     VALUES
           (
      7
     ,2
           ,'Decision Received'
           ,'DecisionRcvd'
           ,6
           ,0
           ,0
     )
GO

delete [WorkFlow].[WorkFlowMaster] where [WorkFlowID] = 11;

set identity_insert [WorkFlow].[WorkFlowMaster] off

Jun 15, 2016

cannot build project in VS2015. Error Message "the operation could not be completed. The parameter is incorrect"

I had downloaded a project from the net containing a demo of a reporting website.
When I tried to run the project as is. It threw a Error.( which was basically shouting about the startup project cant be loaded etc.)
When I tried to compile the same, I got the following error  "the operation could not be completed. The parameter is incorrect"... Some intuitive message that...
I tried many things to correct it, 
Finally opened it in SafeMode and tried compiling.. to my surprise, I was building and threw up a a few errors regarding missing nuget packages..
This was a pointer towards the right direction at least.
I again loaded the project in VS2015 this time not in safe mode and then updated the existing packages and also noticed the dlls of all crystal report packages were missing.. Downloaded them individually removed all the issues and I could build it..
Hope this helps..

Apr 18, 2016

OWIN KATANA TopShelf Nancy

OWIN and KATANA:
http://www.codeproject.com/Articles/826757/Understanding-OWIN-and-Katana
http://www.codeproject.com/Articles/864725/ASP-NET-Understanding-OWIN-Katana-and-the-Middlewa
http://www.codeproject.com/Articles/864725/ASP-NET-Understanding-OWIN-Katana-and-the-Middlewa
http://www.asp.net/aspnet/overview/owin-and-katana



TopShelf:
Tutorial: http://docs.topshelf-project.com/en/latest/configuration/quickstart.html

Windows Service creation:
http://dontcodetired.com/blog/post/Painless-NET-Windows-Service-Creation-with-Topshelf.aspx
http://blog.amosti.net/self-hosted-http-service-in-c-with-nancy-and-topshelf/
https://lunaverse.wordpress.com/2012/03/30/self-hosting-webapi-as-a-windows-service-with-topshelf/

Good Technical Study Links:


MVC Fluent API EF









-------------------------------------------------

Simple Web Token



---------------------------------------------------------
Instrumentation and Performance Metrics


----------------------------------------------------------------------------

SOA Patterns and standards:








-----------------------------------------------------------------------------------------------------------------------

WebAPI / REST related:



-----------------------------------------------------------------------------------------------------------------------

Precompile controllers and views






Lazy(T) Class





-----------------------------------------------------------------------------------------------------------------------

Weak Reference Class








-----------------------------------------------------------------------------------------------------------------------

SOA Entity Composition:














Apr 12, 2016

ASP.NET pre-compile into a single assembly


Approach 1: (From StackOverflow)
The easiest way to do this is to Create (or change) your project as (or to) an "Asp.Net Web Application" rather than the "Asp.Net Web Site". The process of converting is not that hard, but you will need to move everything out of the app_code folder, as WAP (Web Application Projects) projects do not have code inside app_code.

This will cause your project to be compiled into a single dll, rather than the "dll per page" or "dll per directory" scheme that the web sites normally work off of. It will also force you to resolve any type name problems (IE: if you have 6 pages called Default), It's important when you make a single assembly that fully qualified names be unique.

This will give you a proper project file (build script) and dump all your dll's out to the /bin folder when you compile.

You can then use something like ILMerge to combine all your assemblies in the /bin folder into a single dll if that's what you want to do. This assumes all your references are managed assemblies, if you have any non .Net libs it's going to be a lot trickier, although still not impossible.


Approach 2: Use the aspnet_merge.exe Utility to Merge Assemblies
how-do-i-use-the-aspnet_mergeexe-utility-to-merge-assemblies


Approach 3: Compile MVC project to a single DLL embed your js and css files. When you publish, in the configure part of the settings tab (see capture below), be sure to uncheck "allow precompiled site to be updatable" <--- a="" and="" assembly="" br="" chose="" compile="" control="" cshtml="" in="" merge="" page="" single="" the="" will="">

By default there is an assumption that whoever has access to your views and DLLs is trusted. If they have your files, they can do whatever they want with them.

By the nature of HTML, there is no point in trying to conceal your content files such as javascript and CSS. These files are served to the client regardless, so they are always retrievable.

If you want to put your views into DLLs, you can look into RazorGenerator.

A Custom Tool for Visual Studio that allows processing Razor files at design time instead of runtime, allowing them to be built into an assembly for simpler reuse and distribution. Please note that what you're doing is known as "Security through obscurity".(Security through obscurity is the use of secrecy of the design or implementation to provide security).Security through obscurity is discouraged and not recommended by standards bodies.


Approach 4: Packaging ASP.NET ASPX Pages into a separate Assembly

Read here

Find Max length of string array


My requirement was to get the string with the longest value and the shortest value from a huge array. Naturally I was inclined to use the Max() and Min() extensions of Array. But it was throwing queer results. I was stumped, why was it not behaving uniformly? Then I realized for Max or Min on a string all it does is a alphabetical sorting and returning the top and the bottom of the sorted list. I also added a simple lambda solution to it.


Feb 1, 2016

Configure ASP.NET SignalR in a web farm


I had a problem after I put into production a Visual Studio project with SignalR library.

In localhost every call seemed to be right but after the publishing step, some random request returned me an http error. After some google search I found that the problem was that the site is published in a web farm with 7 differents servers. In this case could happen that the first call is send from one server and managed from another server in the farm.

Every server has its encryption and decryption own code and if you want SignalR calls return a valid http status you should share these keys from the servers in the farm. So for each server in the farm open IIS and go to Sites and single click in the site where SignalR is used. In the central panel now go to Machine Key and double click.

Uncheck “Automatically generate at runtime” and “Generate a unique key for each application” and put a valid unique validation Key and a valid unique decryption key in the text boxes.

Look at the picture below, you should have something similar to this.

[Excerpted from: http://nextstepblog.altervista.org/configure-asp-net-signalr-in-web-farm/?doing_wp_cron=1454300698.7938909530639648437500]



Other Important Links:

http://www.asp.net/signalr/overview/performance/scaleout-with-sql-server

http://www.tugberkugurlu.com/archive/scaling-out-signalr-with-a-redis-backplane-and-testing-it-with-iis-express

http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx

Jan 19, 2016

Using Fiddler to test Web API Rest Services and pass an array as parameter + Filtering

1. In Fiddler’s Composer tab, Select the "Parsed" tab.

2. In the same tab, Change GET to POST and enter the address of your Web.api controller (“http://yoursite.com/api/yourControllerName”).

3. Again, under the same tab, In the Request headers Text Box Area (The heading 'Request Headers' may not be visible), enter a JSON content type. “Content-Type: application/json; charset=utf-8″ (w/o the braces). 4. Again, under the same tab, Put your JSON string array (Input param) in the Request Body. e.g., ["aaa","bbbbb","cccccccc"].

5. Click on "Execute". it will hit the controller and you can debug.

To Filter the huge number of network communications recorded in the Fiddler

1. Click on the "Filters" tab, Select "Use Filters"

2. In the 1st drop down under the Hosts Heading, Select "Show Only Intranet Hosts", in the second drop down, select "Show only the following Hosts"

3. In the following text box enter: Enter the local url for e.g. "http://localhost:31314/", here, I was hosting the service in IISExpress.

Dec 17, 2015

Fluent code in c sharp

In 2005, the object-oriented programming expert Martin Fowler published his essay on the ‘fluent interface’. He described an approach for building software with more readable code that could be more easily maintained by developers because it is easier to read, and discover how to use, than a traditional API that contains functions with a number of parameters. While the fluent interface might have been an idea ahead of its time when the technique was first published, one now sees terms employed to describe code, such as, “fluid coding”, “fluent style” and “fluent API”, which suggest its time has arrived.


In LINQ, the 'fluent' method syntax flows logically and intuitively, and allows them to be combined simply, because each method returns the appropriate type of object for the next. Can this fluent technique be extended as an API style to make it easier to develop C# team-based applications for enterprises?

Read about it here

A-Look-at-Fluent-APIs

Dec 10, 2015

Compile your asp.net mvc Razor views into a separate dll

A "step-by-step" guide on how to compile your razor views into a separate dll. Good post about how to package your MVC razor views into a shared .NET assembly that you can use in multiple projects. In the past, I often had duplicated views / helpers in each project that was using ASP.NET MVC 3 w/Razor. This was getting to be a maintenance headache, so I looked around and saw Chris Van De Steeg’s post about how to actually accomplish this.
Pre-compiling-razor-views

Precompile-mvc-views

Detect-errors-of-asp-net-mvc-views

Dec 4, 2015

Web Designing : screen-patterns

http://designingwebinterfaces.com/designing-web-interfaces-12-screen-patterns

http://blogs.msdn.com/themes/blogs/generic/postlist.aspx?WeblogApp=infopath&PageIndex=3


Nov 18, 2015

Asynchronous Programming with Async and Await (MSDN) + Extra

https://msdn.microsoft.com/library/hh191443.aspx

https://code.msdn.microsoft.com/Async-Sample-Example-from-9b9f505c


http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html




(Excerpted from http://www.abhisheksur.com/2010/10/c-50-asynchronous-made-easy.html)



What is await ? 

According to Whitepaper published on the CTP, await keyword lets you to wait your program and lets the program to be declared as a continuation of all the existing awaits so that the compiler automatically waits for all other awaits to be finished until the program continues its execution. 
In other words, say you have invoked 3 asynchronous methods from your code an asynchronous block from your code, you would just write the way you would have written this synchronously with a single keyword await before the call. The call lets the compiler wait until the execution successfully completes. Just as shown below : 
async Task GetStringsFromUrlAsync(string url)
{
     WebClient client = new WebClient();
     return await client.DownloadStringTaskAsync(new Uri(url));
}

This involves the call to DownloadStringTaskAsync which is already implemented for WebClient. Now as you can see I have specified the await just before the call to Asyncrhonous method DownloadStringTaskAsync, it will keep the call on wait. 

Even though if I say use :

async Task GetStringsFromUrlAsync(string url)
 {
      WebClient client = new WebClient();
      Task<string> modifieduri = client.DownloadStringTaskAsync(new Uri(url));
      string result = await modifieduri;
      return await client.DownloadStringTaskAsync(new Uri(result));
}

It means the second Task depends on the first one. So in this case the First DownloadStringTaskAsync will be called. This call actually returns Task(Task for any other operation) which you can keep on wait until it is finished using await keyword. So when you execute, the first call will block until the result is fetched and then it will take the string result and call the next one. 

Thus you can see how you can use await on Task variable to wait the object for completion without any implementation of custom callbacks, global objects etc.


What is async? 

Now, how to implement the asynchronous method? Hmm, async keyword can be placed just before the function call to make the method asynchronous. 

By specifying the async keyword against your method call, the compiler will automatically select your method to be asynchronous, which means it will move to the next line before returning the result in parallel until it finds an await. In the example I have provided theDownloadStringTaskAsync is actually an extension which specifies to be called with async.

Async method can return void, Task or Task based on what you need in your environment. But it is mostly preferable to return at least a Task object so that the caller can better handle the call. 

For any async method, you should put await before fetching the result, otherwise you might end up with blank/null result. await works with async to stop the further execution before finishing the other operations in context.

C# 6.0: Language features and its internals Part 1

Learn about the enhancements in both the CLR and IL in C# 6.0:
---------------------------------------------------------------------------------------
(excerpted from http://www.abhisheksur.com/2015/03/c-60-language-features-and-its.html)

The feature set which have been added / modified as part of C# 6.0 are:

1. Auto Property Initializer
2. Expression bodied Function
3. Static Class Uses
4. String Interpolation
5. Null Conditional operators
6. Exception filters
7. nameof operator
8. Dictionary initializers
9. await in try / finally.
10. Parameterless constructor for struct
------------------------------------------------------------------------------------


Feature 1 :  Auto Property Initializer

Auto properties are not new to C#. It was there since C# 3.0. The auto properties does not require a backing field to be defined which was done automatically during compilation. So as a coder, you can get a ready-made property without writing much of code. As auto properties does not have backing field exposed, you cannot initialize the backing field during object initialization, rather you have to initialize explicitly in constructors.




    
public class AutoPropertyInitialier
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string FullName { get; set; }

        public AutoPropertyInitialier()
        {
            this.FirstName = "Abhishek";
            this.LastName = "Sur";

            this.FullName = this.FirstName + " " + this.LastName;
        }
    }
You can see in the above code block, the FirstName and LastName were Auto - Implemented properties, and both of them were initialized during constructor call, as the backing fields are not exposed for them. Think of a situation when your class is pretty big and you want to introduce a new property, you need to again initialize it inside constructor. This is the pain of the developer.

With C# 6.0, the initialization of auto-implemented properties is damn easy. Take a look into the code below :



    public class AutoPropertyInitializer
    {
        public string FirstName { get; set; } =  "Abhishek";
        public string LastName { get; } = "Sur";

        //public string FullName { get; } = this.FirstName + this.LastName;

        public string FullName { get; }

        public AutoPropertyInitializer()
        {
            this.FullName = this.FirstName + " " + this.LastName;
        }
    }

Here the properties FirstName and LastName were initialized directly after the declaration. You can notice the LastName were intentionally made readonly by not mentioning the setter. The backing field produced for a readonly property is also a readonly variable.

The FullName though is initialized inside constructor as the object initialization works before the constructor call, and hence initialization cannot evaluate "this".


In the above image, the C# representation of actual IL is shown which clearly indicates that the properties do have their backing fields. You can also notice the backing fields for property LastName is defined as readonly. The auto-property initialization is handled very cautiously during compilation and is abstract to you and relieves some of the pain points on writing un-necessary code. 

Feature 2 : Expression bodies functions 
Similar to Auto property initialization, C# 6.0 also comes up with expression bodies functions a long awaited feature. Expression bodied function is also a syntactic sugar implemented in the language which adds the flexibility on how you define member functions. 

Lambda expressions are not new to the language, but you might already know you cannot assign a lambda to a member function if the member function is not defined with a delegate. But if you read my internals to Lamba expression, you already know that for each lambda declaration the compiler creates a backing function body, but that function body cannot be exposed to class level. C# 6.0 enhances the language to allow you to define a member function with lambda expressions. 




    public class ExpressionBodiedFunction
    {
       
        public string WelcomeMsg(string name) => string.Format("Welcome {0}", name);

        public string HelloMsg => "Abhishek";

        public void Print() => Console.WriteLine(this.HelloMsg);

    }
In the above code block the WelcomeMsg is a member function that takes a string argument and returns a string. As per lambda rules, a lambda which has a return statement does not require a return keyword, hence the WelcomeMsg returns the expression with string.Format.

The second property is HelloMsg which is a property like method where the getter of the property HelloMsg will return the string "Abhishek". Remember, as property is also auto-implemented, the get keyword is also automatically evaluated by the compiler.

The last method "Print" is a void method and hence require a non-returning expression. The Console.WriteLine calls the property HelloMsg to print "Abhishek".

Now how does the compiler handles the lambda now without an explicit declaration of delegates ? Well, the answer is simple. The compiler re-writes the lambda into a full functional method body.


Here the WelcomeMsg, HelloMsg and Print after compilation is just a class level methods. The compiler entirely re-written the class into a strongly typed method bodies.



Feature 3 : Static class uses
The C# language teams identified the developers pain points very well, and implemented the Static class uses in the language. If you are using Static types it is always a nice to have feature in the language such that we can omit the repetitive static method call.

For instance, say I want to print a number of lines on screen, using Console.WriteLine. How about if we don't need to mention Console each line when we call WriteLine ? Static class uses does the same thing. Static class uses also recognize extension methods, hence if say a static class defines extension methods, if you add it, it will automatically recognize the extension methods as well.



    using static System.Console;
    using static System.Linq.Enumerable;

    public class UseStatic
    {
        public void CallMe()
        {

            WriteLine("Hi");

            var range = Range(10, 10);
            var check = range.Where(e => e > 13);
            // Static extension method can 
            // only be called with fulll representation
        }
    }


In the above code the WriteLine is automatically detected as we added the head using static statement. The Range is also declared inside System.Linq.Enumerable which is automatically determined and Where is an extension function. The static uses automatically recognized the extension methods as well.

If you look at the internals of the static uses, the compiler rewrites the static type calls on every occurrences of its method call.


In the actual IL, you can see, the WriteLine is padded with its appropriate type Console and Range is also associated with Enumerable. The re-writing is done by the compiler.


Feature 4 : String Interpolation 

String interpolation is another interesting feature introduced with recent version of C# 6.0. Interpolation is not new with strings. We can do string interpolation using string.Format before, but the problem is the code looks really ugly when using interpolation constructs.

For instance,


string myString = "FullName :" + p.First + " " + p.Last;
string myString = string.Format("FullName : {0} {1}", p.First, p.Last);
Here the first instance, is normal string concatenation. This creates an issue with multiple memory allocation if not string interned. The second statement uses string.Format to format a given string and replaces the {} arguments with the proper arguments as parameter to the method. Now while dealing with multiple occurrences of these kind of string placeholders might look very ugly and un-maintainable and error prone.

C# 6.0 comes with a new technique where the string interpolation can take place in between the strings, or rather than putting numeric placeholders, you can use the actual values.



string myString = $"FullName : {p.First, 5} {p.Last, 20}";
Console.WriteLine(myString);

string my2ndString = $"FullName from EBF = {this.EBF.FullName}";
Console.WriteLine(my2ndString);

Here Feature 4 :the string need to be started with a $ sign which indicates that the new kind of string interpolation to occur. The {p.First, 5} will be replaced with the value of p.First aligned to 5 characters.

Now what does the compiler do when these kind of interpolation is encountered. Well, you might be surprised to see, that the string is actually replaced by a call of String.Format during compilation.


So here you can see the actual statement after compilation is nothing but a string.Format call. So string interpolation is also a compiler trick. 

Feature 5 : Null conditional operators

Getting into NullReferenceException or "Object reference not set to instance of an object" is one of the most common exception that every developer must see while working with the langauge. It has been a common problem where developers need to give extra stress to check the null value before calling any member functions. Null for compiler is unknown, and hence if you call a method on null, it will point nowhere and hence throws NullReferenceException and exists the stack.

To handle nulls Microsoft had introduced few language constructs already like Null coalesce operator (??) or Conditional operators. But with the new feature introduced in C# 6.0, the language team has introduced another operator which checks for null.

Null Conditional operator is a new construct that gives away a shortcut to condition the null check before calling its members.



string zipcode = (address == null? null : address.zipcode);

string zipcode = (person == null? null : (person.address == null ? null : person.address.zipcode))

Here in the above two lines you can see how the complexity rises when there are multiple number of null checking on an expression. In the first statement the address is checked with null and when it has address, it will only then evaluate the zipcode. On the second statement, the person object is checked with null, then its address and then finally the zipcode is evaluated.

With null conditional operator the code would look like :



string zipcode = address?.zipcode;

string zipcode = person?.address?.zipcode;

Here the code is reduced greatly even though the actual expression logic remains the same. The ? mark is used for null conditioning. Null conditional operators does not exists as well, it is the same representation as that of the previous conditional operators. If you try to look into reflector, you will see it like :



You can see the null conditional operators are replaced by the compiler with normal conditional statements.


Feature 6 : Exception Filters

Now after so many syntactic sugar introduced with latest version of C#, this is the only new feature that directly relates to IL (Intermediate Language). From the very inception, the exception filters were there in CLR but was not exposed to the C# language. With C# 6.0, the exception filters finally came into existence and finally as a C# developer you can make use of it.

In C# the exception block contains try/catch/finally with multiple catch blocks supported. Based on type of the exception that has been encountered during runtime, the runtime chooses the appropriate catch block to execute. Exception filters allows you to filter out the catch blocks to ensure which catch block can handle the exception. With exception filters in place, there could be multiple catch blocks with same type where the type determines which filter it will call first.

Let us take a look at the code :



public class ExceptionFilters
    {
        public static bool LogException(Exception ex)
        {
            //Log
            return false;
        }
        public static bool LogAgainException(Exception ex)
        {
            //Log
            return true;
        }
        public void CallMe()
        {
            try
            {
                throw new ArgumentException("Exception");
            }
            catch (Exception ex) when (LogException(ex))
            {
                //Keep exception stack intact
            }
            catch(Exception ex) when (LogAgainException(ex))
            {
                // Calling the catch
            }
            
        }
    }

Here the ArgumentException is thrown when CallMe is invoked. The Exception filters are specified using when keyword just like in VB.NET. So the execution is like, it will call LogException method, executes the code and see whether the return statement is true or false. If it is true, it executes the catch block and exit, or otherwise it move and call LogAgainException method.

The exception filter expects a boolean value, hence the call to LogException should be a method returning bool or otherwise we need a comparison operator to always evaluate the when statement to boolean. Exception filters allows the developer to run arbitary code between the exception and the catch block execution without interfering the original stack trace of the exception object.

I have already told you that exception filters is a CLR concept. The IL is laid out as :


 .try --- to --- filter --- handler --- to ---

This tells the compiler to execute try block from line no --- to line ----. It will then call filter line and then call the handler which executes some bunch of lines. Cool ? Yes.

One small addition that I would want to add, if an exception filter encounters an exception and is left unhandled, the compiler handles it for you and automatically skip the exception block and moves to the next catch block. For instance if LogException in the code above throws an exception, it will call LogAgainException without modifying the Stack for ex.


Feature 7 : nameof Operator

nameof is a new addition to operator list supported by C#. This new operator is nothing but a compiler trick, where the compiler determines the name of a Property, Function or an Extension method and writes it directly in compiled output. For instance,



string name1 = nameof(this.MyProp); // evaluates MyProp
string name2 = nameof(DateTime.Now);//evaluates Now
string name3 = nameof(Console.WriteLine); //evaluates WriteLine
string name4 = nameof(new List<string>().FirstOrDefault); // Evaluates FirstOrDefault
string name5 = nameof(name4);//Evaluates name4

The nameof operator works on any type where the type has a name. In case you specify a variable, the nameof operator will evaluate to the name of the variable.


Feature 8 : Dictionary initializers

Dictionary is one of the common array object which used often to send data between objects. Unlike arrays, Dictionaries form an array of KeyValuePair. In modern days, Dictionary is used interchangeably with arrays because of its flexibility of defining key value set.



var list = new List<string>();
list.Add("Abhishek");
list.Add("Abhijit");

//Access
var first = list[0];
var second = list[1];

Here you can see a list is used to store string data, where index giving you the reference to individual objects. If I write the same with dictionary, it will look like :



var dict = new Dictionary<int,string>();
dict.Add(0, "Abhishek");
dict.Add(1, "Abhijit");

//Access
var first = dict[0];
var second = dict[1];

Even though the basics remain the same, the developers are increasingly inclined to use the later as it gives a flexibility to assign any index. You can define "Abhishek" to 10 and "Abhijit" to 20 without the limitation of fixed incremental index usage of dictionary.

As more and more people are going to Dictionary, language team thought about giving easier constructs to define Dictionary. Even though we can go without it, but it is a nice to have feature. In C# 6.0, you can define dictionary using the following syntax :

var dict = new Dictionary<int,string>
{
   [10] = "Abhishek",
   [20] = "Abhijit"
};

//Access
var first = dict[10];
var second = dict[20];

Or even if the key is declared as string, you can also use like:


var dict = new Dictionary<string,string>
{
   ["first"] = "Abhishek",
   ["second"] = "Abhijit"
};

//Access
var first = dict["first"];
var second = dict["second"];

This way it is very easier to declare and use dictionaries. The last two syntaxes were introduced in C# 6.0.

Now if you think of the actual implementation, there is nothing as such. If you try to sneak peek on the compiled code, you will be seeing the same code with object being initialized inside the constructor.



Here even though the new construct is used, the dictionary object is actually initialized in constructor itself. The code is re-written by the compiler.


Feature 9: Await in catch and finally

If you are not using async / await stuffs too often you might have already overlooked that you cannot specify await in catch or finally block before the latest version of C#. Microsoft has initially thought that it wont be possible to allow awaits in catch/finally because the compiler creates a state machine which slices each and every await statements into a sequence of calls which can pause and resume the function upon request. But as try/catch/finally are handled by CLR, allowing such thing will indicate every errors in the state machine is properly handled and also each of try/catch/finally would need their individual state machines each.

Based on the complexity, microsoft haven't provided the feature in previous version, but as everything is handled by the compiler itself, it is a very important feature to have as a programmers perspective hence in latest version of C# 6.0, the feature have been revised and await in catch/finally is allowed.



 public async Task<string> DownloadUrl(string url)
        {
            WebClient client = new WebClient();
            client.BaseAddress = "www.abhisheksur.com";
            try
            {
                return await client.DownloadStringTaskAsync(url);
            }
            catch
            {
                return await client.DownloadStringTaskAsync("mvp");
            }
            finally
            {
                //Await is also allowed.
            }
        }

Hence you can successfully compile the code above where the await works during the catch execution.

If you try looking at the code in reflector it will look very ugly with so many of StateMachine declarations and so many of goto statements to properly navigate to lines inside the state machine on failure. It would be nice if you can look into it personally, but if there is any problem understanding the implementation, feel free to ask me in discussions.

But as a benefit, you get this feature ready.


Feature 10: Parameterless constructor for struct

If you are unaware, there was no parameterless constructors in struct.  This is because of the fact that ValueTypes has a unique behavior where it will be automatically assigned a default memory when declared.

For instance,


int x;

Here int is a struct (System.Int32) and the value of x would be automatically 0. You can even declare array of a struct. For instance,



struct MyStruct
{
  public int Property1{get;set;}
  public string Property2 {get; set;}
}

MyStruct[] array = new MyStruct[10];

Now the Struct MyStruct will automatically get the default(MyStruct) in the array even though its not explicitly constructed. If you want to know more why struct does not support default constructor, feel free to read the article I posted .

In C# 6.0 the parameterless constructor is allowed. This is also a trick made by the compiler. It actually replaces the default constructor with the parameterless constructor passed in when the new operator is used to construct the object.

So if you say,


MyStruct s = new MyStruct();

The .NET will call the parameterless constructor for you.

Apr 14, 2015

How to hard reset Visual Studio instance

When developing extensions sometimes you just mess up, others someone else does. If you start getting errors loading even the most mundane extensions, these are the instructions to hard reset your instance.
1. Close Visual Studio (if you haven’t already).
2. Open the registry editor (regedit.exe)
3. Delete the HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\{version}
4. Delete the HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\{version}_Config
5. Delete the %USERNAME%\AppData\Local\Microsoft\VisualStudio\{version} directory.
  • Enjoy your brand new Visual Studio instance.
Use {version}=10.0 for Visual Studio 2010
Use {version}=11.0 for Visual Studio 2012
Use {version}=12.0 for Visual Studio 2013
If on the other side you want to reset the experimental hive you can do the same to with the ‘{version}Exp’ ones.

(source: http://www.corvalius.com/site/hacks/how-to-hard-reset-visual-studio-instance/)

Apr 4, 2015

Connect to a SQL Server DB which is on a client Network while you are using your office PC

Trusted Authentication will use the credentials that you are logged into the machine with to try and connect, there’s not a way round this, if you want to use trusted authentication for management studio, you need to be logged on to your machine with an account that is allowed access to SQL, if not then you will have to use SQL authentication.

Try this: Use RUNAS to set your Windows Auth domain for database connections

runas /user:domain\user "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\ssmsee.exe"

runas /user:domain\user "C:\WINDOWS\system32\mmc.exe /s \'C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC\'"

runas /user:domain\user isqlw





Example:

runas /netonly /user:cricket\mdatta " C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\ssms.exe"

runas /netonly /user:cricket\mdatta isqlw