Tuesday, December 6, 2016

Live Unit Testing in Visual Studio 2017

Let’s have a look at one of the coolest feature of Visual studio 2017. If you will see below snapshot, there are many new icons here. 

























These icons are part of cool new feature called Live unit testing in Visual Studio 2017 Enterprise.

Live Unit Testing continuously runs and displays unit test result and code coverage inside editor itself. It automatically finds and runs impacted tests for every line of code. In above diagram, Red cross indicates failed test case, Green tick indicates passed test case and Blue minus indicates the code that have no test coverage at all.

Let’s start by looking at failing test case. You can quickly navigate to failing test case by clicking on tooltip as shown below: 






And below is my test case:











At this point, I’m not really sure why this is failing. So, I’ll go ahead and debug this test
We can see here that above particular code has thrown a null reference exception.
Please note that a new exception handler has been introduced in VS 2017. Now one can see complete exception details here itself.








Now as I get to know the cause of failure, I quickly went and initialized my field in my constructor as:







As soon as I updated my code, editor is showing test case as passed. Hope you enjoyed learning new feature.

Friday, December 2, 2016

Performance and Memory tips

Two things play a very significant role in any application development, and those are application’s footprint and performance. Whenever I’m asked to work on these two tracks, I used to visit a huge list of sites to get many more ideas apart from what I already know. So, I thought, why can’t I collate all the good points and add them to my repository. At the same time, I thought of sharing those points in this blog.

Rather than making this blog post full of theory, I’m planning to make it simple by just adding the bullet points.

