Azure App Service, Azure Blob, Azure Cloud, Cloud Computing

Azure storage data services types and How to store files in Azure Storage Account 2/2: Part 9

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

9. Azure storage data services types and how to store files in azure storage account 2/2

Now we will do the same upload process programmatically in visual studio, we will only focus on the relevant code, and we do not discuss any coding best practices as it is out of scope of this series, it is left on user’s own discretion.

Install WindowsAzure.Storage nugget package

Open your visual studio if you have not opened it already and open the solution with which we are working in this series. Right click on solution | Manage Nugget Packages | click on Browse | search for azure storage. Install latest stable version in our case it is 8.7.0 and accept the license. By the time you read this post, Microsoft may have released new version, so do not worry about it as all the code will still work.

Figure 5 Install Windows AZURE Storage nugget package

Open you Index controller and the code should look like below; all you need to do is modify the highlighted with your credentials to make it work.

Figure 6 Change settings for storage account container

Now to get the Blob service endpoint Go to Storage Account | Blob Service | under Primary blob service endpoint you will have the URL, copy it.

Figure 7 Blob service endpoint

For storage account and keys Go to your storage account | Access keys, although storage account can be retrieved from main overview window, but this view will give you both account name and keys in one location. Please remember that whoever have these keys, will be able to manipulate almost anything in the storage account level so you do not want to give these keys to everyone. Azure provides us SAS (shared access service) which is robust and should be used to limit access in production environments. You also have option to regenerate keys, which will invalidate the previous keys and will provide you new keys.

Figure 8 Blob service Storage account and keys

Coding in Visual Studio

In first Index method, all we are doing is listing the blob files in the container images and making a dictionary, which have image name as key and image URL as value, and we are passing it to view using ViewBag.files.

Figure 9 Get all files in container

In post Index method, we are checking for file first, if exists then we are creating reference to the blob container with file name, but you could also use GUID for your blob name. Note that if a blob/file exists in the container with particular name and you use the same name to upload the file again then existing file will be overwritten. We are also using file stream to mitigate the need to save the file on disk first, you can of course implement your logic accordingly.

Figure 10 Insert Files in container

Full code for home controller is as below:

Now let us move to the view part, it is very simple, we just added the code for form, which will post to index method of home controller and is multipart that means will contain file as post response. We create an input with type file and a submit button. Make sure that your input file name is the same as you have mentioned the parameter to post Index file, otherwise you will not get the file in response.

Figure 11 File Upload code in MVC

We also included the code to display file name and picture along with it.

Figure 12 View all images code

Full code for Index View as below:

Now run the application locally and upload a picture, you will see that the filename is encoded in html friendly name as it have spaces in it and images are shown. There are two pictures as we upload first picture right after we created storage account and the other one is uploaded programmatically.

Figure 13 Complete view of file upload

You can also verify the image upload by going to images container in storage account.

Figure 14 Image uploaded to images container

This code tutorial is just starting point; you can play with it and use this code to modify according to your needs. I hope this being information for you.

Coming up next How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger

Azure App Service, Azure Cloud, CICD, Cloud Computing, VSTS

How to use Visual Studio Team Services to do continuous Integration and continuous delivery: Part 7

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery

In this post, we will be using Visual Studio Team Services as our source code management, if you do not know what the VSTS is, then a short description is that it is a free service which enable source code management, automate and simplify your azure deployment. It also support continuous integration and various tools such as Git, package management, TFVC, Reporting and testing. It requires you to have an account for VSTS, if you do not have, you can start free. Free version can be used up to 5 users, you can check your options at following link

Once you have the account setup, go to http://my.visualstudio.com/ and click on Get Started under Visual Studio Team Services. We will use VSTS to make our Build server and to configure continuous integration and continuous delivery, of course there are a lot of features in the VSTS but we will only focus on Git and continuous Integration and continuous delivery.

Figure 1 Get Started with Visual Studio Team Services

Microsoft will take you to landing page, here you can create an account if you have not created already and will list down your accounts, select an account to proceed.

Figure 2 Create Visual Studio Team Services Account

Once you go to the dashboard of the VSTS, click on create new Project, fill the form and click Create. Remember to choose Git as version control. If you are planning to use TFS as version control, it is totally possible and steps to enable continuous integration and delivery are the similar.

Figure 3 Create Project in VSTS

Once the project is created you will be presented with some useful information to get started,but in our case, since we already have a source code so we will use visual studio to upload our source code to this project.

Figure 4 Dashboard after new git project is created

Open up the visual studio and load your project if you have not opened it already. Now Go to Tools | Options | Source Control | and make sure that Git is selected in Current source control plug-in drop down. If you use TFS then make sure you have selected TFS while you created your project in VSTS as source control.

Figure 5 Select Git as plugin

We have opened the project and are ready to check in our source code. Now right click on Solution and click Add to Source control, or at the very bottom right of the visual studio click Add to Source control. You will see two options one is with Visual Studio Team services and other once is to connect with GitHub. Since we are using VSTS, click on connect under Visual Studio Team Services. In this process visual studio will create a master branch on local repository for you and also configure security and folders that needs to be uploaded and will also put rules to ignore folders like bin, debug and obj, plus it will ignore any user specific settings such is publishing profiles etc. Visual makes it easy to work with different source control providers and make it easy to handle security of your source code by excluding some files which are developer specific or machine specific.

Figure 6 Configure Visual Studio Team Services as source control

Now if you are not logged in, go ahead and login, choose the visual studio account | select project and click on connect and then click on clone, as you can see that it will clone the project from local repository to the online repository.

Now re-open the project and right click on solution and choose commit, enter a message and then choose Commit staged. Then click on sync to server option. You can also click on upward arrow button, which is at the very bottom and it will present the same window or you can click on team explorer to get to the same menu. Now click on Publish Git Repo.

Now select your account and then click advanced and select you team project and click Publish repository. Once your source code is published it is time to enable the continuous integration and continuous delivery in VSTS.

Figure 7 Publish code to the git repository

Go back to the dashboard of your project in VSTS, you will see now that it have changed the view and now showing, set up Build button, You can also access this Set up Build, by going to Build and Release tab and then click on setup a new definition.

Figure 8 Continuous Integration set up

Choose Azure Web App template under Featured tab, and click apply.

Figure 9 New Definition Azure Web App

Now choose your subscription and click authorize complete the process, choose app service name, choose hosted agent as hosted VS2017. Click on Get Sources under Process tab.

Figure 10 Configure build Process

You can see that is already selected the Repository and branch, you can change these settings if you want to configure any other source i.e. GitHub. Go through all other tabs just to see what is in there; you do not need to change anything. Keep in mind this will deploy to the main App Service, you can also change the settings to deploy it on any slot you have in the App Service.

Figure 11 Repository configuration

Now click on Triggers tab and check Enable continuous integration, this option will automate our build when you check in the code.

Figure 12 Enable continuous Integration

Save the settings and click on Queue, or click on Save & queue option. We will manually trigger the build process to see what is the process, and will also check in the code in visual studio to aromatically trigger the build which will deploy the source code to our App Service.

Figure 13 Queue the changes to App service.

Now click on builds and select your application, you will see that the Build is successfully deployed.

Figure 14 successfully deployed the build

Click on link under recently completed to see more information. Now it is time to test out continuous integration next.

Figure 15 recently run Build

Go to VS2017 | Views | Home | Index.cshtml and change it like below and right click on project solution and chose commit | choose commit all | sync | in outgoing commit click push

<div
class=”jumbotron”>


<h1>ASP.NET</h1>


<p
class=”lead”>@WebConfigurationManager.AppSettings[“WelcomeMsg”]</p>


<p
class=”lead”>We have implemented continuous integration.</p>


<p><a
href=”https://asp.net&#8221;
class=”btn btn-primary btn-lg”>Learn more &raquo;</a></p>

</div>

Figure 16 Commit Changes

Figure 17 sync the changes

Figure 18 Push changes for continuous Integration to trigger.

Once the push is clicked, you will see that a new build task is added in queue and is in progress.

Figure 19 New build job is in progress

Once the build is successful, refresh you site and you will find that new changes have been applied.

Figure 20 build successful

If you want to know what exactly was deployed you can actually see the files and can even download them as well, click on any of the build in above screen and the click on artifacts tabs and then explore the drop folder. In this folder you will find the zip file, which contains the files that were deployed to the App Service, you also have parameters and commands, it could be useful if in some case you need to replicate the things on local system, you just need to download the artifacts and configure them locally. Now let us see the result on actual site after our recent continuous integration deployment.

Figure 21 Artifacts Explorer

Before changes

Figure 22 before changes applied

After changes applied through continuous integration.

Figure 23 after changes applied through continuous integration

Remember that this was a build definition that we created there is another robust definition called release definition which have many scenarios that may suit your needs.

Figure 24 Release Definitions

Congratulations! You have successfully completed the full life cycle of Azure development, I hope it will help you in setting up development environment and carry on the Azure development. Please go through site you may find other series that may interest you. Good Luck!

coming up next Azure storage data services types and how to store files in azure storage account 1/2

Azure App Service, Azure Cloud, Cloud Computing, Visual Studio

Remote debugging App Service using visual Studio, monitoring and configuring alerts: Part 3

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

  1. Remote debugging App Service using visual Studio, monitoring and configuring alerts

We have successfully configured the deployment slot in previous post, now we are going for remote debugging. In Azure remote debugging using visual Studio is not enabled by default, you have to enable debugging for specific VS version to debug. Although if you have permissions you can do it by Visual Studio if you do not have enough permissions, please ask your Azure Subscription owner. In our case we are the owner of Azure Subscription so we will proceed for debugging with out requiring any permissions,

If you have not already opened your visual studio, then open it and load the project you had deployed earlier. Now go to Cloud Explorer | your subscription | Select your app | Actions | Click on Attach debugger.

Figure 1 Remote Debugging

Debugger will be attached, it may take some time depending upon your internet connection speed and your location distance to Azure Data center, If you are getting error like below, that means you do not have permissions to debug or you have not enabled Debugging for your App Service, remember by default debugging is not enabled. In my case, it was referring to firewall settings and we found out that there were some ports closed which needed to be opened to communicate with Azure.

Figure 2 Error While connecting to Remote Debugging Session

Go to Azure Portal and then App Service | Settings | Application Settings | Debugging | enable Remote debugging and also choose visual studio version and click save. Now by this step your debugger should be attached successfully.

Figure 3 Enabling Remote Debugging

If you still not able to resolve the problem please consult my recent post which discuss the steps to fix common debugging issues.

Also check Microsoft page

Once you are in debugging mode, you can use all the tools that you use while debugging locally.

App Service Monitoring

Let us move on to monitoring our App Service.

Azure provide you multiple ways to monitor your App Service, Let us explore them, go to Azure Portal and then App Service | Overview, in this view you can see the basic information about http errors, data in, data out, number of requests and Average response time. All though this view will satisfy most users, but you can even drill down and generate custom views for you as well, just click on any of the chart and you will see a new view, in which you can tailor the chart according to your metrics.

Figure 4 Monitoring performance

For demo, We have added CPU Time into the mix, and we can see that display show both Average Response Time and CPU Time as well.

Figure 5 Create Custom chart

You can click on Pin to dashboard to see it on your main dashboard as well.

Figure 6 Pin to dashboard

Alerts

This bring us to our next topic alerts, alerts can be very useful especially when you have a lot of resources and do not have time to manually check the metrics to see what is going on. We can create alert right on a particular metric by clicking on

Or App Service | Monitoring | Alerts

Let us create an alert on CPU Percentage on our App service Plan ( it will include all the apps hosting on same App Service Plan) and choose CPU Percentage and set condition to “greater than” and threshold to 80. You will notice that the chart shows you current CPU percentage in dotted line and the threshold in straight line, do not forget to check email owners check box if you are the owner and want to get the alert email otherwise there is a textbox to add addition emails or call a webhook. Webhook is a restful endpoint, which is publically accessible to Azure and will be called when metric pass the condition we set. Mostly developer use webhook to initiate a workflow, which could for example increase number of running instances through PowerShell script or initiate a business workflow. To learn more about webhook see https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-webhooks-alerts

Figure 7 Create alert on CPU percentage for App Service Plan

As you, can see that I have created two alerts, one for CPU percentage and another for Memory percentage. Now when ever condition you set will meet you will get an email or a webhook will be called. Alerts truly simplify our monitoring task specially if we have a lot of resources to worry about.

Figure 8 Create Alert on CPU and Memory percentage

Congratulations, you have successfully debugged and monitored your application and configured alerts.

Coming up next Diagnostic logs, live stream, process explorer and KUDU

 

Azure App Service, Azure Cloud, Cloud Computing, Visual Studio

Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK: Part 2

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK

At this point, you have successfully created the App Service Plan, App service and published the changes to the Azure using Visual Studio and Azure SDK. Let us move to exciting part of remote debugging and monitoring, fire up your visual studio if you have not opened it before and open the project we created in recently.

Figure 1 App Service Deployment Slot

Let us talk about Deployment Slot, this is feature provided by App service, this basically enable you to deploy your code to different containers i.e. QA, Staging or Production, in this way you will make sure that your code is fully tested and ready to move to production. This approach support setting up different App Settings and connection strings for the Databases for each slot, which will enable us to test and deploy our changing with ease.

I know you are excited about this feature lets, let us open web.config and add a new App Setting

<add
key=WelcomeMsg
value=Welcome to Azure Development!/>

Figure 2 Add new App settings in web.config

Now show this setting in our index page, Views | Home | Index.cshtml and edit it as shown below:

@using System.Web.Configuration;

@{

ViewBag.Title = “Home Page”;

}

<div
class=”jumbotron”>


<h1>ASP.NET</h1>


<p
class=”lead”>@WebConfigurationManager.AppSettings[“WelcomeMsg”]</p>


<p><a
href=”https://asp.net&#8221;
class=”btn btn-primary btn-lg”>Learn more &raquo;</a></p>

</div>

Figure 3 Edit Index file

Now after this change just press F5 to lunch and see the change.

Figure 4 App setting message is showing

Now publish your solution to the Azure App service as we did in our previous post. You can see that new changes publish.

Figure 5 Publishing Changes to Azure App Service

We will override the changes in our App Service Settings; go to Azure Portal and then App Service | Settings | Application Settings, here we will add the key and value to override the default one, please note that we can also use this interface to add new keys as well, this interface is not limited to just overriding key values.

Figure 6 Override Application Settings in Azure Portal

Now refresh to see that message is changed as expected.

Figure 7 Override setting display

Now let us create slots for staging slot. Go to Azure Portal and then App Service, Deployment | Deployment Slots | Click Add Slot and name it as staging, in configuration Source choose the main app and click ok. It could take few minutes depending upon application size.

Figure 8 Add new Staging Deployment Slot

Once deployment slot is added successfully, click on it to see the settings.

Figure 9 Deployment Slot is added successfully.

Change the “WelcomeMsg” to “Welcome message from staging” and tick “slot setting” check box, as when we swap this deployment slot with production this setting will not move and will stay with this slot. This way we will be able to retain the settings that are slot specific.

Figure 10 Change App settings on Staging Deployment Slot

Now question arise how we are going to deploy our source code to staging slot, the answer is very simple Publish Profile, click on Over view and then click on Get Publish Profile and save it to local system.

Figure 11 Get Publish Profile Setting

Go back to visual studio | Publish and click on Create new profile, select Existing check box, click on import profile, click ok, select the staging profile that we got in previous step and click ok.

Figure 12 Import Profile

Figure 13 Import profile and publish

For testing purposes open Index.cshtml file and update it as below

<p
class=”lead”>We have update the project</p>

Figure 14 Update for staging

Publish the changes to staging slot by just clicking on publish; we can see that our recent changes have been published successfully.

Figure 15 Publish changes to staging slot

All these steps lead us to the exciting part of Deployment Slot Swap, Go to Azure Portal and then App Service, click on swap and click ok.

Figure 16 Slot Swap

Figure 17 Choose Swap Slot

Swapping operation will take some time to complete, in side-by-side comparison you can see that staging code is swapped to production and production code is swapped to staging. Remember that user will be experience any downtime in this operation will continue to use the application as they were before swap.

Figure 18 Production and Staging slot swapping

With this congratulation, you have successfully created new staging slot, deployed your code to staging slot, used sticky slot settings, and swapped production with staging.

Coming up next Remote debugging App Service using visual Studio, monitoring and configuring alerts