Showing posts with label Windows Azure. Show all posts
Showing posts with label Windows Azure. Show all posts

Monday, November 30, 2015

Azure Active Directory Authentication with roles


I got the requirement to implement Azure Active directory authentication in our project. When i searched for this then i came to know following articles which helped me to understand the how this authentication works and how to implement it.

Source Code is available at following location :

Brief introduction, how it works is mentioned at following links: 


Thursday, June 18, 2015

ServiceConfiguration transformation

ServiceConfiguration transformation:

This will help us to build the package immediately. You just need to add different configuration files, and give different values.

Add following sections in cloud solution ccproj
1)
<ItemGroup>
<ServiceConfiguration Include="ServiceConfiguration.production.cscfg" />
<ServiceDefinition Include="ServiceDefinition.csdef" />
<ServiceConfiguration Include="ServiceConfiguration.Dev.cscfg" />
<ServiceConfiguration Include="ServiceConfiguration.Int.cscfg" />
<ServiceConfiguration Include="ServiceConfiguration.Staging.cscfg" />
<ServiceConfiguration Include="ServiceConfiguration.Local.cscfg" />
<ServiceConfiguration Include="ServiceConfiguration.Cloud.cscfg" />
</ItemGroup>

2)

<PropertyGroup Condition=" '$(Configuration)' == 'Dev' ">
<OutputPath>bin\Dev\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Int' ">
<OutputPath>bin\Int\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Staging' ">
<OutputPath>bin\Staging\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Production' ">
<OutputPath>bin\Production\</OutputPath>
</PropertyGroup>

3)
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.targets" />
<ItemGroup>
<EnvironmentDefinition Include="ServiceDefinition.Debug.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Production.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Int.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Local.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Dev.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Cloud.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<EnvironmentDefinition Include="ServiceDefinition.Staging.csdef">
<BaseConfiguration>ServiceDefinition.csdef</BaseConfiguration>
</EnvironmentDefinition>
<None Include="@(EnvironmentDefinition)" />
</ItemGroup>
<Target Name="ValidateServiceFiles" Inputs="@(EnvironmentDefinition);@(EnvironmentDefinition->'%(BaseConfiguration)')" Outputs="@(EnvironmentDefinition->'%(Identity).transformed.csdef')">
<Message Text="ValidateServiceFiles: Transforming %(EnvironmentDefinition.BaseConfiguration) to %(EnvironmentDefinition.Identity).tmp via %(EnvironmentDefinition.Identity)" Importance="High" />
<TransformXml Source="%(EnvironmentDefinition.BaseConfiguration)" Transform="%(EnvironmentDefinition.Identity)" Destination="%(EnvironmentDefinition.Identity).tmp" />
<Message Text="ValidateServiceFiles: Transformation complete; starting validation" Importance="High" />
<ValidateServiceFiles ServiceDefinitionFile="%(EnvironmentDefinition.Identity).tmp" ServiceConfigurationFile="ServiceConfiguration.$(Configuration).cscfg" />
<Message Text="ValidateServiceFiles: Validation complete; renaming temporary file" Importance="High" />
<Move SourceFiles="%(EnvironmentDefinition.Identity).tmp" DestinationFiles="%(EnvironmentDefinition.Identity).transformed.csdef" />
</Target>
<Target Name="MoveTransformedEnvironmentConfigurationXml" AfterTargets="AfterPackageComputeService">
<Copy SourceFiles="ServiceDefinition.$(Configuration).csdef.transformed.csdef" DestinationFiles="$(OutDir)ServiceDefinition.csdef" />
</Target>

<Target Name="PublishVirtualApplicationsBeforeCSPack" BeforeTargets="CorePublish;CsPackForDevFabric" Condition="'$(PackageForComputeEmulator)' == 'true' Or '$(IsExecutingPublishTarget)' == 'true' ">
<Message Text="Start - PublishVirtualApplicationsBeforeCSPack" />
<PropertyGroup Condition=" '$(PublishDestinationPath)'=='' and '$(BuildingInsideVisualStudio)'=='true' ">
<!-- When Visual Studio build -->
<PublishDestinationPath>$(ProjectDir)$(OutDir)</PublishDestinationPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(PublishDestinationPath)'=='' ">
<!-- When TFS build -->
<PublishDestinationPath>$(OutDir)</PublishDestinationPath>
</PropertyGroup>
<Message Text="Publishing '%(VirtualApp.Identity)' to '$(PublishDestinationPath)%(VirtualApp.PhysicalDirectory)'" />
<MSBuild Projects="%(VirtualApp.Identity)" ContinueOnError="false" Targets="PublishToFileSystem" Properties="Configuration=$(Configuration);PublishDestination=$(PublishDestinationPath)%(VirtualApp.PhysicalDirectory);AutoParameterizationWebConfigConnectionStrings=False" />
<!-- Delete files excluded from packaging; take care not to delete xml files unless there is a matching dll -->
<CreateItem Include="$(PublishDestinationPath)%(VirtualApp.PhysicalDirectory)\**\*.dll">
<Output ItemName="DllFiles" TaskParameter="Include" />
</CreateItem>
<ItemGroup>
<FilesToDelete Include="@(DllFiles -> '%(RootDir)%(Directory)%(Filename).pdb')" />
<FilesToDelete Include="@(DllFiles -> '%(RootDir)%(Directory)%(Filename).xml')" />
</ItemGroup>
<Message Text="Files excluded from packaging '@(FilesToDelete)'" />
<Delete Files="@(FilesToDelete)" />
<Message Text="End - PublishVirtualApplicationsBeforeCSPack" />
</Target>

Happy Coding!!

Tuesday, April 28, 2015

Azure : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

Problem: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.


Solution:
In azure, while running the project in temporary folder it creates your azure project and its related files. some dll names are large so in result whole path is large and which results in above error.

There are 2 solutions to solve this issue:

Solution 1:
Step 1: Open .ccproj project
Step 2: Under PropertyGroup Add following line
           <ServiceOutputDirectory>C:\azuretmp\</ServiceOutputDirectory>

Source : http://govada.blogspot.in/2011/12/windows-azure-package-build-error.html


Solution 2:
Step 1: Right click on "My Computer" -> Select Properties
Step 2: Click on "Remote Setting" -> Select Advanced -> Select Environment variables -> Click on "New" -> Add Variable name as "_CSRUN_STATE_DIRECTORY"
and Add Variable value as whatever you want. ex: C:\A -> click on save
Source: http://blogs.msdn.com/b/jnak/archive/2010/01/14/windows-azure-path-too-long.aspx

Happy Coding !! 

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/

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());
        }
    }
}

Happy Coding!!