Uhh! Enough of gossip. Let’s get started by going through some common and important rules.
  • Create object only when it is really required
  • More the objects lesser the performance :( 
  • Grab resources, use them and release at the earliest
  • Default capacity of StringBuilder is 16. So, if you want to store less than 16 values then make sure to set the capacity.
  • Avoid unnecessary boxing and unboxing
  • Prefer lazy loading
  • Use Static variables cautiously as they will stay live throughout application life
  • Avoid using IDisposable everywhere
  • For web apps, enable server GC
  • Throw fever exceptions. Avoid using exceptions to control the program flow. Never catch exceptions that you can not handle. Use Performance Monitor to check exception count and other relevant information
  • Always implement Finally block
  • Prefer value types i.e. If structure can work then why to take class
  • Prefer AddRange() over Add() for adding multiple items to collection
  • Trim your working set. Use and load minimal and only required number of assemblies. Prefer single huge assembly rather than using multiple small assemblies
  • Prefer thread pool rather than creating a new thread for each request
  • Use For loop for string iterations rather than ForEach iterator
  • Use StringBuilder for string manipulation
  • Prefer early binding
  • Be careful while choosing .NET collections as each collection is designed for specific purpose
  • Use StringCollection class to store strings
  • Use Hashtable when frequent query is required on large number of records
  • Prefer ListDictionary as it is faster than HashTable for <10 records
  • For small data go for SortedList. For large data, go for ArrayList and then call Sort method on it.
  • Prefer arrays over collections unless you need some special functionality as they use contiguous memory arrangement and are faster
  • Avoid calling GC.Collect method because it traverse all the generations. If you have to call GC.Collect in your particular niche case, then make sure to clean finalized object also using GC.WaitForPendingFinalizers() and again call GC.Collect. This will collect all the dead objects.
  • Avoid implementing Finalize as it requires 2 GC cycle. Implement it only and only you hold unmanaged resources
  • Call GC.SuppressFinalize method inside Dispose method
  • Be cautious while using Thread.Suspend, as it may lead to deadlock due to incorrect synchronization.
  • Lock(object) is the cheapest method to perform synchronization
  • Avoid locking ‘this’ as it will lock entire object, even few of its member doesn’t require synchronization 
  • Prefer Using statement to ensure Dispose is called 
  • A very good diagram is given on MSDN which talks about few more concepts around this area: 





















Hope you enjoyed reading this article. Please drop your valuable comments, so that I can improvise this list further. Happy learning !!!


Tuesday, August 23, 2016

Associating any file with an installed application

Recently I got a requirement to associate a file having custom extension with the installed version of our own application. So, whenever user clicks on that given extension file, it should open up directly in my WPF application along with the data population. So, let’s check out how you can achieve this:

Step 1 - Creating an application: Create a WPF application or you can take any of your existing application. I’m working on WPF application (using MVVM) but the concept remains same for others, you can go with your Windows Forms or Console application also. That’s pretty acceptable. In my application, I'm taking few customer properties and a file path.

Step 2 - Get key for Signing: For signing my assembly I’m using inbuilt feature of Visual Studio. Go to your project properties, navigate to Signing option as shown below:

                                            








If you already have a key then you can browse and associate it else you can always go ahead and create a new key. Please note: You can use any other way to sign your assembly. In order to make this post simple, I’m using the easiest way J
Once the key is created, it will be automatically added to your project with name as <xxx>.pfx

Step 3 - Signing the manifest: Now as your key is ready, next step is to associate it with your deployment. This can be done by updating the ClickOnce manifests section under Signing tab as shown below:











On the above screen check the given checkbox and browse your pfx file. Note: Please verify expiry date of the certificate once it is browsed successfully.

Step 4 - Associating a File: Next step is to associate our custom file extension, for me it is .shw (this .shw file is in JSON format). Similarly you can take any extension that you would like to associate with your WPF application.  Let’s quickly go to Publish option and furnish all the required details as shown in below image:

















Add default icon for your application using Application Files dialog. Once the icon is associated with your application, you will be able to see that in Application Files dialog. By any chance, if you can’t find your icon in list, please follow my previous post for troubleshooting this J

Step 5 - Tweak your application: Now most important step is to update our WPF application, so that it can understand this new extension (.shw).

Update application startup with following code: 
protected override void OnStartup(StartupEventArgs e)
   {      
      if (AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData != null
               && AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData.Length > 0)
            {                

               string fileName = "";
               try
                {  
                   fileName = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData[0];
                   Uri uri = new Uri(fileName);
                   fileName = uri.LocalPath;

                   AppShared.SelectedFilepAth = fileName;

                }                

               catch (Exception ex)
                {                   

	  // do exception handling
                }
      }              
       base.OnStartup(e);
    }

Update your ViewModel so that your view can read data as:

public MyViewModelConstructor()
        {                     

             if (!string.IsNullOrEmpty(AppShared.SelectedFilepAth))
             {

                Customer = Newtonsoft.Json.JsonConvert.DeserializeObject<Customer>(System.IO.File.ReadAllText(AppShared.SelectedFilepAth));
                FilePath = AppShared.SelectedFilepAth;
             }            

            else
            {                

                Customer = new Customer() { Name = "Test User", Age = 100 };
            }
        }
 
Step 6 - Final Step: Build your code. Publish it. Install it and wow you are done. After performing all these steps, try double clicking your file, it will automatically pop-up your application with pre-loaded data.
 
It's straight forward. Isn't it?

Ok, let's do something more. Till now we were doing our file association via nice and user friendly GUI. But there is an alternate way available to achieve the same for our command-line lovers in which they have to play with application manifest file directly. Below are the steps to perform the same operation using command line:
  • Open you application manifest. In case, if it is not available please add a new app manifest file to your project and open it into the editor of your choice.
  • Add a new fileAssociation element as a child of Assembly element.
  • Add 4 attributes to this newly added fileAssocitation element – extension, description, progid and defaultIcon. Sample manifest file is given here, In case if you want to associate more file types, then you have to add another fileAssocitation element.
  • Sign application manifest using Mage.exe utility:
mage -Sign <YourApp>.exe.manifest -CertFile <yourCertificate>.pfx

Entire list of Mage command parameters is given over here.
Hope you enjoyed associating your file with multiple ways. Create a file with funny extension, open it and have fun. Happy learning!

Tuesday, August 16, 2016

Error: Icon file is not set to be published...

Today while working on one of the application that was for ClickOnce deployment I faced a small issue which took almost my half an hour. My task was to associate a default icon to my application. So, in order to do this, I went to my project properties and simply set the icon as shown below:
















As you can see in above screenshot, it is showing an error icon which states: 'Icon file is not set to be published with the application, or is not part of the required download group'

Then I build my application and land up with below error message which was more clear:






After hitting my head for many minutes, I thought, let's check out what is this 'download group'.

On surfing net, I got the clue that download group is nothing but a collection of files which are going to be part of our publish activity.
So, I quickly opened Application Files dialog using Project properties >> Publish. The Application Files dialog looks like:















I noticed that my icon file is not listed above. Then I got an idea on what went wrong :)
I immediately changed the to build action to 'Content' and force it to 'Copy Always'. And guess what?
My error is gone :)

