Wednesday, 28 December 2011

Classic ASP on azure and start up tasks...

In Windows Azure a startup task is a Command-Line Executable file that gets referenced in the Cloud Service Definition file (.csdef). When a deployment is uploaded to Windows Azure a component of the Windows Azure Platform called the Fabric Controller reads the Definition file to allocate the necessary resources for your Deployment. Once the environment is set up the Fabric Controller initializes the Role Startup Process.



<Startup>
      <Task commandLine="StartupEnableASP.cmd" executionContext="elevated" taskType="simple">
      </Task>
</Startup>


Add the below commands to to a CMD file in a web role project and in the properties of the cmd file set copy always for the  "copy to output directory" attribute.


1. To enable ASP engine on a VM/hosted service.
    start /w pkgmgr /iu:IIS-ASP
2. To make APPpool run as a 32 bit application.

%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.enable32BitAppOnWin64:true /commit:apphost


3. To enable scripterrors send to browser in case of an ASP application.
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/asp -scriptErrorSentToBrowser:"True" /commit:apphost


4. To set scripttimeout for an ASP application.
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/asp /limits.scriptTimeOut:"00:30:00" /commit:apphost


5. To disable session state management of an ASP application.
%windir%\system32\inetsrv\appcmd set config /section:asp -session.allowSessionState:"False" /commit:apphost


6.  Change the time zone on a server.
%windir%\system32\tzutil.exe /s "Central Standard Time"


7. To register 32 bit dlls on a 64-bit operating system. The use /s is to suppress the message box after successful registration of the COM component. Otherwise the Azure role to start will be hanging in management portal. 
%SystemRoot%\Syswow64\Regsvr32.exe cdonts.dll /s
%SystemRoot%\Syswow64\Regsvr32.exe aspsmartupload.dll /s

8.  Installing .Net 4.0 dll to GAC before converting to a TLB file for using in an ASP app on Azure. Again note /s as suppression attribute. Now the TLB file can be used in ASP with server.CreateObject("")
gacutil.exe /i Utilities.dll
%SystemRoot%\microsoft.net\framework\v4.0.30319\regasm.exe Utilities.dll /tlb:Utilities.tlb /s

8.  Installing Microsoft.WindowsAzure.StorageClient.dll a .Net 4.0 dll to GAC before converting to a TLB file for using in an ASP app on Azure. Note the Microsoft.WindowsAzure.StorageClient.dll is not available on GAC of VMs in Azure so we did this to fix our problem when creating object of our custom dll from ASP on azure.
gacutil.exe /i Microsoft.WindowsAzure.StorageClient.dll
%SystemRoot%\microsoft.net\framework\v4.0.30319\regasm.exe Microsoft.WindowsAzure.StorageClient.dll /tlb:Microsoft.WindowsAzure.StorageClient.tlb /s


Note: The gacutil is not available on Windows Azure VMs on R2 server. You need to package the gacutil.exe and set its property attibute "copy to output directory" to copy always.



Migrate On-Premise SQL Server database to SQL Azure

1.     Fix local database so that it can be migrated to SQL Azure

             SQL Azure requires all the tables to have clustered index. Create clustered indexes & primary keys in                        local database by running below script whichever is appropriate.

CREATE CLUSTERED INDEX INDEX_NAME ON [dbo].[TABLE_NAME]([PRIMARY KEY]);
OR
ALTER TABLE dbo. TABLE_NAME ADD Id INT IDENTITY PRIMARY KEY CLUSTERED (Id);
OR
ALTER TABLE TABLE_NAME WITH NOCHECK
ADD CONSTRAINT PK_ TABLENAME _ PRIMARY KEY PRIMARY KEY CLUSTERED
(PRIMARY KEY);

2.     Connect to SQL azure database from SQL server 2008 R2 Client


a.       Server Name: ******.database.windows.net
b.      User Id: *****@******.database.windows.net
c.       Password: *******

3.     Create schema in SQL Azure.


        CREATE SCHEMA [SCHEMA_NAME] AUTHORIZATION [USER_NAME]
        GO

