Thursday, December 31, 2015

Integrate your GIT repository with Visual Studio 2015

 

You can now easily use visual studio with your solution in GIT. You can Commi, pull, push ans run all source control commands with in your favourite IDE visual studio. You dont need to use any third party tools for that. Integration in so much easy just new steps.

1. Visual Studio-> Team-> Manage Connections

2. Then you can see team explorer

image

3. Under Local GIT repositories Click on clone

image

in there on first text box enter your GIT repository URL and the the second one add the local path and then click clone then It will promted to enter your credentails/

It will download the source code from GIT with source control binding. After that you can see all source control commands related GIT available in your visual studio.

I .Commit your changes locally

after you done with the changes when you right click on a file , project or solution you can find the Commit option as follows. This will commit your changes to local repository.

image

II .Sync your code with server code

Before pushing your code to online GIt repositor you need sync your local repository with server. For that you can use the option sync then pull.

image

III .push your changes to server

Then you can push your changes to server using push option

If you are someone familiar with TFS this is very smiliar to use.Even command names are bit different it does almst the same function

Get rid of usafe tag on angular url

 

Issue :

I recently wanted to use html sms tag in my mobile web site. In the site I have used angual binding to dynamically bind the sms url as follows

<a id="lnk_sms" href="{{sms_Link}}"  class="sms-button social-media-button" alt="SMS" style="display:none">SMS</a>
 
href value is something like :- 'sms:?body=bodyText’

when I run the application agular keep adding a unsafe prefix infront of the url and sms functionality wasn’t working.

<a id="lnk_sms" href="'unsafe:sms:?body=bodyText’"  class="sms-button social-media-button" alt="SMS" style="display:none">SMS</a>

After spending hours I was able to find the problem and soltion

Problem :

Only http, https, ftp and mailto are enabled by default. Angular will prefix a non-whitelisted URL with unsafe:

Solution :

You need to explicitly add URL protocols to Angular's whitelist using a regular expression. A good place to whitelist the sms: protocol would be in your module's config block:

var app = angular.module( 'myApp', [] )
.config( [
    '$compileProvider',
    function( $compileProvider )
    {   
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|chrome-extension):/);
        // Angular before v1.2 uses $compileProvider.urlSanitizationWhitelist(...)
    }
]);

The same procedure also applies when you need to use protocols such as file: and tel:.

Please see the AngularJS $compileProvider API documentation for more info.

Hope this Helps!!!

Monday, December 28, 2015

Share you link in Social Media

Technorati Tags: ,

If you want to a share a link in you web app there are plenty of options available.

Recently I got a requirement to share a link , which is browsed through mobile. We have provided the following options to the users.

1. SMS

2. Twitter

3. LinkedIn

4. FaceBook

5. Viber

Following points gives you how to add the functionality to your site easily.

 

1. SMS

For this you have to just put the following HTML tag in to you application which is accessed via mobile. This will open the sms app in user’s mobile pre populated with the content which you have stated.

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

If you want it to work on iOS, you need this:

<a href="sms:/* phone number here */;body=/* body text here */">Link</a>

Apparently iOS8 had to go and change things on us, so thanks to some of the other commenters/responders, there's a new style for iOS:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

(phone number is optional)

You can see a live demo here http://bradorego.com/test/sms.html (note the "Phone and ?body" and "Phone and ;body" should autofill both the to: field and the body text. View the source for more info)

 

2. Twitter

Twitter is really easy. All what you have to do is go to following link and twitter will generate the code to be pasted in you app.

https://about.twitter.com/resources/buttons#tweet

image

You will get something like this finally. Here I am sharing the current page URL.

<a href="https://twitter.com/share" class="twitter-share-button"{count}>Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

Rather sharing a link this page gives you generating code for buttons for follow,hashtag and mention.

 

3. LinkedIn

LinkedIn also provides you a button generation in the following link.

https://developer.linkedin.com/plugins/share

image

Here URL is optional. If you keep it blank and generate the button it will be default to current page, In case if you want to share a different URL then you can set it in the URL text box and generate the button. They have different options of buttons with number of shares as well.

finally you will get a code as follows.

<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>
<script type="IN/Share" data-counter="right"></script>

 