After fixing this issue, I realized that it was very silly. As it wasted my many minutes, I thought to add it to my repository, so that it can be useful for all others who got stuck like me :(

Happy troubleshooting !!! 

Sunday, August 14, 2016

Back to blogging

Hey friends, I’m back to my blogging world after a gap of few months. Yes, you are right. I’m alive. Actually I was busy with motherhood J

Last night, I was scared to look at my blog, fearing all the followers have given up on me. Trust me, this is very-very scary feeling. Although it’s been not so long since I become a blogger but a fear of losing followers, who actually take time to read my posts is something which I can’t express. Hope you are reading this.

After a long break, it is bit difficult to get the same momentum. But I’ll try to make it up. So, stay tuned and keep reading.

Happy learning!!!

Tuesday, February 16, 2016

Reading version information from project.json

As of now, many of you are aware about the information stored in project.json file in ASP.NET Core 1.0. My this blog post will tell, how to retrieve the version number from project.json file.


Below is the screenshot of my json file:




















And below is the code to get the version number:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
            // Reading project.json file
            var projConfig = new ConfigurationBuilder().AddJsonFile("project.json").Build();

            //app version number
            var appVersion = projConfig["version"];

            // SQL Server version number
            var sqlServerVersion = projConfig["dependencies:EntityFramework.MicrosoftSqlServer"]; 
}
Hope this post was helpful.
Mothertouch Round Walker (Blue)

Thursday, February 11, 2016

Application State in ASP.NET Core 1.0

Mee Mee Steam Engine
Introduction to Application State
Application state provides a way to store in-memory data, which are smaller in size. It includes both global and user-specific data. Such data can be used across application and can be used by all users. Prior to ASP.NET Core 1.0 also there were Application and Session state options available to store such sort of data.

Ways of managing Application State?
Now question is, which state storage provider is to be used and when? It is influenced by variety of factors:
  • Size of data
  • Format of data
  • Duration to persist data
  • Sensitivity of data, etc.
Based on your answers, Application State can be managed in variety of ways like:
  • HTTPContext
  • Cookies
  • Session
  • Querystring and Post
  • Cache
  • Other options (EF, Azure Table Storage, etc.)
As part of ASP.NET Core 1.0 release, there is change in HTTPContext object. Hence I’ll emphasis on that.

HTTPContext:
Items collection of HTTPContext is used to store data which is required only for that particular request. It means contents are discarded and renewed after every HTTP request. HTTPContext.Items is a simple dictionary collection of type IDictionary<object, object>. HTTPContext.Items is very useful in sharing data between various middleware components. In another words, one middleware component can add data to HTTPContext.Items collection and other middleware component in the same HTTP request pipeline can read it. Ways to get an instance of HTTPContext can be found here.
Why HTTPContext re-introduced?
Main reasons for re-introducing HTTPContext are:
  • ASP.NET Core 1.0 no more uses System.Web assembly. It was done in order to reduce the application footprint by introducing new libraries based on functionality.
  • Huge size of object graph for HttpContext. Earlier this size was approximate 30K, which has now come down to approximate 2K.