4.     Generate scripts from SQL server 2008 R2 client

          Use Generate scripts task in SQL server 2008 R2 to generate scripts separately for each objects.
                1.       While generating scripts for table objects set “script indexes” to True
                      2.       While generating scripts for table objects set “script Triggers” to True
                3.       Once script is generated for stored procedure objects  –
·         In all stored procedure Change any other schema names. To dbo. or  your own schema name.
             

5.     Executing Schema Objects scripts in the following order

                            ·         Table object Scripts
                            ·         View Object scripts
                            ·         User Defined Functions scripts
                            ·         Stored Procedures scripts

6.     Generate In/Out BCP commands into a batch file


        In this section we get the BCP commands in a batch file for getting data out of local database and              commands for sending data to SQL azure.
    SET NOCOUNT ON

    DECLARE @DestServer nvarchar(max)
    DECLARE @DestDatabase nvarchar(max)
    DECLARE @DestLogin nvarchar(max)
    DECLARE @DestPassword nvarchar(max)

    DECLARE @SrcServer nvarchar(max)
    DECLARE @SrcDatabase nvarchar(max)
    DECLARE @SrcLogin nvarchar(max)
    DECLARE @SrcPassword nvarchar(max)

    -- SQL Azure Server
    SET        @DestServer = '*******.database.windows.net'
    SET        @DestDatabase = 'Database name'
    SET        @DestLogin = 'username@*******.database.windows.net'
    SET        @DestPassword = '*******'

    -- SQL Server
    SET        @SrcServer = 'Local db server name'
    SET        @SrcDatabase = 'local DB name'
    SET        @SrcLogin = 'user  id'
    SET        @SrcPassword = '*******'
     
    PRINT 'echo %DATE% %TIME% > bcp.log'
    PRINT ''
     
    ---------------------------------------------------

    DECLARE @Schema nvarchar(max)
    DECLARE @Table nvarchar(max)

    DECLARE table_cursor CURSOR FOR
        SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.tables
    WHERE Table_Type = 'BASE TABLE'
    OPEN table_cursor;

    -- Perform the first fetch.
    FETCH NEXT FROM table_cursor INTO @Schema, @Table;

    -- Check @@FETCH_STATUS to see if there are any
    -- more rows to fetch.
    WHILE @@FETCH_STATUS = 0
    BEGIN

        PRINT 'REM Download ' + @Schema + '.' + @Table
        PRINT 'echo bcp.exe ' + @SrcDatabase + '.' + @Schema + '.' +
        @Table + ' out ' +
        @Schema + '.' + @Table + '.bcp -q -h "TABLOCK" -N -CRAW -S' +
        @SrcServer + ' >> bcp.log'
        PRINT 'bcp.exe ' + @SrcDatabase + '.' + @Schema + '.' + @Table +
        ' out ' + @Schema + '.' + @Table +
        '.bcp -q -h "TABLOCK" -N -CRAW -S' + @SrcServer +
        ' -U' + @SrcLogin + ' -P' + @SrcPassword + ' >> bcp.log'
        PRINT ''

        PRINT 'REM Upload ' + @Schema + '.' + @Table
        PRINT 'echo bcp.exe ' + @DestDatabase + '.' + @Schema + '.' +
        @Table + ' in ' + @Schema + '.' + @Table +
        '.bcp -q -h "TABLOCK" -N -E -CRAW -S' + @DestServer +
        ' >> bcp.log'
        PRINT 'bcp.exe ' + @DestDatabase + '.' + @Schema + '.' +
         @Table + ' in ' + @Schema + '.' + @Table +
         '.bcp -q -h "TABLOCK" -N -E -CRAW -S' + @DestServer +
         ' -U' + @DestLogin + ' -P' + @DestPassword + ' >> bcp.log'
        PRINT ''
   
       -- This is executed as long as the previous fetch succeeds.
       FETCH NEXT FROM table_cursor  INTO @Schema, @Table;
    END

    CLOSE table_cursor;
    DEALLOCATE table_cursor;
                 1.       Run the above scripts on SQL server 2008 R2 client query window. Copy the generated code into                           .                      a batch file.
                2.       Open Command prompt with an Administrator mode go to the folder and run the batch file.

    7.     Summary

               Hope the above steps can  help you migrate local database server to Azure.