4. FaceBook

Following link provided you step by step on how to add a share button for facebook

https://developers.facebook.com/docs/plugins/share-button#configurator

image

same as the above once this also have a button generator where you just have to copy and paste it.

5. Viber

Viber provided the following link for you to integrate your web app with there viber app.

https://www.viber.com/en/developers/share_on_viber

Just going through two simple steps you can add share via viber options in you web app.

As you can see adding these functionalities are really easy and takes less time. But these functionality will increase traffic in you web app.

Hope this is helpful

Thursday, September 10, 2015

Explicitly freeing memory in c#

Recently I wrote a program which loads some CSV files in to DataTables and do some processing. These files are in large size and have nearly 1 million rows. When I load first file in to data table it works fine. But then I load the second file I get a “Out of Memory exception” even though first I make the previously loaded datatable to null. After searching for hours I found the following solution which solved my issue.

  1. var sourceDataTable = DataParserHelper.GetDataTableFromCsv(@"F:\FileSplitter\Data\5263\property_finance.csv");
  2.         
  3.          sourceDataTable.Clear();
  4.          sourceDataTable.Dispose();
  5.          sourceDataTable = null;            
  6.          GC.Collect();
  7.          GC.WaitForFullGCComplete();
  8.          GC.WaitForPendingFinalizers();

 

if you make the datatable null what happens is it frees the Datatable from the rows and columns. But actually those rows and columns retained in the memory and those are not ready for the garbage collector. But when you call Clear() all the memory will be cleared.

 

Hope this helps.

 

Happy Coding !!!

Monday, September 7, 2015

Sending Emails using SendGrid in C#.net

SendGrid has been built from the ground up as an API company. It is built to serve developers and make it easy to send email no matter your environment. You can send email over SMTP or HTTP, and even use one of it’s official client libraries. In just a few minutes, you can send your first email and millions more. It has different pricing plans for you , so you can select as you want. There is a free plan to which you can send up to 12,000 emails per month for free.

For more details about pricing visit here

1. Create an account in SendGrid

2. Create a Console Application and add the following nuget package.

SendGrid 5.1.0

3. Create a email template in SendGrid.

4. In the following example I’m loading some user information saved in a CSV file and then send password information for those users. Following is the template I have created for that in SendGrid. In the template I have used “{{}}” to specify the tags. You can use what ever the tab you want but make sure you use the same tag when you send the email request to sendgrid

Email Template
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. </head>
  6. <body>
  7. <p>&lt;%body%&gt;</p>
  8.  
  9. <p>Dear {{FirstName}}</p>
  10.  
  11. <p>You have been granted access to  raise online Facilities Service Requests.</p>
  12.  
  13. <p>Your user id is : {{UserName}}</p>
  14.  
  15. <p>Your password is:&nbsp; {{Password}}</p>
  16.  
  17. <p>Thanks</p>
  18.  
  19. </body>
  20. </html>
  21. <title></title>

5. Create a User Model class

  1. public class User
  2.     {
  3.         public string UserName { get; set; }
  4.         public string Password { get; set; }
  5.         public string FirstName { get; set; }
  6.         public string LastName { get; set; }
  7.         public string Email { get; set; }
  8.     }

6. Load CSV file to DataTable

  1. public static System.Data.DataTable LoadContent(string filePath)
  2.       {
  3.           StreamReader sr = new StreamReader(filePath);
  4.           string[] headers = sr.ReadLine().Split(',');
  5.           DataTable dt = new DataTable();
  6.           foreach (string header in headers)
  7.           {
  8.               dt.Columns.Add(header);
  9.           }
  10.           while (!sr.EndOfStream)
  11.           {
  12.               string[] rows = sr.ReadLine().Split(',');
  13.               DataRow dr = dt.NewRow();
  14.               for (int i = 0; i < headers.Length; i++)
  15.               {
  16.                   dr[i] = rows[i];
  17.               }
  18.               dt.Rows.Add(dr);
  19.           }
  20.           return dt;
  21.       }

