Sunday, January 31, 2016

Continuous Integration for .net Core projects ( for beginners )

Hello developers,

This article is about how you can easily setup continuous integration for your dot net core (dnx) projects with appveyor.com

Let's begin,

Step 1: Create an account and connect to repository

Create an appveyor account. It's free for open source projects.

Enable CI for for your desired repository as specified below :

Click on new Project


Select your source control
Add the repository

Step 2: Setup build Environment

Go to Settings of this project

Click on Environment



Select the Operating System as Visual Studio 2015 RC
Specifying Clone directory is optional.
In "Cached directories and files", we can put the location where packages and runtimes are stored so that it doesn't have to download them every time build runs. This reduces the build time.
Note that it is : C:\Users\appveyor\.dnx

Now click on Build tab

You now have 3 options,

  1. MSBuild
    • Build using msbuild. Easiest of all. You don't have to write any script. Appveyor will automatically detect the solution file and launch MSBuild to build your entire solution. That's it. That did work for me for conventional project but for coreclr project, I faced some issues.
  2. Script
    • I prefer using scripts because it gives me more flexibility of how I want to build my projects. Also, if I have the script, I can take these scripts and run them on a linux machine too because linux won't have MSbuild.
  3. Off
    • Well, we are using CI to build your application. No reason to to turn this Off!


Ok, Let's go through the build script now
Note that I have selected CMD from the type of scripts and not PS.

The first line of my build script adds a nuget package source. This is the nuget package source where all latest (or beta) packages are found for asp.net 5. This step was required for me because I needed one of those packages but you can safely omit this line if you don't need those packages.

Next I am upgrading the coreclr runtime and this command also tells the OS to use this as default runtime to run application. But we don't run the application on CI server, we just build it so, we don't need that line, right ? NO. Because we will be RUNNING some tests on CI server.

Next - I am now in the directory where appveyor has cloned my repository (c:\projects\<projectName>), so I know that there is an src folder and it contains some projects. I am going to build all projects one by one. dnu restore restores the nuget packages into C:\Users\appveyor\.dnx\packages. If you remember, we have cached this directory in Environments tab to avoid downloading packages every time it builds. Then dnu build compiles the project.

Click on Save.

Step 3: Setup Tests

Click on Tests tab.



Here too you have the option of "Auto" or "Script". I will again go with scripts. ( If you go with Auto, let me know your experience in comments).
I have a project called Tests. The first line changes the directory to go into that project folder.
Next, I restore the nuget packages required for this project.
Next command is "dnx test"

Wait, what ? how does dnx know how to run my tests ?!
Here's how:


I have a command specified in my project.json that simply calls xunit runner. dnx is able to run the commands specified in project.json. You can have multiple commands if you like, .. may be UnitTest, IntegrationTest, etc.

Step 4: Setup Build Notifications

Alright, now lets setup some email notifications to know if the build fails. Click on Notifications tab.



This diagram is actually pretty self explanatory. Just select "Email" as notification type, provide your email address(s), select the checkboxes as shown in diagram and save the settings.

Now whenever a check in is made in your repo, appveyor will automatically trigger a build and run all your unit tests. If it fails, you will get an email alert.

There you go. You have successfully set up continuous integration for your .net core project.

Feel free to explore all the options given to you in settings console. And please provide feedback in comments.

My next article will be about automated deployments. - aka Continuous Delivery.

Thank you.

Saturday, September 28, 2013

Adding Log Feature to Entity Framework

Today I will show how you can add auditing / logging to entity framework. [Code First]

Please note that this is an old blog post. An update to date instruction page is now at https://github.com/bilal-fazlani/tracker-enabled-dbcontext/wiki & repository is shared on github



As developers who deal with database everyday, we all are aware of those 4 columns that are present at the end of all tables,  i.e. create date,update date,create user, update user, etc. Depending on implementation these columns may differ in different scenario. But the objective here is same, auditing/logging.

The same can be achieved with entity framework by adding 4 extra properties to all POCO classes. This can be made more efficient by inheriting from and a class who already has these properties. To be honest, I have never followed that approach with entity framework.


I follow a different approach where you have a separate table which is dedicated for log entries.

-------------------------------------------------------------------------
Update: 5 March 2015
  1. For ASP.NET MVC 5 and above use: TrackerEnabledDbContext.Identity
  2. For everything else use: TrackerEnabledDbContext
Update: 26 March 2015
If you find any bugs, please raise them on : github.com
-------------------------------------------------------------------------

How do we do it ? - In 4 easy steps

Step 1: Install the nuget package

Install the package I have created from nuget package. 
you can easily do that by typing "Install-Package TrackerEnabledDbContext" in the package manager console.

Package Manager Console

Step 2: Inherit

Inherit your DbContext from TrackerContext (or TrackerIdentityContext for MVC 5 or above). It's present in TrackerEnabledDbContext namespace.
And instantiate it with connection string for example :

        public DatabaseContext()
            : base("DefaultConnection")
        {
        }

Add a migration and update your database. You will notice that 2 new tables are created for recording changes. (dbo.AuditLog)

      

Step 3: Decide

Decide which tables you want to track. and apply [TrackChangesattribute to those POCO classes.
Once you do that all the columns in that table will be tracked. In case you want to skip tracking for some specific columns, you can apply [SkipTrackingattribute to those columns (properties).

here is an example :

    

Step 4: Start using

Whenever you make a change in database, you call DbContext.SaveChanges(). Now you have an overload available for that which takes an integer. This should be the logged in persons user id. Please note that if you don't pass the user id, this change will not be recorded into the tracking table.

Example :
    databaseContext.SaveChanges(userId);


Now with version 2.5, there new methods available to fetch logs from audit log table.

       var ProductLog = context.GetLogs<Product>(426);

Please leave your comments and feedbacks. Thank you.





Saturday, March 16, 2013

Time Out Exception While Executing an Sql Command

Problem :

System.Exception: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding

Cause : 

sqlcommand has a property called commandTimeout. Its default value is 30 seconds. If the query execution time is more than this value, It will throw above exception.

Friday, December 14, 2012

How to add an icon to your custom control

How to add an icon to your custom control

There's a lot of confusion for this on the internet. However, this is a very simple process.
This is how you do it :

  1. Have an image ready
  2. Make sure the image name is same as the control class.
  3. Resize it to 16 x 16 px (can be done with paint)
  4. While saving, select type as "16 Color bitmap" (paint is really cool !)
  5. Add it to your project (root directory)
  6. Go to its properties
  7. Select the build action as embedded resource.
  8. Rebuild the control
  9. Done!


Tuesday, October 16, 2012

Accessing deleted row field values from data table with C#.net


Accessing deleted row field values from data table with C#.net

Solution #1

Sometimes we delete data row in data table of dataset. Then we may need again to get deleted row field information from the dataset back for some calculation purpose. It gives error if we want to access the row data from the data table telling "The data row has been deleted ...".

We can still access the field data information by using DataRowVersion.Original parameter. For example 

int customerId = Convert.ToInt32( DataTable1.Rows[0][0, DataRowVersion.Original];

This will give the original version of the row data. And voila - we have our original data back.

Solution #2


We can make a Data View out of the datatable. Then convert that data view into data table again. This will give us the original data. But this time the datarowstate = "added".

We see one example code for this.

DataView myView = new DataView(sourceTable, null, null,DataViewRowState.Deleted);DataTable myTable = myView.ToTable();

Continuous Integration for .net Core projects ( for beginners )

Hello developers, This article is about how you can easily setup continuous integration for your dot net core (dnx) projects with appveyo...