Pages

Thursday, April 10, 2014

Error Logging using Log4Net for azure and normal applications


Following are good links for error logging using Log4Net

For Normal Applications:
http://geekswithblogs.net/MarkPearl/archive/2012/01/30/log4net-basics-with-a-console-application-c.aspx

For Azure Applications:
http://www.kongsli.net/nblog/2011/08/15/log4net-writing-log-entries-to-azure-table-storage/

Wednesday, April 9, 2014

Azure Active Directory integration in asp.net MVC

Azure Active Directory integration in asp.net MVC

Pre- requisite:
1. Visual Studio 2012 Professional or Visual Studio 2012 Ultimate
2. Identity and Access Tools for Visual Studio 2012
3. Azure Subscription

Step 1:
 You need to create Active directory in your Azure subscription. You need to register your application in azure subscription.
 Active directory is called as tenant. Your users from your active directory(tenant) are able to login to your application.
 other users are not able to login. To make other users access available then you need to implement step 3 as well.
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151790.aspx

Step 2:
You can achieve following:
 1. Getting full user details
 2. Creating and Updating Users
 3. Getting a list of groups
 4. Updating group membership
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151791.aspx

Step 3:
 If you want to allow access to other user from your organization. then you need to implement multiple tenant(active directory) in your application.
 So other user will be able to login to your application.
Details explained in following link.
 http://msdn.microsoft.com/en-us/library/windowsazure/dn151789.aspx 

Monday, April 7, 2014

Azure: Building and Packaging Virtual Applications within Azure Projects

Check following links for proper deployment. Which deploys published code instead of complete code solution.

http://kellyhpdx.wordpress.com/2012/04/11/deploying-multiple-web-applications-to-a-single-azure-instance-and-applying-web-config-transforms-correctly/


http://blogs.msdn.com/b/davidhardin/archive/2013/01/18/building-and-packaging-virtual-applications-within-azure-projects.aspx

Thursday, January 23, 2014

Windows Azure: Upload and Download Functionality- upload/download image to blob

Following class is useful to download or upload image from blob. Provide necessary parameters to function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using Microsoft.WindowsAzure;
using System.IO;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace AzureDemo
{
    public class AzureBlobManager
    {
        public string uploadFilesToAzureBlob(FileUpload PostedFile, string FileName, string containerName, Guid userid)
        {
            string blobURLs = string.Empty;
            try
            {
                // Retrieve storage account from connection string.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Retrieve a reference to a container.              
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();

                if (PostedFile.PostedFile.ContentLength > 0)
                {
                    System.IO.Stream inputStream = PostedFile.PostedFile.InputStream;
                    inputStream.Position = 0;

                    byte[] myBinary = new byte[PostedFile.PostedFile.ContentLength];
                    //PostedFile.InputStream.Read(myBinary, 0, (int)PostedFile.ContentLength);
                    using (var binaryReader = new System.IO.BinaryReader(inputStream))
                    {
                        myBinary = binaryReader.ReadBytes(PostedFile.PostedFile.ContentLength);
                    }
                    // Retrieve reference to a blob named "myblob".
                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Concat(userid, "_", PostedFile.FileName));

                    // Create or overwrite the "myblob" blob with contents from a local file.                      
                    //blockBlob.UploadFromStream(PostedFile.PostedFile.InputStream);

                    blockBlob.UploadFromStream(new System.IO.MemoryStream(myBinary));
                    // blockBlob.UploadFromStream(PostedFile.PostedFile.InputStream);

                    blobURLs = blockBlob.Uri.ToString();
                }
            }
            catch (Exception ex)
            {
                //HandleException.HandleExceptionLog(ex);
            }
            return blobURLs;
        }


//Directly reads from blob and sends it to user
        public void DownloadFileFromBlob(string fileName,string containerName)
        {
            CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
            MemoryStream memStream = new MemoryStream();
            blob.DownloadToStream(memStream);

            HttpContext.Current.Response.ContentType = blob.Properties.ContentType;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName.ToString());
            HttpContext.Current.Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
            HttpContext.Current.Response.BinaryWrite(memStream.ToArray());
        }
    }
}

Friday, October 25, 2013

