Blog

RestSharp with Async Await

RestSharp is an excellent open source project to use in a Windows Phone app if you want make http calls to a json api. However it doesn't have any inbuilt support for the async await syntax. Thankfully with C#'s extensions methods we can add this support in our app.

1namespace RestSharpEx
2{
3 public static class RestClientExtensions
4 {
5 public static Task<IRestResponse> ExecuteTaskAsync(this RestClient @this, RestRequest request)
6 {
7 if (@this == null)
8 throw new NullReferenceException();
9
10 var tcs = new TaskCompletionSource<IRestResponse>();
11
12 @this.ExecuteAsync(request, (response) =>
13 {
14 if (response.ErrorException != null)
15 tcs.TrySetException(response.ErrorException);
16 else
17 tcs.TrySetResult(response);
18 });
19
20 return tcs.Task;
21 }
22 }
23}

This will add a new function to the RestSharp client type called ExecutreTaskAsync. Inside the method it will call the ExecuteAsync function as you normally would, but has also implemented returning a Task and setting it's results when its complete.

To use the function would be as follows

1var client = new RestClient("http://www.YOUR SITE.com/api/");
2var request = new RestRequest("Products", Method.GET);
3var response = await client.ExecuteTaskAsync(request);

Using Microsoft pubCenter with AdDuplex

Not everyone realises but when you put an ad control such as Microsofts pubCenter control in your app, it doesn't mean that every single user will see an ad every single time. Microsoft and any other ad provided has a limited number of ad's to show based on the number they've sold. So sometimes there just isn't an ad to show. This is referred to as fill rate, usually expresses as a percentage this tells you what percentage of requests to show an ad actually resulted in an ad being shown.

Not having 100% fill rates causes 2 issues. Firstly your not maximising your revenue as people are using your app but not seeing an ad. Secondly, unless you've designed for it, there's potentially an odd looking blank space in your app where an ad is meant to go.

There is a solution though, when an ad isn't received by the ad control it does at least fire an event to inform you that this has happened. You can either then do something to update your UI or show something else in its place like another ad control.

The second ad control I am using is AdDuplex. Unlike a traditional ad, this isn't going to pay me out any money. Instead its specifically for Windows Phone developers to promote there apps and works on a basis that if you show an ad, your ad will get shown on another app. For every 10 ads your app shows, 8 of yours will be shown on other apps. The remaining 2 ad spaces are used by AdDuplex to make there money. The benefit of this system is there is a 100% fill rate, so your ad space is used to its full potential.

Enough talk, show me the code

In the xaml mark-up I have a MS pubCenter ad control and an AdDuplex control along with the properties set to make them fit the layout of my page. Nothing special over what you would normally do here, other than the fact there's 2 controls on top of each other and the AdDuplex one is collapsed.

1<UI:AdControl Name="MSAdControl" ApplicationId="YOUR APP ID"
2 AdUnitId="YOUR AD UNIT ID" HorizontalAlignment="Left"
3 Height="80" VerticalAlignment="Top" Width="480" BorderThickness="0" Grid.Row="0" />
4<adduplex:AdControl x:Name="AdDuplexAdControl"
5 AppId="YOUR APP ID" Visibility="Collapsed" />

In the code behind I'm setting up 2 event handlers on the MS pubCenter control for error occurred and ad refreshed.

The error occurred even will get fired when an Ad isn't loaded. In that instance I need to show the AdDuplex control. Having an event on the ad refreshed control also ensures that if an ad ever does manage to display the control will be shown again.

1public MainPage()
2: base()
3{
4InitializeComponent();
5MSAdControl.ErrorOccurred += MSAdControl_ErrorOccurred;
6MSAdControl.AdRefreshed += new EventHandler(MSAdControl_NewAd);
7}
8
9void MSAdControl_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
10{
11System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
12{
13MSAdControl.Visibility = Visibility.Collapsed;
14AdDuplexAdControl.Visibility = Visibility.Visible;
15});
16}
17
18void MSAdControl_NewAd(object sender, EventArgs e)
19{
20System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
21{
22AdDuplexAdControl.Visibility = Visibility.Collapsed;
23MSAdControl.Visibility = Visibility.Visible;
24});
25}
Introducing Training Buddy for Windows 8.1

Introducing Training Buddy for Windows 8.1

3 years ago I created Training Buddy for Windows Phone 7 purely because I was switching from iPhone and wanted to have decent sports tracker. As it was a new platform I figured I'd make it.

Since then the app has become more popular than I imagined. Earlier this year it was updated with a fresh new look and to take advantage of new features in Windows Phone 8 such as new live tile sizes and Nokia Maps. Training Buddy Free (the free reduced functionality version) was also updated to include support for Training Buddy Live so activities can be viewed and shared on the Training Buddy website.

The app has also received some amazing feedback. My favourite of which Jason wrote in the UK app store for Training Buddy Free:

Now 3 years on with Windows 8.1 launched I felt it was time Training Buddy got its own Windows 8 app allowing its users to view all their activity data on their desktop and tablet. This has always been possible through the website since the launch of Training Buddy Live, but an app opens the door to be more platform specific and easier to use offline without an internet connection and then sync at a later date. It also happens that I'm a big fan of apps over websites.

This is just the start though, over the next few months more and more features will be added, just as the phone app has continued to evolve over the last 3 years.

Download Training Buddy for Windows Phone 8.1

IIS Where are my log files?

This is one of those things that once you know is very very simple, but finding out can be very very annoying.

IIS by default will store a log file for each site that it runs. This gives you valuable details on each request to the site that can help when errors are being reported by users.

When you go searching for them your initial thought may be to go to IIS and look at the site you want the files for. There you will see an item called logging. Excellent you think, this will tell you all you need to know.

There's even a button saying "View Log File...", but once you click it you realise things aren't so simple. The link and folder path on the logging page both take you to a folder, containing more folders. In those folders are the logs, but there's a folder for each site in IIS and they've all got a weird name. How do you know which folder has the log files for the site you want?

Back on the IIS logging screen there's nothing to say which folder the files will be in. There isn't any indication anywhere.

The answer however is very easy. Each folder has the Site ID in its name. You can find the Site ID for your site in IIS either by looking at the sites list

or clicking on a site and clicking advanced settings