7. Load User List using the DataTable

  1.  public static List<User> GetUserList(DataTable content)
  2.       {
  3.           List<User> users = new List<User>();
  4.  
  5.             foreach (DataRowrow in content.Rows)
  6.           {
  7.               User user = new User();
  8.               user.FirstName = row["FirstName"].ToString();
  9.               user.LastName = row["LastName"].ToString();
  10.               user.UserName = row["UserName"].ToString();
  11.               user.Password = row["Password"].ToString();
  12.               user.Email = row["Email"].ToString();
  13.               users.Add(user);
  14.           }
  15.  
  16.           return users;
  17.       }

8. Send Email to users using SendGrid

  1. public static void SendEmail(string filePath)
  2.       {
  3.           DataTable content = LoadContent(filePath);
  4.  
  5.           List<User> users = GetUserList(content);
  6.  
  7.           foreach (User user in users)
  8.           {
  9.               string xmstpapiJson = GetMessageHeader(user);
  10.  
  11.               SendGridMessage sendGridMessage = new SendGridMessage();
  12.               sendGridMessage.From = new MailAddress(fromAddress, fromName);
  13.               sendGridMessage.AddTo(user.Email);
  14.               sendGridMessage.Subject = "New Registration";
  15.               sendGridMessage.Headers.Add("X-SMTPAPI", xmstpapiJson);
  16.               sendGridMessage.Html = string.Format(" ");
  17.  
  18.  
  19.               var userName = sendGridUserName;
  20.               var password = SendGridPassword;
  21.               var credentials = new NetworkCredential(userName, password);
  22.               var transportWeb = new Web(credentials);
  23.  
  24.               transportWeb.Deliver(sendGridMessage);
  25.               Console.WriteLine("Email Sent to:"+user.Email);
  26.            
  27.               
  28.           }
  29.  
  30.           Console.WriteLine(string.Format("{0} Emails Sent", users.Count()));
  31.           Console.ReadLine();
  32.           
  33.       }
  34.  
  35.       private static string GetMessageHeader(User user)
  36.       {
  37.           var header = new Header();
  38.  
  39.           header.AddSubstitution("{{FirstName}}", new List<string> { user.FirstName });
  40.           header.AddSubstitution("{{LastName}}", new List<string> { user.LastName });
  41.           header.AddSubstitution("{{UserName}}", new List<string> { user.UserName });
  42.           header.AddSubstitution("{{Password}}", new List<string> { user.Password });
  43.           header.AddFilterSetting("templates", new List<string> { "enabled" }, "1");
  44.           header.AddFilterSetting("templates", new List<string> { "template_id" }, "67d23037-08bc-445a-bda1-903f6af65b67");
  45.           return header.JsonString();
  46.       }

 

Hope This Helps.

 

Happy Coding !!!

Point in Time Restore in Azure SQL Database

With new Basic, Standard and Premium Service Tiers , you get the new improved facility of “Point in Time Restore”. Which is, it provides you zero-cost automatic backup system. You only incur additional cost if you use the restore capability. The new database created by restore is charged at normal database rates. Together, the automated backup system and point-in-time restore provide a zero-cost, zero-admin way to protect databases from accidental corruption or deletion, whatever the cause.

Period that backups retain will based on the service tier.

Basic Tier :- 7 Days

Standard Tier :- 14 Days

Premium Tier :- 35 Days

 

Restoring a Live Database to a Point in Time

Using Azure management portal you can easily. Go to the Database which you want to restore. Then Click on the “Restore” Button in the bottom bar.

image

You will be prompted for a new database name and offered a slider control to pick the restore point within the retention period. Alternatively, you can manually enter the date and time to the nearest minute. Once you confirm the action, the database will be restored.

image

 

After you confirmed your new database will be create in with the given name in the same database sever.The time taken to restore the database depends on many factors, including the size of the database, the time point selected, and the amount of activity that needs to be replayed to reconstruct the state at the selected point. For a very large and/or active database restore may take several hours.

 

Restoring a Deleted Database

If you want to restore a deleted database which is using Basic, Standard or Premium service tier , You are given the facility of restoring it to the point which it has been deleted. To restore go to SQL databases tab in Management portal and go to deleted databases. There you can see a list of databases which has been deleted.

image

Select the database you want to restore then click on the restore button at the bottom. Then you will get the following window which you can type a name for the database and restore the backup for a new database

