Monday, 13 July 2020
Moved
As a result, I've copied all old posts to Github pages where I can author everything in markdown.
https://alanparr.github.io/
All new posts will be at the above url.
Friday, 10 July 2020
Supporting SameSite None in .Net 4.6 or lower.
When they first did this in March, it caused a number of issues including breaking website integrations with some payment gateways.
If you're on .Net 4.7 or higher, Microsoft supports setting SameSite to None. The official recommendation is that if you want to use SameSite None, then you need to move up to .Net 4.7.2, which if you are able, you should absolutely do.
However, there are those of us who are stuck on .Net lower than 4.7 and there is nothing we can do about it and our employers want to know that their sites aren't going to start breaking come the 14th of July.
While trying to find a solution to this problem, I stumbled upon what appears to be a possible solution for those of us stuck on lower .Net versions.
var cookie = new HttpCookie("myreallyimportantcookie")
{
Value = "myreallyimportantcookievalue",
Secure = true,
Path = "/",
HttpOnly = true
};
As you'll see from the below image, we have a cookie with the secure attribute and httponly, but no samesite attribute.
Adding SameSite
In .Net 4.7.2, if we want to support SameSite, we simply add the SameSite attribute.var cookie = new HttpCookie("myreallyimportantcookie")
{
Value = "myreallyimportantcookievalue",
Secure = true,
Path = "/",
HttpOnly = true,
SameSite = SameSiteMode.None
};
Of course, we can't do this in .Net 4.5 as the SameSite property doesn't exist. Instead, we can do this somewhat gross thing:
var cookie = new HttpCookie("myreallyimportantcookie")
{
Value = "myreallyimportantcookievalue" + ";SameSite=None",
Secure = true,
Path = "/",
HttpOnly = true
};
And now if we run the application, we can see we have the SameSite attribute set to None.
Disclaimer #1
This solution is completely unsupported and a bit gross. The approved solution is to move to .Net 4.7.2 and if you are able, you should absolutely do that. But sometimes the real world places limits upon us and if you are in that situation, hopefully this will get you out of a bind.Disclaimer #2
I have only tested this in an MVC solution in .Net 4.6.1. Theoretically, I think it will probably work in versions lower than that unless the cookie value-handling semantics changed massively at some point in the past. If you want to use this, test for yourself and post a comment if it worked as maybe that will help someone else.Saturday, 13 June 2020
API Head-to-head Update : AWS S3 Vs Windows Azure Table Storage Vs Rackspace Cloud Files
The code
Initialise Provider
CloudIdentity cloudIdentity = new CloudIdentity()
{
APIKey = "mykey",
Username = "myusername"
};
var provider = new CloudFilesProvider(cloudIdentity);
Create container
provider.CreateContainer(containerName);
Upload file
provider.CreateObjectFromFile(containerName, testFile, blobName);
List Blobs (objects in Rackspace parlance)
provider.ListObjects(containerName).ToList();
Delete file
provider.DeleteObject(containerName, blobName);
Delete container
provider.DeleteContainer(containerName);Can't argue with the simplicity of the code, once you've initialised the provider, everything is one line.
The results
Worth noting that I don't have the original test file, so the file being uploaded here is one I happened to have lying around of this guy. It is only 1k larger, so don't expect it will have a massive effect on the results.| Operation | S3 | Azure | Rackspace |
|---|---|---|---|
| Create Container | 847 | 668 | 1915 |
| Upload 7Kb file | 83 | 92 | 230 |
| List Blobs (1) | 40 | 172 | 51 |
| Delete Blob | 47 | 35 | 112 |
| Delete Container | 240 | 45 | 68 |
Conclusion
Rackspace was slowest, but that isn't terribly surprising considering I was using a third-party 2-year old library, it isn't necessarily a realistic comparison, just for my own amusement.Sunday, 8 January 2017
Converting docx to PDF in Azure
We needed full access to Windows to do this, which meant a VM. The cheapest Windows VM in azure is a Basic A0 at less than £9 a month, much cheaper than commercial document conversion services I found, which were at least £20 a month and had really weird APIs that were going to be pretty tricky to integrate in to our application.
I implemented a Windows service using Topshelf and the original Free Spire.Doc code for the actual conversion and installed this on to the VM. It simply polls an Azure Storage Queue for a message and deserializes the body to the following class.
private class ConversionMessage
{
public string SourceBlobContainer { get; set; }
public string SourceBlobName { get; set; }
public string DestinationBlobContainer { get; set; }
public string DestinationBlobName { get; set; }
public string ConversionType { get; set; }
}
This simply contains the Azure Blob container and the name for the source document, and the destination container and name for the converted document. There is also a ConversionType property which only has one valid value currently, I added this to facilitate adding other conversions in the future.
When a message is received, the service then converts the document with freespire and puts the converted document in the destination container.
Below is all the code for doing the conversion and saving it.
private void Convert(ConversionMessage message)
{
Console.WriteLine(message);
var inputBlob = GetBlobReference(message.SourceBlobContainer, message.SourceBlobName);
var outputBlob = GetBlobReference(message.DestinationBlobContainer, message.DestinationBlobName);
if(message.ConversionType == "docxtopdf")
{
LogInfo("Beginning conversion, type: docxtopdf");
ConvertDocxToPdf(inputBlob, outputBlob);
}
else
{
LogError($"Invalid conversion type {message.ConversionType} received");
}
}
private CloudBlockBlob GetBlobReference(string container, string blobName) => _blobClient.GetContainerReference(container).GetBlockBlobReference(blobName);
private void ConvertDocxToPdf(CloudBlockBlob inputDoc, CloudBlockBlob outputDoc)
{
var inputStream = new MemoryStream();
inputDoc.DownloadToStream(inputStream);
inputStream.Seek(0, SeekOrigin.Begin);
var doc = new Spire.Doc.Document();
doc.LoadFromStream(inputStream, FileFormat.Docx);
var outStream = new MemoryStream();
doc.SaveToStream(outStream, FileFormat.PDF);
outStream.Seek(0, SeekOrigin.Begin);
outputDoc.UploadFromStream(outStream);
LogInfo("Conversion successful");
}
Holding all of this together is an Azure Function. This function is really simple, it just gets called whenever the docx file is created in Azure Blob Storage and creates the conversion message, and puts it in the queue for the Windows service on the VM to pick up.
public static void Run(CloudBlockBlob myBlob, CloudQueue queue, TraceWriter log)
{
log.Info($"ConvertWordQuoteToPdf function processed: {myBlob.Name}");
var filename = System.IO.Path.GetFileNameWithoutExtension(myBlob.Name);
var cm = new ConversionMessage();
cm.SourceBlobContainer = "docs";
cm.SourceBlobName = $"{filename}.docx";
cm.DestinationBlobContainer = "docs";
cm.DestinationBlobName = $"{filename}.pdf";
cm.ConversionType = "docxtopdf";
var msg = new CloudQueueMessage(Newtonsoft.Json.JsonConvert.SerializeObject(cm));
queue.AddMessage(msg);
}
One of the coolest things about this in my view, is that all of this required no changes to the main application at all, we just reacted to the creation of the docx file that it was already doing.
Sunday, 1 January 2017
Automating repetitive tasks with Azure Functions.
SETUP
{
"Revision":"c1b49afddh7c",
"Author" : "Author Name",
"Created_At" : "2016-09-27T11:54:58+00:00",
"Log" : "Updated version to cpm 1.9.40
Added fluffy bunny controller",
"Branch" : "develop",
"Project":"cpm",
}
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage.Table;
public static async Task<object> Run(HttpRequestMessage req, ICollector<CommitMessageTableEntity> commitLogTable, ICollector<<ommitMessage> releaseQueue, TraceWriter log)
{
string jsonContent = await req.Content.ReadAsStringAsync();
log.Info(jsonContent);
var data = JsonConvert.DeserializeObject<CommitMessage>(jsonContent);
log.Info(data.Log);
var te = new CommitMessageTableEntity();
te.Set(data);
try
{
commitLogTable.Add(te);
}
catch (System.Exception ex)
{
log.Info("An error occurred: " + ex.Message);
return req.CreateResponse(HttpStatusCode.OK, "An error occurred, see log for details.");
}
if(te.IsRelease) {
log.Info("This is a release, adding to queue so it gets added to Radius.");
releaseQueue.Add(data);
}
return req.CreateResponse(HttpStatusCode.OK, "Success");
}
We log this to an Azure Storage table, I've no use for this currently but it costs practically nothing and is an easy way to check if the function was called if I have any problems in the future.
{
"bindings": [
{
"webHookType": "genericJson",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "table",
"name": "commitLogTable",
"tableName": "commitlog",
"connection": "SourceIntegrationSA",
"direction": "out"
},
{
"name": "releaseQueue",
"queueName": "releasequeue",
"connection": "SourceIntegrationSA",
"type": "queue",
"direction": "out"
}
],
"disabled": false
}
{
"frameworks": {
"net46":{
"dependencies": {
"Dapper": "1.50.2"
}
}
}
}
Sunday, 3 January 2016
Windows 10 Mobile: An update
Even though my previous post was only published 6 days ago, it was written a week before that so I’ve now got 3 weeks of usage under my belt and felt the need to post an update.
The biggest issue I’ve encountered over the last few weeks, and which I forgot to incude in the previous post, was battery life. This was suffering quite a bit in diaily use to the point where regular use of email, Readit, Facebook and Twitter was rendering the battery dead before dinner time. To combat this, I reduced the telemtry levels down which seemed to have no effect. I was on the verge of quitting my bold experiment and retreating back to WP8.1 when Readit released an update with improved performance which also seems to have solved my battery problems, so it seems the issues weren’t with the OS itself, just a single app. It’s a poignant lesson about how a single regularly used app can uniwttingly completely alter your perspective of a platform.
My other issue, which I did highlight in my prevous post is performance. I’m glad to say that shortly after that post, this seemed to spontaneously improve dramatically. There were no new builds in this time period, I don’t know if there was some indexing going on that was taking an age to complete and was chomping CPU cycles, but all seems well now. I think the aforementioned performance issues in Readit may have also played a part in my negative impressions.
I am still loving the ability to reply to a text without unlocking. My security concerns aside, it is really convenient!
I’ve just started working on my first app, a basic music/podcast playing app with Band control integration (as MS have deemed to only update Band 2 with music controls, not Band 1, I’m doing my own!) While the model is quite different to what I am used to as a Win32 desktop and Web developer, it is quite consistent and I’m slowly starting to get my head around things. Debugging my app on the physical phone has been completely painless, although I wish I could say the same about using the emulator!
Just 3 days ago, I was seriously contemplating going back to 8.1. I am glad to say that I am now pretty happy with WM10. It’ll be interesting to see if Microsoft keep the same pace of development with WM10 after release as they have with Windows 10 desktop, of if they repeat their previous mistake of releasing a new version and then dropping focus and putting their efforts in somewhere else.
Realistically, MIcrosoft are never going to unseat Android and IOS from the 1 and 2 spots, but there is still a chance of carving a decent market share as a third-player, but only if they don’t screw it up again. This is their chance to waste, I hope they don’t.
Monday, 28 December 2015
Windows 10 Mobile: Impressions after a week.
I’ve been keeping a close eye on Windows 10 Mobile, and I haven’t really liked what I’ve seen. The navigation is a clear lift from Android, and not in a good way, and paradigms like the Pivot control which made WP8.1 unique seem to have disappeared. But I’ve never been one for sticking my head in the sand and sticking to the current version of something because the new version looks scary and different so, rather than wait for WM10 to be released, I decided screw it, I’ll get what is essentially the RTM build on my Lumia 1020 by joining the Insider Preview programme.
This was about a week ago, and these are my impressions after using WM10 on my daily driver for a week.
The Upgrade
The upgrade itself went very smoothly. Took about an hour and everything was exactly where I left it when it came back, even down to the Start layout which wasn't preserved when upgrading 8.1 to 10 on the desktop. Kudos for attention to detail though, as I had the old neutered Office app pinned to my start screen, the upgrade downloaded the new Excel, Word, and Powerpoint apps and put them in a tile group in the same spot where the old Office app was pinned. Not a massive feat of software engineering, but a nice touch.
Overall, the upgrade is much like going from Windows 8.1 to 10 on the desktop, generally a non-event with a few niggles that I'm confident will disappear over time.
Navigation
It’s still screwy, although not as bad as I expected. The mail app for example, has the old ellipsis menu at the bottom of the page AND the new hamburger menu at the top of the page. This is just plain confusing and I hope that apps will settle on using one method of navigation over time, even if it is the hamburger. I'm yet to decide if the "hold down the Start button to bring the screen down so you can access controls at the top of the phone with one hand" feature is a nasty hack to get around the idiotic decision to follow Android and put all navigation at the top of the phone away from the user's hand, or a clever trick to get around the idiotic decision to follow Android and put all navigation at the top of the phone away from the user's hand.
Performance
My initial impression of performance was that it was generally comparable to Windows Phone 8.1 on the same device, slower in some areas but faster in others. After a few more days of working with it however, it is definitely slower overall. Loading the main apps I use, such as Mail, Twitter, and Readit, can take seconds. Once they’ve loaded, performance is about the same as on WP8.1, the only issue here is initial load time.
Miscellaneous
The settings app is immeasurably better. The old one was just a completely unorganised list of options, with no sane grouping and a number of really useless names that don’t help you figure out where to find the setting you want. The new one follows the same layout and groupings as Windows 10, so that’s one advantage to the OSes sharing a larger amount of functionality these days.
The tiles are larger than on Windows 8, however the “Use More Tiles” option makes them too small in my opinion. Guess you can’t please everyone!
The ability to reply to texts without unlocking your phone is really convenient, although I’m slightly concerned about the security issues with such a feature.
Conclusion
My overall impression is that this initial release is a little rough. Obvious, keep in mind that even though build 10586 is RTM, I’m still running the insider preview so there may be extra telemetry enabled and reduced optimisations that may be hampering the performance. Is it as smooth as WP8.1? No. However, I still prefer it to Android even in it’s current state, and given that Windows 10 has improved from it’s already pretty stable condition upon release, if Microsoft keep up the same pace with WM10 as they did with desktop post-release, then I’m confident the rough edges will be gone fairly soon.
Will WM10 make Windows Phone a mainstream consumer phone OS finally? Again, no, I highly doubt it. But there are a lot of benefits to the shared code, features, and manageability of Windows 10 and it’s various derivations, including WM10, that may look very tempting to businesses, especially as that space is being rapidly vacated by Blackberry, there is room for a new mainstream business phone, and WM10 may just have a chance there.
Monday, 21 December 2015
In-place upgrade Windows?! You’ve got to be kidding me!
Before the release of Windows 8, this was my default reply to anyone who dared suggest doing an in-place upgrade of Windows. I’d done it before in upgrading from 98 to ME and I’d seen and heard many horror stories of failed in-place upgrades that it become clear that it wasn’t even worth the effort, you were going to have to do a fresh install either way, so you may as well make it plan A.
Then along came the £15 upgrade offer for Windows 8 shortly after it’s release in October 2012. It seemed like a no-brainer just to get the latest version of Windows for such a small price. So I went for it, in the expectation that I would have to do a clean install anyway, reducing the upgrade to simply the hoop I had to jump through to get the offer.
Imagine my surprise when it worked. There were no BSODs, no applications failing to load after the update, no driver issues, nothing. It. Just. Worked. The only thing I had to do was re-intsall Linqpad to get Windows Search to show in the results lists when searching for “linq”, but in retrospect, if I’d given it a day or two to reindex everything it probably would’ve picked it up on it’s own eventually. In the months after, I upgraded several more machines and witnessed a number of other upgrades, all of them completed with at most minor issues easily solved by driver/Windows updates, or no issues at all. My faith in Windows in-place upgrades was restored.
That upgraded OS served me faithfully until I elected do a clean install when replacing my spinning rust HDD with an SSD 6 months later. While I now trusted Windows upgrades, I still don’t trust transferring OSes between disks, been burned on that front numerous times too.
Then in early 2015, Microsoft announced the Insider Preview programme for Windows 10. Why not I thought, so I took a laptop, signed up, and in the following 6 months, saw in-place upgrade afer in-place upgrade take place, successfully too for the most part, while keeping in mind this was pre-release so breakages were expected. By the time Windows 10 was released in July, I had no hesitation in just going ahead with the in-place upgrade. To my delight, but not really to my surprise anymore, it just worked. I have since updated a number of machines from both Windows 7 and 8.1 to 10 and haven’t had a single failure yet or major issue that hasn’t been simply resolved by running Windows updates.
Whatever you may think of Windows 8 or it’s successors, Windows in-place upgrades are no longer the joke they once were. They’re a very appealing and extremely reliable way of updating to the most recent version of Windows without going through the chore of a clean install. I’ll take an hour to do an in-place upgrade vs spending a day doing a clean install any day!
Having said that, always back up anything important before doing an upgrade. Even if the upgrade process works 9999 times out of 10,000, you don’t want to be that unlucky 1.
Thursday, 17 December 2015
OpenLiveWriter–It’s like Windows Live Writer but it works with my blog!
Since I started blogging, I’ve had to suffer the apalling mess that is the blogger editor.
Windows Live Writer was hailed as the panacea of free editors, but whatever I tried I could never get it to work.
Then Scott Hanselman and a group of Microsofties resurrected it as Open Live Writer and less than a week later, blogger support now works!
Go download Open Live Writer now!
Wednesday, 16 September 2015
Windows 10 upgrade experience
Friday, 11 September 2015
UniqueIdentifier as a primary key, that will solve all of our problems!
In evolving a single-instance website to multi-instance one, one of the many problems I have faced is how to deal with database access when your website instances are on the opposite side of the world.
My solution to this was to use SQL Azure Data Sync, makes sense as my databases are already in SQL Azure anyway.
Facilitating this involved changing all of the integer primar keys of every table to a different type with a lower possibility of collisions when syncing between databases.
I thought a Guid in the .Net side and a UNIQUEIDENTIFIER in the database would be the perfect fit for this. I was very, very wrong.
While using uniqueidentifiers as PKs has virtually eliminated any possibility of a sync collision, there is a very undesirable side effect of very high index fragmentation, bringing my site crashing to it's knees as soon as the indexes reach a critical level of fragmentation.
As is customary, I decided to run some tests. Below are the results of inserting 10k records in to an empty table:
Ouch! 96% fragmentation vs 18% on 10k records. Now in reality I rarely insert 10k records at the same time, but certain operations involve hundreds and this level of fragmentation will occur over the course of time.
Regardless of what data type your PKs are, fragmentation will happen. But the massive downside of using uniqueidentifier is that this not only happens a lot faster, but also a simple defrag or rebuild indexes is not going to save you as the data is inherently, due to it's random nature, impossible to efficiently index.
My first idea was to use an identity column (in my case called clusterkey) for the clustered index and keep the PK as a Guid with the PK constraint being non-clustered. This would sort out the fragmentation problem. But unfortunately, SQL Azure Data Sync didn't like my clusterkey, I suspect because it is a non-PK identity column. Regardless of the reason, it's not viable, so I looked further.
A contact at Microsoft suggested using the SequentialId() function in SQL Azure (available as of the latest V12 release), but all of my Guids are generated in code, so this was too big a change for me. My colleague Dave came to the rescue by tracking down this article, which describes how to generate reasonably sequential guids in C# that should keep the clustered index happy.
I'll not repeat the contents of the article, which is a really informative read, but it seems to work. This is how my tests look now:
I'll take that, much better fragmentation and approximately the same cost for inserts.
Note: The insert time includes the time taken to generate the Guid/SequentialGuid in code. Also, don't take the fact that SequentialGuid is smaller here as an indication that it is consistently faster. During testing, the number was generally the same as Guids and ints, but was consistently faster as soon as I came to actually measuring it for this blog post!
As these kinds of distributed environments become more common, I suspect more and more people will hit similar issues so hopefully this will help someone avoid making the same mistakes I have.
Friday, 5 June 2015
Windows Phone 10 - First Impressions
If you want to run the Technical Preview, details are here. But seriously, heed Microsoft's warning. You don't want to apply this to your main phone. I haven't hit any bugs yet, but the many interaction problems mean I'd be really annoyed if I had installed this on my main phone.
The Good

The settings app is vastly improved over the one on WP8.1, which is essentially a load of very narrowly scoped categories in a really long list on the page. The new app is well thought out, and the categorisations seem to broadly match those on Windows 10 on the desktop, which means you experience with one should transfer pretty well to the other.The Bad
Yes, that good section whizzed by rather fast didn't it? While I generally like Windows 10, even though it does remove some of the minor features of Windows 8.1 that I quite like, WP10 takes this a step further and removes pretty much everything I like about WP in one fell swoop. Let's go through the list shall we?Command placement
WP was very predictable, all commands were at the bottom of the screen, easily reached by your thumb to summon them up. They were large and full width, so you could easily trigger them with the hand that is holding the phone without too much stretching. The overall one-handed experience with WP8.1 is very comfortable.A great deal of the commands seem to have moved to the new "hamburger" menus in WP10, which are situated in the top-left, about as far away from your phone-holding hand as you can get (unless you're left handed of course).
![]() |
| Top, bottom, ellipsis, where do I go? |
Swiping
As part of their march to look like everyone else, MS have removed the pivot control from their apps. This means that instead of just swiping, again with your phone-holding hand, to move between screens, you now have to stretchto either the hamburger menu or, in the case of the dialer, to the buttons situated in the middle-top of the screen OR the buttons situated in the middle-bottom of the screen. Add to that, the dialer still has the older ellipsis menu from WP8.1, this one app has about every interaction model available, except for the one that suited it the most. Now I'm sure the ellipsis will go before release, but MS seem to have lost sight of the fact that the Pivot control was one of the greatest things about WP8.1 and I can't help but think that they could've got away with just making it a little smaller to make things more familiar to Android/iOS users while still retaining the superior interaction.
Schizophrenic navigation in the UWP mail app.
I've noted this separately from the above as I genuinely don't know whether this is just because this is a TP or if this is how this is actually intended to work, but the interaction with the mail app is incredibly painful and I sincerely hope this is not a sign of what we can expect from the UWP platform. I generally don't like doing 1-to-1 comparisons against a preview build, but in this case it's necessary to show how much of backward step this is.In WP10: Tap hamburger (top-left), cog icon (bottom-right), tap options (in menu that appears from top of screen). My finger is going all over the phone here. It's quite a stretch and it's not even a big phone. On my Lumia 1020, I suspect this would be a 2-handed operation.
![]() |
| Ready to dropdown and nowhere to go. |

Desktop input controls on a phone
Most of the criticism aimed at Windows 8 is that it uses a phone UI on the desktop. It would appear misplaced UI elements is a two-way street. In the calendar app, and presumably all UWP apps, the dropdowns, rather than being full screen as they have been previously, are dropdowns just as they would be on the desktop. Except now they've got to contend with fat fingers and an on-screen keyboard that gives them no vertical height to occupy.
Slow animation on apps view
Now I'm nitpicking a bit, but WP at the moment is a very snappy, fast OS. You can get things done pretty quickly and there are no slow animations getting in your way, everything happens almost instantaneously. Long pressing on the back button to bring up the running apps view triggers in what feels like about 100-200ms and the animation is quick.On WP10, it feels more like 700-800ms and the animation is slow. I don't think this is down to the hardware as there is no visible lag, it just seems that they've slowed it down to make it smoother. Smooth === slow in this case.
Verdict
Not good. WP has always been a very different beast to other mobile operating systems, maybe this is part of the reason it hasn't taken off. But, in my view, the interaction with WP is faster, easier, and more natural than interacting with Android or iOS. I haven't had a great deal of contact with iOS and even less with BlackBerry, but I had Android phones for about 6 years before my current Lumia and while I didn't hate using them, WP was the first mobile operating system that I can actually describe as being pleasant to use.While there are a lot less WP users, those who stick with it tend to be much more satisfied with it than users of other operating systems. This survey, while admittedly a couple of years old, proves the point (at least among the users Reader's Choice surveyed).
In their attempts to try and appeal to Android and iOS users, MS seem to have forgotten the things that make WP so pleasant to use. While I do not blame them at all for trying to make the experience more familiar to users of other OSes so the move doesn't seem quite so scary to those used to Android or iOS, you've got to think of your existing users while trying to appeal to new ones.
I suspect that, if the current preview is anything to go by, WP10 is just going to look like a pale imitation of Android or iOS, and that's a damn shame.
Thursday, 21 May 2015
Getting started with Azure Application Insights
- The instrumentation key (commonly referred to as the iKey) is located in an applicationinsights.config file, which is not very helpful if you want to change it when deploying to Live or Staging environments.
- If you use any monitoring system, such as Traffic Manager, Web Apps AlwaysOn, or any Web Testing application, this all gets included as "real traffic", which is fair enough as AppInsights has no way of knowing that it isn't real traffic. You may want to see it, but I personally do not so I wanted a way to filter it out.
My particular variation of his code adds the SiteIdentifier and the assembly version to the telemetry.
public class AppInfoApplicationInsightsConfigInitializer : IContextInitializer
{
public void Initialize(TelemetryContext context)
{
context.Properties["SiteIdentifier"] = System.Configuration.ConfigurationManager.AppSettings["SiteIdentifier"];
try
{
var verAtt = (AssemblyInformationalVersionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)[0];
context.Component.Version = verAtt.InformationalVersion;
}
catch (Exception)
{
context.Component.Version = "Application Version Unknown";
}
}
}
I also have a TelemetryInitializer that finds traffic from load balancers and web testers as explained at the top of this post, and reclassifies it as synthetic traffic, making it easier to exclude from charts and reports. I found that this traffic shows up as a different user every time, making my user count orders of magnitude higher than it should be. public class SyntheticSourceInitializer : ITelemetryInitializer
{
public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
{
if (HttpContext.Current == null)
return;
//Set traffic manager check and web test request to synthetic.
if (HttpContext.Current.Request.Url.ToString().EndsWith("/monitoring"))
{
telemetry.Context.Operation.SyntheticSource = "AzureAliveCheck";
}
//Set Azure Web Apps AlwaysOn pings to synthetic.
if (HttpContext.Current.Request.UserAgent == "AlwaysOn")
{
telemetry.Context.Operation.SyntheticSource = "AzureAliveCheck";
}
}
}
You've got full access to the current request so you can identify the traffic however you need to (referrer, headers, request url, etc)I then tell AppInsights to use these classes with the below entries in Application_Start() in global.asax.cs
//Configure application insights.
TelemetryConfiguration.Active.InstrumentationKey = System.Configuration.ConfigurationManager.AppSettings["iKey"];
TelemetryConfiguration.Active.ContextInitializers.Add(new AppInfoApplicationInsightsConfigInitializer());
TelemetryConfiguration.Active.TelemetryInitializers.Add(new SyntheticSourceInitializer());
As I dig further in to Application Insights, if I find more examples of useful overrides for default behaviour, I will add additional blog posts detailing these.
Wednesday, 15 April 2015
Close a window by title in C#
When you're developing for embedded systems that don't have a mouse or keyboard attached, a misbehaving program that decides to pop up windows at random is suddenly a lot more inconvenient.
Cue the below code snippet, which takes in a window title and sets it's state to minimised, maximised, or normal depending on the parameters you pass in. As usual, this is a Linqpad script. You just need to add a reference to System.Runtime.InteropServices, which is part of .net 4 and above.
void Main()
{
var windowTitle = "Untitled - Notepad";
IntPtr hWnd = FindWindow(null, windowTitle);
if (!hWnd.Equals(IntPtr.Zero))
{
ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);
}
}
// Define other methods and classes here
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);
This just imports the FindWindow and ShowWindowAsync methods from the user32 assembly. FindWindow is used to find the window we want to close. This return a pointer to the window handle, which we then pass to ShowWindowAsync along with an int indicating what we want to do to the window.
In the above example, I already know the window title, but this is unlikely to be the case in the real world. You can get this with the below snippet which will select out the window title and the process name. You can obviously add a where clause and modify the results based on your needs.
Process.GetProcesses().Select (p => new{p.ProcessName, p.MainWindowTitle})
Friday, 16 January 2015
ProTip: Open Powershell as admin from Powershell
If I am in a standard Powershell prompt and need to get an admin one open, I used to search for Powershell, right-click, run as admin. I'd do this even if I was already in a Powershell prompt as I can never remember the syntax for runas.exe.
A much easier way, especially if you are already in a Powershell prompt is:
Start-Process powershell -verb runas
This works for any executable and will pop up UAC appropriately to allow you to enter credentials if you need to, or just run as admin if you are already an Administrator.
Hope this helps some one.
Edit
This can be further shortened to
start powershell -verb runas
Thanks anonymous user!
Thursday, 9 October 2014
Copying records between tables in different Azure accounts : The Next Generation
- It's all very custom, you need to give it the type that is in the table and add lambdas to make sure it doesn't try to select the wrong object.
- Due to a change in the Azure Storage Library, it no longer works.
I hadn't used this script in a while but now I have an impending need for something like it but better that will allow me to copy the contents of all the tables or a defined subset of tables in a given account, enter the DynamicTableEntity which allows you to get an entry from a table as a dynamic object.
To run the code, open up Linqpad and add a reference to the Windows Azure Storage library, best way to do this is using Nuget.
void Main()
{
var srcClient = CreateClient("source account connection string");
var destClient = CreateClient("destination account connection string");
var mappings = new List<Tuple<string,string>>();
//Manually setup mappings.
//mappings.Add(new Tuple<string,string>("table1","table1copy"));
//mappings.Add(new Tuple<string,string>("table2","table2copy"));
//Copy all tables from the src account in to identically named tables in the destination account.
var tables = srcClient.ListTables(null, new TableRequestOptions(){PayloadFormat = TablePayloadFormat.JsonNoMetadata});
mappings = tables.Select (t => new Tuple<string,string>(t.Name,t.Name)).ToList();
Copy(srcClient,destClient,mappings);
}
public void Copy(CloudTableClient src, CloudTableClient dest, List<Tuple<string,string>> mappings) {
mappings.ForEach(x=>{
var st = src.GetTableReference(x.Item1);
var dt = dest.GetTableReference(x.Item2);
dt.CreateIfNotExists();
var query = new TableQuery<DynamicTableEntity>();
foreach (var entity in st.ExecuteQuery(query))
{
dt.Execute(TableOperation.InsertOrReplace(entity));
}
});
}
public CloudTableClient CreateClient(string connString){
var account = CloudStorageAccount.Parse(connString);
return account.CreateCloudTableClient();
}
In the above code, we create CloudTableClients to represent the source and destination accounts, then we build a mapping of source and destination tables.
We can do this manually if we only want to copy some tables and/or we want the destination tables to have different names to the source tables.
Alternatively, we can get a list of all the tables from the source and use that to build a 1:1 map, this will have the result of copying all items in all tables from the source account to the destination.
The Copy method simply takes the clients and mapping and does some iteration to get the items from each table from the source account and save them to the destination account.
Note: The code above is horribly inefficient for copying large amounts of data as it inserts each request individually. In a follow up post, I'll make this more efficient by making use of the TableBatchOperation.
Sunday, 31 August 2014
Using Custom Model Binders in ASP.Net MVC
What is (Custom) Model Binding?
Model Binding is the process through which MVC takes a form post and maps all of the form values in to a custom object, allowing you to have a POST action method which takes in a ViewModel and have it automagically populated for you. Custom Model Binders allow you to insert your own binders for particular scenarios where the default binding won't quite cut it.Creating our custom binder
We have the following typical example ViewModel: public class MyViewModel
{
public string MyStringProperty { get; set; }
}
It's just a class, nothing special about it at all. Now we want to manually handle the binding of this model because we want to add some text to the end of MyStringProperty when it gets bound. This is unlikely to be something you would want to do in real life, but we're just proving the point here.This is our binder:
public class MyViewModelBinder:IModelBinder
{
protected System.Collections.Specialized.NameValueCollection Form { get; set; }
private void Initialise(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Form = controllerContext.HttpContext.Request.Form;
}
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Initialise(controllerContext, bindingContext);
var msp = Form["MyStringProperty"];
return new MyViewModel {MyStringProperty = msp + " from my custom binder"};
}
}
Model Binders need to implement IModelBinder and have a BindModel method. This gives you access to the controllerContext from which you can access HttpContext and the bindingContext, which admittedly I have never had to use.In our binder, we just manually pick up the MyStringProperty value from the form, add it to a new instance of our object and return it, adding our incredibly important piece of text to the end of the retrieved value.
Using our Custom Binder
There are 2 ways we can use our custom binder, which one we use depends on each scenario. If we need to override the binding of a class for a particular Action method, we can use the ModelBinder attribute on the relevant parameter of the Action Method: [HttpPost]
public ActionResult Index([ModelBinder(typeof(MyViewModelBinder))]MyViewModel model)
{
return View(model);
}
This will apply our Custom Binder to this property (MyViewModel) for this action only, no other actions or controllers will be affected.Alternatively, if we want to apply our custom binder to MyViewModel globally within the application, we can add the following line to Application_Start in global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ModelBinders.Binders[typeof(MyViewModel)] = new MyViewModelBinder();
}
Using this method, everywhere a parameter of type MyViewModel is encountered on an ActionResult, our custom binder will be invoked instead of the standard one. Because this applies globally, we do not need the ModelBinder attribute on our Action Method so the overridden behaviour is completely transparent to the controller, promoting code reuse and keeping model binding logic where it belongs.
Wednesday, 6 August 2014
API Head-to-head: AWS S3 Vs Windows Azure Table Storage
I thought I’d take a deeper look at both APIs and see how they compare. I’ll go through some standard operations, comparing the amount of code required to perform the operation.
If you want a comparison of features, there are plenty of blog posts on the subject, just Bingle It
All the code in this test is being run in Linqpad, using the AWS SDK for .Net and Windows Azure Storage Nuget packages.
Create the client
Both Azure and S3 have the concept of a client, this represents the service itself and is where you provide credentials for accessing the service.Azure
var account = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("connectionstring");
var client = account.CreateCloudBlobClient();
S3
var client = AWSClientFactory.CreateAmazonS3Client("accessKey", "secret",RegionEndpoint.EUWest1);
S3 wins on lines of code but I don’t like having to declare the datacenter the account is in. In my opinion, the application shouldn’t be aware of this. 1 point to Azure.
Creating a container
This is a folder, Azure refers to is a container, S3 calls it a bucket.Azure
var container = client.GetContainerReference("test-container");
container.CreateIfNotExists();
S3
try
{
client.PutBucket(new PutBucketRequest { BucketName = "my-testing-bucket-123456", UseClientRegion = true});
}
catch (AmazonS3Exception ex)
{
if(ex.ErrorCode != "BucketAlreadyOwnedByYou") {
throw;
}
}
S3 loses big time on simplicity here. To my knowledge, this is the only way to do a blind create of a container, that is creating it without knowing up front if it already exists. Azure makes this trivial with CreateIfNotExists. 2 points to Azure.
Uploading a file
Azure
var container = client.GetContainerReference("test-container");
var blob = container.GetBlockBlobReference("testfile");
blob.UploadFromFile(@"M:\testfile1.txt",FileMode.OpenOrCreate);
S3
var putObjectRequest = new PutObjectRequest {BucketName = "my-testing-bucket-123456", FilePath = @"M:\testfile.txt", Key = "testfile", GenerateMD5Digest = true, Timeout=-1};
var upload = client.PutObject(putObjectRequest);
They’re pretty much equal here, but the S3 code is more verbose. I like the idea of getting a reference to a blob while not knowing if it actually exists or not.
List Blobs
Azure
var container = client.GetContainerReference("test-container");
var blobs = container.ListBlobs(null, true, BlobListingDetails.Metadata);
blobs.OfType().Select (cbb => cbb.Name).Dump();
S3
var listRequest = new ListObjectsRequest(){ BucketName = "my-testing-bucket-123456"};
client.ListObjects(listRequest).S3Objects.Select (so => so.Key).Dump();
In terms of complexity, they’re pretty even here too. Azure has one more line but it’s not a difficult one. Notice that whereas with Azure, we get a reference to a container and then perform operations against that reference, with AWS all requests are individual so you end up having to explicitly tell the client for every operation what the bucket name is. Point to Azure.
Deleting a Blob
Azure
var dblob = container.GetBlockBlobReference("testfile");
dblob.Delete();
S3
var delRequest = new DeleteObjectRequest(){ BucketName = "my-testing-bucket-123456", Key="testfile"};
client.DeleteObject(delRequest);
Neither code is particularly complicated here, but I prefer Azure’s simplicity with the container and blob reference model so point Azure.
Delete a Container
Azure
var container = client.GetContainerReference("test-container");
container.Delete();
S3
var delBucket = new DeleteBucketRequest(){ BucketName = "my-testing-bucket-123456"};
client.DeleteBucket(delBucket);
Again, pretty equal. To micro-analyse the lines, you could say that for Azure, you’ve got one potentially reusable line, and one throw-away line. With S3, they’re both throw away. But in reality, unless you’re doing thousands of consecutive operations, it doesn’t really matter.
Conclusion
In terms of complexity, Azure’s and S3’s APIs are pretty much equal, but it’s easy to see where they each have their uses. Azure’s API is a much thicker abstraction over REST, whereas the S3 API is such a thin-veneer that you could imagine a home-grown API not turning out that differently (but most likely not as reliable).In my mind, if you’re doing lots of operations against lots of different blobs and containers then S3’s API is more suitable as each operation is self-contained and there are no references to containers or blobs hanging around.
If you’re doing operations which share common elements, such as performing numerous operations on a blob or working with lots of blobs within a few containers, Azure’s API seems better suited as you create the references and then reuse them, reducing the amount of repeated code.
Bonus Section
If you could be bothered to read past my conclusion, congratulations on your determination! The comparative speed of Azure and AWS has been done to death, but I couldn’t resist getting my own stats.These are ridiculously simple stats, essentially Stopwatch calls wrapped around the code in this post. The file I am uploading is only 6k. The simple reason for this is that everyone tests how these services handle lots of large objects, but no one seems to cover the probably more common scenario of users uploading very small files. The average size is probably higher than 6kb, but this is what I’ve got hanging around so this is what I’m using.
So here are my extremely simple and probably not at all reliable benchmarks.
| Operation | S3 | Azure |
|---|---|---|
| Create Container | 573 | 279 |
| Upload 6Kb file | 99 | 55 |
| List Blobs (1) | 41 | 103 |
| Delete Blob | 55 | 45 |
| Delete Container | 221 | 38 |
Not covered in this post: Both APIs also have the Begin/End style of async operations and Azure has the bonus of async operations based on the async/await pattern, I may do another post on that in the future.
TL;DR; Azure's API is in my opinion a better abstraction and it's faster for most operations.
Friday, 18 July 2014
Upgraded to Azure Storage Emulator 3.2, where have all my tables gone?
Good - The error stopped happening.
Bad - Where the f**k have all my tables gone!
I'll be buggered if I'm recreating and repopulating them all so I went hunting. I managed to find the emulator database is in C:\Users\<username>\. In that directory you'll find mdf files called WAStorageEmulatorDb**.mdf where ** is the version number. I had ones ending in 22, 30, 32. Each will be accompanied by a _log file.
I loaded them up in Linqpad and the schemas looked the same, so for a punt I just renamed the files ending in 32 to something else and the renamed the files ending in 30 to 32.
Start up the emulator and everything is present again. That saved me a few hours!
Wednesday, 19 March 2014
Copying records between tables in different Azure accounts
Deploying the new Cloud Service was easy.
Taking a backup of the EU database and deploying it to Hong Kong was also easy.
However, recently I've been making increasing use of Azure Table storage for trivial data storage scenarios where the data isn't relational and the data will want to be shared amongst multiple instances eventually without having to wait for a database sync. It was at this point that I realised, I have no way of copying data from one storage account to another.
Time to correct that!
public void Transfer<T>(Microsoft.WindowsAzure.Storage.CloudStorageAccount fromAcc, Microsoft.WindowsAzure.Storage.CloudStorageAccount toAcc, string table, Expression<Func<T,bool>> expr) where T: TableServiceEntity {
var fromTC = fromAcc.CreateCloudTableClient();
var fromT = fromTC.GetTableReference(table);
var toTC = toAcc.CreateCloudTableClient();
var toT = toTC.GetTableReference(table);
toT.CreateIfNotExists();
var fromContext = fromTC.GetTableServiceContext();
var toContext = toTC.GetTableServiceContext();
var fromData = fromContext.CreateQuery<T>(table).Where(expr);
foreach (var item in fromData)
{
toContext.AttachTo(table,item);
toContext.UpdateObject(item);
}
toContext.SaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate);
}
This Transfer method takes in a from account and a to account and the name of the table.
The last parameter is an expression for the where clause. This is for scenarios where the same table contains multiple types of objects and you just want to query out the ones of a particular type for transfer using whatever clause is appropriate.
T must derive from TableServiceEntity and be the type of the object from which the record originated, or one that is similarly shaped.
The method is quite straight forward, it just fires up 2 table clients, gets a reference to the table specified by the table parameter, creates it on the receiving end if it doesn't exist (I think it's safe to assume that it already exists at the source end), queries out the data, then attaches it to the destination context and saves changes.
This upserts all of the data in to the source table.
Usage is simple:
var fromAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accesskey");
var toAccount = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=accesskey");
Transfer<MyProject.MyType1>(fromAccount, toAccount, "sharedtable", p=>p.PartitionKey == "Type1");
Transfer<MyProject.MyType2>(fromAccount, toAccount, "sharedtable", p=>p.PartitionKey == "Type2");
Transfer<MyProject.SomeOtherType>(fromAccount, toAccount, "someothertype", p=>p.PartitionKey != "");
Put all this together in Linqpad and you've got a simple way to transfer records between accounts on an ad hoc basis. As expected, it works with the Storage Emulator so you can use it to clone the contents of a production account down to your local dev machine and vice versa.