IE11 and Windows 8.1 : Solution for __doPostBack and Ajax not working

​Following Steps are performed to fix IE 11: ajax and __dopostback issues:
There are 2 solutions:
1. Site Level Fix:
   step 1: Perform Steps from following links
        http://www.nuget.org/packages/App_BrowsersUpdate/
  
   step 2: In ie.browser add mentioned settings from following link
           http://stackoverflow.com/questions/18009636/crosspostback-ajax-controls-and-asp-net-generated-postbacks-work-for-net-4-5

2. Machine Level Fix:
   Main Source: http://www.hanselman.com/blog/IE10AndIE11AndWindows81AndDoPostBack.aspx
   Step 1: Download patch from following link.
     http://support.microsoft.com/kb/2836939
   Step 2: install patch

Wednesday, October 2, 2013

SQL Server: Insert missing Identity Column Values

When we set identity to a particular column, we dont need to pass value for that column.
SQL Server automatically increments its value.

Suppose if we inserts 10 records and deleted last record. Maximum value will be 9 but when we add new record then SQL server takes next value as 11 not it doesn't take 10. Because we already added record in 10th position and deleted after it.
Example:
CREATE TABLE [dbo].[Student](
[Sno] [int] IDENTITY(1,1) NOT NULL,
[Sname] [nvarchar](50) NULL,
[Saddrs] [nvarchar](50) NULL
) ON [PRIMARY]
GO
insert into student(sname,saddrs) values('Abhi','India')
insert into student(sname,saddrs) values('Sourav','Japan')
insert into student(sname,saddrs) values('david','US')
insert into student(sname,saddrs) values('rahul','UK')
insert into student(sname,saddrs) values('gautam','Australia')
insert into student(sname,saddrs) values('jonathan','Germany')
insert into student(sname,saddrs) values('prasad','Pune')
insert into student(sname,saddrs) values('sandeep','Pune')
insert into student(sname,saddrs) values('john','Pune')
insert into student(sname,saddrs) values('sanjay','Pune')

Now we will delete sno=10
delete from student where sno=10
After Record deleted

Now If we add new record then it will take sno=11 but we want to add sno=10

In this case we need to insert value manually by turning off identity feature off. After inserted manually we need to identity turn on  again.
Syntax:
SET IDENTITY_INSERT TABLE-NAME  ON

Example:
SET IDENTITY_INSERT student  ON

Here
SET IDENTITY_INSERT TABLE-NAME  ON :turns off identity increment. Specify table name on which you want to turn off identity

Your Insert Statement, you need to provide identity column value because in previous command you have turned off autoincrement. Provide missing identity values which you want to add.
Example: Student is table in which sno is identity column.
I have 10 records, i have deleted last record i.e sno=10 now i want to add value for sno=10

My query will be
insert into student(Sno,sname,saddrs) values(10,'sachin','Pune')
After Above Query Result will be

Syntax:
SET IDENTITY_INSERT TABLE-NAME  OFF
Example:
SET IDENTITY_INSERT student  OFF

Here
SET IDENTITY_INSERT TABLE-NAME  OFF:turns on identity increment.
Now your autoincrement will be started, you dont need to provide value for identity column.

If you try to provide value then sql server will give you an error.
Example:
insert into student(Sno,sname,saddrs) values(11,'Rakesh','Pune')

Paging In SQL Server with derived table and CTE

First We will create table

create table RecordTable(col1 int, col2 int)

insert into RecordTable
select 1,1
union all
select 1,2
union all
select 1,3
union all
select 1,4
union all
select 1,5
union all
select 1,6
union all

select 1,7

We will do it through in 2 ways.
1. With Derived Table
2. With Common Table Expression.

Mostly performance is same in both ways, in some scenario it may vary.

1.Paging with Derived Table
select * from(
select col1,col2,
ROW_NUMBER() over( order by col1)  as rowno
from RecordTable)
ctedup
where rowno>minNo and rowno<MaxNo

2. Paging with CTE
;with ctedup as(
select col1,col2,
ROW_NUMBER() over( order by col1)  as rowno
from duplicateRecordTable)
select * from ctedup
where rowno>MinNo and rowno<MaxNo

It will gives you records in between min and max no.