image

 

Hope this helps.

Calling Async Call from Main()

This is a small code which shows different ways of calling an async method inside Main

Following is my sample method.

  1. public async Task<int>SampleMethod()
  2.     {
  3.         await Task.Delay(5000);
  4.         int answer = 21 * 2;
  5.         return answer;
  6.     }

 

1. Using  GetAwaiter()

 

  1. static void Main(string[] args)
  2.     {
  3.         SampleMethod().GetAwaiter().OnCompleted(() =>
  4. {
  5.     Console.WriteLine("finished");
  6. });
  7.         Console.ReadKey();
  8.     }

 

2. Using  Wait()

  1. static void Main(string[] args)
  2.     {
  3.         SampleMethod().Wait();
  4.     }

 

Hope this helps,

Tuesday, August 18, 2015

Object graph for type contains cycles and cannot be serialized

This issue occurs when parent and child classes has cyclic reference and when you tries to serialize it to json or xml. See the following code which gives the error.

  1.  
  2. [DataContract]
  3. public class Family
  4. {
  5.     [DataMember]
  6.     public IList<Parent> Parents;
  7.     [DataMember]
  8.     public IList<Child> Children;
  9. }
  10.  
  11. [DataContract]
  12. public class Parent
  13. {
  14.     [DataMember]
  15.     public string Name { get; set; }
  16.     [DataMember]
  17.     public IList<Child> Children { get; set; }
  18. }
  19.  
  20. [DataContract]
  21. public class Child
  22. {
  23.     [DataMember]
  24.     public string Name { get; set; }
  25.     [DataMember]
  26.     public Parent Father { get; set; }
  27.     [DataMember]
  28.     public Parent Mother { get; set; }
  29. }

This is the sample object

  1. var dad = new Parent { Name = "John" };
  2.             var mum = new Parent { Name = "Mary" };
  3.  
  4.             var kid1 = new Child { Name = "Ann", Mother = mum, Father = dad };
  5.             var kid2 = new Child { Name = "Barry", Mother = mum, Father = dad };
  6.             var kid3 = new Child { Name = "Charlie", Mother = mum, Father = dad };
  7.  
  8.            
  9.             var listOfKids = new List<Child> { kid1, kid2, kid3 };
  10.             dad.Children = listOfKids;
  11.             mum.Children = listOfKids;
  12.  
  13.             var family = new Family { Parents = new List<Parent> { mum, dad }, Children = listOfKids };

When I tried to serialize this object using data contract serializer I go the cyclic reference error. Following is the code I used to serialize this object in to json.

  1. DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Family));
  2.          MemoryStream ms = new MemoryStream();
  3.          ser.WriteObject(ms, family);
  4.          string jsonString = Encoding.UTF8.GetString(ms.ToArray());
  5.          ms.Close();
  6.          return jsonString;

 

Json.Net gives the solution for this. I my case I don’t want cyclic references to be serialized. So following is the solution I came up.

 

1. Reference json .net to your project. Use the following nuget

PM> Install-Package NewtonSoft.Json

2.  Then add follwong using statement to your class.

  1. using Newtonsoft.Json;

3. Then use the following code. It will ignore the cyclic references and serialize the object without any issue.

var jsonSerializer = new JsonSerializer
        {
            MissingMemberHandling = MissingMemberHandling.Ignore,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        };
       
        var sb = new StringBuilder();
        using (var sw = new StringWriter(sb))
        using (var jtw = new JsonTextWriter(sw))
            jsonSerializer.Serialize(jtw, family);

        var result = sb.ToString();

 

for more details on this error please go here

 

Hope this helps

 

Happy Coding !!!!!!

Friday, July 31, 2015

Updating Service Tiers in Azure SQL Database

Azure SQL Web and Business databases are being deprecated and retired September 2015 so it's time to start planning to upgrade existing Web or Business databases to the Basic, Standard, or Premium service tiers.