Application State Considerations
  • Data is stored in-memory. Hence it is fast as compared to database stored on the server.
  • Application State stores data as Object type, so value has to be converted to appropriate type while reading.
  • Application State data can be access simultaneously by many threads. So, data updates should be done in thread-safe manner.
  • Application State cannot be preserved in Web Farm and Garden scenarios.

Wednesday, February 10, 2016

Consuming Services in ASP.NET Core MVC View

Exclusive Amazon Deals
In continuation to my previous post on injecting services in controller, this time I'm writing on how to inject services directly in MVC View. In order to achieve this, a new keyword @inject is used.
Here I'm not writing entire code again as it can be referred from my previous article.

Let's register the service in ConfigureServices method as:

public void ConfigureServices(IServiceCollection services)
        {
           ...
           ...
           services.AddTransient<IGUIDService,GUIDService>();
        }

Next is to inject service inside a View as:
@inject IGUIDService guidService

Now service is injected and available for use. Let's quickly use it:

@using CustomTagHelper.Services;
@inject IGUIDService guidService

<p>@guidService.GenerateGUID()</p>

Run your application and you will be able to see the required output as:









Hope you enjoyed learning :)

Monday, February 8, 2016

Consuming Services in ASP.NET Core MVC Controller


Exclusive Amazon Deals
Another interesting feature of ASP.NET Core is service consumption using Dependency Injection. I already provided the overview of Dependency Injection in my earlier articles. Moving further, in this article we will see how one can inject and consume services to controller using dependency injection.

In ASP.NET, you can access services at any stage of your HTTP pipeline through dependency injection. By default, it provides Constructor injection, but it can easily be replaced by any other container of your choice. Before moving ahead, one point is very important to understand and that is lifetime of services.

ASP.NET Core allows you to configure four different lifetimes for your services.
  • Transient - Created on every request
  • Scoped - Created once per request
  • Singleton - Created first time when they are requested
  • Instance - Created in ConfigureServices method and behaves like a Singleton

To understand the service consumption in easier way, we will create a sample application going step-by-step. My example service will provide GUID to respective controller. So, let's start quickly. 
Create a new project - Create a new project using ASP.NET Web Application template as shown below:
















Add Service Interface - Create an interface named IGUIDService under Services folder as shown below:

public interface IGUIDService
    {
        string GenerateGUID();
    }

Add Service Implementation - Create a class named GUIDServiceunder Services folder and provide the implementation of IGUIDService interface as shown below:

public class GUIDService : IGUIDService
    {
        public string GenerateGUID()
        {
            return ("Generated GUID is: " + Guid.NewGuid()).ToString();
        }
    }


Add a Controller - Next task is to add a Controller namedGUIDController by right clicking on Controllers folder as shown below:














Add a View - Before adding a View, create a folder named under Views folder. Now add a View by right clicking on GUID folder as shown:

















Update the code inside View as shown below: 

@ViewData["GeneratedGUID"]

Use Service in Controller - Now instantiate the service and set the data for View as shown below:

public class GUIDController : Controller
    {
        private IGUIDService _guidService;

        public GUIDController(IGUIDService guidService)
        {
            _guidService = guidService;
        }
        public IActionResult Index()
        {
            ViewData["GeneratedGUID"] = _guidService.GenerateGUID();
            return View();
        }
    }

Register Service - Last step is to register the service in the ConfigureServices method of Startup class as shown below:


public void ConfigureServices(IServiceCollection services)
        {
           ...
           services.AddMvc();
           services.AddInstance<IGUIDService>(new GUIDService());
        }


Everything is set. Now run your application and you would be able to see GUID on browser as: