Posts

Showing posts with the label azure

Use Azure Functions to Execute a SQL Azure Stored Procedure on a Timer

Image
It was hard to find this information all in one place, so I thought I'd throw a post together quick.

If you have the need to execute a stored procedure on a timer, that can be fairly easily accomplished with Azure Functions.

Create a new Timer Trigger Azure Function, set the cron timer as desired, and add code similar to the code below:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    var conString = ConfigurationManager.ConnectionStrings["ConString"]?.ConnectionString;

    using(var con = new SqlConnection(conString))
    {
        con.Execute("dbo.MyProc", commandType: CommandType.StoredProcedure);
    }
}


This code will not compile at this time. Note the name of the connection string, we'll need that in the app settings page.

Go to the Connection Strings section of the Application Settings for the function app (this will look very similar to the Con…

Azure Kudu Deployment with Version Number

I recently blogged about generating a version.txt file on every build via Visual Studio. This solution is fine if you are building from you local machine and using Web Deploy to deploy to Azure, but if you have a github webhook and are using kudu, the VS Post Build Events are not executed.

The easiest way I found to generate a version.txt file for every deployment is to add a post deployment action to your Web App/App Service.

Create a file within the /site/deployments/tools/PostDeploymentActions/ folder of your App Service (you may need to create that folder). You can name the file whatever you'd like, I chose `generategitversionfile.cmd`. The file should contain at least the following line:

git rev-parse HEAD > "..\wwwroot\version.txt"

This command will be executed within the context of the `/site/repository` directory, that's why we need a more fully qualified path for where to write the file.


Now, after every kudu deploy, a new version.txt file will be created …

Stop Azure WebJob from Azure WebSite

Image
I recently had the need to toggle the status of an Azure WebJob from the WebSite in the same Azure App Service. I found a couple half-complete answers online, but I thought I'd put together this simple guide to have a complete example all in one place.

To accomplish this, we'll be using the kudu REST API for WebJobs.

(note: I also looked into toggling the WEBJOBS_STOPPED environment variable from the WebSite, however I was not able to verify that solution was going to work. I confirmed I was able to set the environment variable from the website, however the value did not update in the "Application Settings" section of the Azure Portal and I was concerned the WebJobs may read a separate environment variable than the WebSite [process scoped vs machine envar for example])

The first thing you'll need to do is download the PublishSettings file (aka "Publish Profile") associated with the App Service (you need the username/password from this file to satisfy th…

Replace Azure Scheduler with Azure Functions

Image
I have written before about the shortcomings of the Azure Scheduler (doesn't integrate with classic [non-RM] Azure Storage accounts, message body pushed to Azure Storage Queue is wrapped in XML, costs $15/mo for basic scheduling tasks...) but the good news is, now with Azure Functions, your Scheduler Tasks can mostly be replaced with Functions.


Here are a couple tables to break down the differences in Triggers and Actions.
Scheduler Triggers compared to Function Input Bindings

Scheduler Functions Manual X X Timer X X Http
X Storage Queue

Azure Functions and AWS Lambdas

I'm working on a new talk about Azure Functions and AWS Lambdas. I'm writing this blog post to compile a list of links to more information for people that attended the talk.


Presentation Slides: https://drive.google.com/open?id=0BwgLvVq0rcS7cmxrQUhCWUxTNTQ

Azure Function Intro Source Code: https://github.com/Stonefinch/AzureFunctionIntro

Azure Intro Web App Source Code: https://github.com/Stonefinch/AzureIntro

Azure Function Tools for Visual Studio: https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/

AWS Lambda C# in Visual Studio: https://aws.amazon.com/blogs/developer/using-the-aws-lambda-project-in-visual-studio/


I'll add more links and update the slides as I continue to build out the talk.


Hope this helps,
Aaron








ASP.NET MVC 5 User Admin

AKA (for google-fu):
asp.net mvc 5 web site administration tool
asp.net mvc 5 web configuration tool
asp.net mvc 5 identity
asp.net mvc 5 membership

I have missed the asp.net Web Site Administration Tool that used to be included with Visual Studio. I created an alternative for asp.net mvc 4 you can find here.

For asp.net mvc 5, I created an Azure Site Extension to provide the same functionality. It can be run from VS on your local machine as well as installed as a site extension on Azure.

Links:
extension: https://www.siteextensions.net/packages/AspNetUserMaintenanceAzureSiteExtension/
github repo: https://github.com/Stonefinch/AspNetUserMaintenanceAzureSiteExtension


Hope this helps,
Aaron


UPDATE

These are now published to nuget.org https://www.nuget.org/packages/AspNetUserMaintenanceAzureSiteExtension/

Write Azure WebJob Logs to SQL

We've been working with Azure WebJobs recently (more info: here and here) and even though the built in logging to Azure Storage is great, it is difficult to query at times.

In more recent version of the WebJobs SDK, the team exposed a TraceWriter collection via the JobHost configuration that allows consumers to write logs to a custom repository.

The snippet linked below is a naive implementation of using that collection to write the logs to SQL Server. Feel free to adapt it to fit your needs (i.e. performance concerns, async, batching, etc.)

Write Azure WebJob Logs to SQL:
https://gist.github.com/aaronhoffman/3e319cf519eb8bf76c8f3e4fa6f1b4ae


Hope this helps,
Aaron


Azure HTTP Web Server Logs to SQL

Image
If you enable File System Web Server logs for your Azure App Service...

Settings > Diagnostics logs > Web server logging > File System





You'll start to see logs on the file system for your app service here:

(you can find your FTP host and credentials in the publish profile file)

/LogFiles/http/RawLogs




Alternatively, you can see these logs through the Kudu UI:

https://{yourappservicename}.scm.azurewebsites.net/DebugConsole/?shell=powershell



Instead of downloading these one and a time and parsing through them, you can use the following library to assist in loading them into a relational database.

You could even run this as a webjob within your app service.


Get the code here:
https://github.com/Stonefinch/AzureWebServerLogsToSql


**Update: This repo now includes a web project that references the console app as a WebJob.

You may also want to check out the LogParser tool by Microsoft https://blogs.msdn.microsoft.com/friis/2014/02/06/how-to-analyse-iis-logs-using-logparser-logparser…

Update Azure SQL Firewall Rule using PowerShell (update 2018)

Image
Microsoft Azure SQL only allows connections from whitelisted IP addresses. My ISP seemed to change my external/public IP address daily. The combination of these two things was very annoying for me.

I assembled the powershell script below to make my life a little easier. It updates the firewall rule for each Azure SQL instance, and ensures they are all set to my current IP address.

Note: There are two categories of Azure PowerShell scripts/cmdlets. There are Resource Management cmdlets and Service Management cmdlets. I will describe how to perform this task with both below:


Service Management (the older way)
1. Install and Configure https://github.com/Azure/azure-powershell#installation

2. Execute Get-AzurePublishSettingsFile to get the publishsettings file for the subscription you're going to be working with (a browser window will open, select the profile there. Note: you can add multiple publish settings.).




3. Execute Import-AzurePublishSettingsFile and provide the path to the f…