Upgrading a Web or Business database to a new service tier involves the following steps:

  1. Determine service tier based on feature capability
  2. Determine an acceptable performance level based on historical resource usage
  3. Why does existing performance for my Web or Business database map to the higher Premium levels?
  4. Tuning your workload to fit a lower performance level
  5. Upgrade to the new service tier/performance level
  6. Monitor the upgrade to the new service tier/performance level
  7. Monitor the database after the upgrade

In this post I will discuss about the point 2 : “Determine an acceptable performance level based on historical resource usage”

 

How to view the recommended service tier in the new Azure Management Portal

New Management Portal

  1. Log on to the new management portal and navigate to a server containing a Web or Business database.
  2. Go to browse all, Then SQL Servers
  3. Select the server you want to upgrade
  4. Click the Latest Update part in the server blade.

image

5. Click Upgrade this server.

The Upgrade this server blade now shows a list of Web or Business databases on the server along with the recommended service tier.

image

How to view DTU consumption in the Management Portal

Log on to the management portal and navigate to an existing Web or Business database.

  1. Click the MONITOR tab.
  2. Click ADD METRICS.
  3. Select DTU percentage and click the checkmark at bottom to confirm.

image

Hope this helps.

Monday, July 13, 2015

Clear Azure Cache : Get All keys in Azure Redis cache

 

Recently I got a requirement to clear cache value for a given client account. In our systems we have multiple contractors , and we cache some data related to that contractor account. Cache keys are as follows.

  1. string.Format("{0}:ServiceTypeList", clientConfiguration.Code);
  2.       string.Format("{0}:PropertyViewModelList", clientConfiguration.Code);
  3.       string.Format("{0}-{1}:FaultTypeList", clientConfiguration.Code,
  4.                     serviceType.ServiceTypeID);
  5.       string.Format("{0}-{1}:LocationList", clientConfiguration.Code,
  6.                     property.PropertyNo);

Here some cache keys are based on Client Configuration code, and some have client configuration and some code too. But when we have to clear the cache for a given client account we have only the Client Configuration code. So we used the following approach.

1. Get all Cache Keys

  1. private static ConnectionMultiplexer _connection;
  2.  
  3. public static IServer GetDataCacheServer()
  4. {
  5.     if (_connection == null || !_connection.IsConnected)
  6.     {
  7.         _connection = ConnectionMultiplexer.Connect(HybridConfig.GetAppSetting("RedisCacheConnectionString"));
  8.     }
  9.  
  10.  
  11.     var endpoints = _connection.GetEndPoints();
  12.     var server = _connection.GetServer(endpoints.First());
  13.     return server;
  14.  
  15. }
  16.  
  17.  
  18. public static IEnumerable<RedisKey> GetAllCacheKeys()
  19. {
  20.     var server = GetDataCacheServer();
  21.  
  22.     var keys = server.Keys();
  23.     return keys;
  24. }

2. Create Remove method

  1. public static void Remove(string key)
  2.         {
  3.            
  4.  
  5.             try
  6.             {
  7.                 key = string.Format("{0}_{1}_{2}", key, HybridConfig.GetAppSetting("ApplicationUrl"), HybridConfig.GetAppSetting("ApplicationVersion"));
  8.                 var database = GetDataCache();
  9.                 database.KeyDelete(key);
  10.              
  11.                 if (AllKeys.Contains(key))
  12.                 {
  13.                     AllKeys.Remove(key);
  14.                 }
  15.             }
  16.             catch (Exception ex)
  17.             {
  18.                 SqlLogger.Error(typeof(AzureCache), string.Format("Error while trying to remove the key : {0} | Message : {1}", key, ex.Message), ex);
  19.             }
  20.         }

3. Search the client configuration code in it and clear the cache


            var keys = AzureCache.GetAllCacheKeys();
            string applicationTag = string.Format("_{0}_{1}", HybridConfig.GetAppSetting("ApplicationUrl"),
                                                  HybridConfig.GetAppSetting("ApplicationVersion"));
            List<string> cacheKeys = (from redisKey in keys
                                      select redisKey.ToString()
                                      into key
                                      where key.Contains(applicationTag) && key.Contains(clientConfiguration.Code)
                                      select key.Replace(applicationTag, string.Empty)).ToList();
            if (cacheKeys.Any())
            cacheKeys.ForEach(AzureCache.Remove);

Happy Coding !!!