Tuesday, 21 January 2014
Xero API .NET Client - Implementing your own ITokenRepository
So here is a bit of a brain-dump of my experiences...
To perform any API operation, you need to first instantiate a Session. In our case we have Partner access, so we need to create a XeroApiPartnerSession. The constructor requires an ITokenRepository (it also requires Certificates - but that's for another blog post!).
So since you can't create an instance of an Interface, you need to create your own class that implements ITokenRepository, and inside that class it's up to you to deal with the loading & saving of access tokens.
When I first saw this, I thought it was overkill - why couldn't I just pass in my access token?!? But I delved more into the API I learned that access tokens expire after 30 minutes - even with Partner access. This means you need to Renew your access token periodically. The good news is that if you implement an ITokenRepository, all this complexity is fully handed by the .NET API, so it's a pretty good design after all.
So onto creating a Token Repository... The ITokenRepository interface looks like this:
As you can probably guess, you need to make the Get methods return your tokens, and your Set methods need to save the tokens. My initial idea was just to Serialize the AccessToken and RequestToken classes to a file - easy peasy - until I realised those classes weren't marked as Serializable...
So that meant I would need to store the values of the properties in the Set methods, and then re-create the classes in the Get methods. And that's what I did:
For simplicity sake, I decided to serialise all the required properties to my own JSON object, as I didn't see the need for specific DB columns etc - but that's up to you. However note that ALL of the properties I am save & loading here are required! If you miss any (which I initially did), things will not work correctly within the API.
Anthony.
Wednesday, 23 February 2011
Running a ClickOnce Application as Administrator
private bool IsRunAsAdministrator()
{
var wi = WindowsIdentity.GetCurrent();
var wp = new WindowsPrincipal(wi);
return wp.IsInRole(WindowsBuiltInRole.Administrator);
}
private void Application_Startup(object sender, StartupEventArgs e)
{
if (!IsRunAsAdministrator())
{
// It is not possible to launch a ClickOnce app as administrator directly, so instead we launch the
// app as administrator in a new process.
var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);
// The following properties run the new process as administrator
processInfo.UseShellExecute = true;
processInfo.Verb = "runas";
// Start the new process
try
{
Process.Start(processInfo);
}
catch (Exception)
{
// The user did not allow the application to run as administrator
MessageBox.Show("Sorry, this application must be run as Administrator.");
}
// Shut down the current process
Application.Current.Shutdown();
}
else
{
// We are running as administrator
// Do normal startup stuff...
}
}
// The following properties run the new process as administrator
processInfo.UseShellExecute = true;
processInfo.Verb = "runas";
Wednesday, 24 March 2010
Table Storage Backup & Restore for Windows Azure
If you're using Table Storage in Windows Azure you're probably well aware of its real-time replication of data, which for me was a key factor in deciding to use the technology.
That said, I think the ability to perform a traditional database backup or restore (i.e a snapshot of the database) would be a really nice feature - which Table Storage does not currently support. Here are my top reasons why:
- Data replication may protect you from disk faults, but it doesn't protect you from accidental or malicious deletion. You'll only get this by taking snapshots of your data and storing it elsewhere.
- From a testing perspective, it can be really handy (or sometimes imperative) to "copy back" your production DB to your UAT or development environment.
So I thought I could write my own backup tool that retrieves all data via queries and stores it in a file - and then restore it back again by performing inserts. What started as a small & quick project turned into something much bigger - so I'm releasing it as open source.
Download Table Storage Backup
The project consists of 3 components:
- Backup Server. The backup server can be installed in your existing Web Role or Worker Role. The backup server performs all backup & restore operations within the Windows Azure environment.
- Backup Client. The backup client provides a friendly way of performing a backup & restore from a Windows PC.
- Backup Library. You can use the backup library to implement your own backup system or automate your backup operations, e.g. perform backups on a schedule.
How does it work?
- Data is backed up by retrieving all entities from all tables. The maximum number of entities are returned per table service query (until a partition entity is hit or 1000 entities are returned).
- Data is restored by performing batch insert operations. The maximum number of entities are inserted per batch (100 entities per partition or 4mb batch size).
- All transactions are performed at the raw REST level for efficiency, and to ensure data is duplicated precisely.
Please Contribute!
If you have any questions, feedback or bug reports please post them on the CodePlex site - and if you'd like to work on this project directly please contact me!
Cheers,
Anthony.
Wednesday, 23 December 2009
Azure Table Storage Client Extensions
Azure Table Storage is my storage technology of choice for my Windows Azure App. For anyone who’s used this technology, you’ll be well aware of its limitations. One in particular is its lack of relational functionality (aka table joins). For example, take the following LINQ query:
from o in context.Orders
join c in context.Customers on o.CustomerId equals c.CustomerId
select new
{
o.OrderNo,
c.FirstName,
c.LastName,
o.Total
};
This query will simply fail with a The method 'Join' is not supported. error (not to mention I’m also trying to use projection which will also fail).
But I was thinking that wouldn’t it be cool if a query like this just worked, and was smart enough to perform the required asynchronous queries behind the scenes to return the data.
Now of course there would be a performance hit in doing this, but for small datasets the overhead should be more than acceptable. For large data sets, the only workable option is to denormalise your data beforehand.
So I’d like to introduce my (highly experimental) XTableServiceContext:
Download from CodePlex
XTableServiceContext is a replacement to the standard TableServiceContext class provided by Microsoft. It aims to provide identical functionality, but without the limitations. At the time of writing this blog, the following additional features are supported:
- Relationships via Join()
- Projection via Select()
However I hope to keep filling in the gaps (Union, GroupBy etc) as I need more functionality.
Test Harness
Thanks to the guys at Microsoft Australia for giving me some extra CTP tokens, I'm able to publish the test harness while the CTP is still available:
http://storageclientext.cloudapp.net/
Important Note!
Remember that we are still limited by the underlying technology provided by Table Storage, and that this class retrieves data using the most efficient method available. This may mean performing multiple transactions behind the scenes so please keep this in mind.
Please send me your questions, feedback and suggestions!
Monday, 16 November 2009
Development Storage Sync (Azure November 2009 SDK)
With the November 2009 release of the Windows Azure SDK, Microsoft have changed the development storage to support dynamic schemas - just like the real Azure environment.
Because of this change, there is no longer a need to run the Create Test Storage Tables function (hoorah!) - which also means my existing Development Storage Sync tool is now defunct too.
However not all is sunshine and fairies. In order for development storage to update its schema, you must first insert an entity with the most recent properties (see this forum). If you don't, you'll get a crash if you attempt to query on a property that's not in the schema.
Essentially this means that you still need to update your development db each time you change an entity!
To make life easier, I've updated my existing Development Storage Sync tool to do this. Using reflection, it will update the schema for your entire context in one action:
Download Development Storage Sync
Installation
The application requires a reference to your assemblies that contain your TableServiceContext - which means you can't just double-click the app to run it. However a really easy way to run the app is to create an External Tool in Visual Studio. To do this:
- Click Tools > External Tools...
Add a new External Tool:
The Command field should point to DevStorageSync.exe in your installation directory.
The Arguments field should point to your assembly that contains your TableServiceContext (surround with talking marks, separate multiple assemblies with a semicolon).
Now, whenever you want to synchronise your development db, just click Tools > Sync Development Storage.
Please let me know of any bugs or feedback.
Cheers,
Anthony.



