Skip to content

Instantly share code, notes, and snippets.

@carlosfigueira
Created August 13, 2014 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosfigueira/9582c08851d116f5a426 to your computer and use it in GitHub Desktop.
Save carlosfigueira/9582c08851d116f5a426 to your computer and use it in GitHub Desktop.
Files with solution for problem in StackOverflow question 25246280 (http://stackoverflow.com/q/25246280)
<Page
x:Class="StackOverflow_25246280_App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StackOverflow_25246280_App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="btnStart" Content="Start (app 1)" FontSize="30" Margin="10"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Click="btnStart_Click" />
<TextBox AcceptsReturn="True" Name="txtDebug" Margin="10" Grid.Row="1" />
</Grid>
</Page>
using System;
using Microsoft.WindowsAzure.MobileServices;
using SO_25246280_Portable;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace StackOverflow_25246280_App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
var client = new MobileServiceClient(
"https://SERVICE_NAME_HERE.azure-mobile.net/",
"SERVICE_KEY_HERE",
new AppSpecificTableNamesHandler("App1")
);
var table = client.GetTable<MyType>();
var item = new MyType { Name = "John Doe", Age = 33 };
await table.InsertAsync(item);
AddToDebug("Inserted item with id = {0}", item.Id);
item.Age = item.Age + 1;
await table.UpdateAsync(item);
AddToDebug("Updated item, age now = {0}", item.Age);
}
catch (Exception ex)
{
AddToDebug("Error: {0}", ex);
}
}
void AddToDebug(string text, params object[] args)
{
if (args != null && args.Length > 0) text = string.Format(text, args);
this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
}
}
}
<Page
x:Class="StackOverflow_25246280_App2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StackOverflow_25246280_App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="btnStart" Content="Start (app 2)" FontSize="30" Margin="10"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Click="btnStart_Click" />
<TextBox AcceptsReturn="True" Name="txtDebug" Margin="10" Grid.Row="1" />
</Grid>
</Page>
using System;
using Microsoft.WindowsAzure.MobileServices;
using SO_25246280_Portable;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace StackOverflow_25246280_App2
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
var client = new MobileServiceClient(
"https://SERVICE_NAME_HERE.azure-mobile.net/",
"SERVICE_KEY_HERE",
new AppSpecificTableNamesHandler("App2")
);
var table = client.GetTable<MyType>();
var item = new MyType { Name = "John Doe", Age = 33 };
await table.InsertAsync(item);
AddToDebug("Inserted item with id = {0}", item.Id);
item.Age = item.Age + 1;
await table.UpdateAsync(item);
AddToDebug("Updated item, age now = {0}", item.Age);
}
catch (Exception ex)
{
AddToDebug("Error: {0}", ex);
}
}
void AddToDebug(string text, params object[] args)
{
if (args != null && args.Length > 0) text = string.Format(text, args);
this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
}
}
}
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace SO_25246280_Portable
{
public class AppSpecificTableNamesHandler : DelegatingHandler
{
public const string TablePrefix = "MyType";
private const string TablesPathPrefix = "/tables/";
private string tableSuffix;
public AppSpecificTableNamesHandler(string tableSuffix)
{
this.tableSuffix = tableSuffix;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
UriBuilder uriBuilder = new UriBuilder(request.RequestUri);
string path = uriBuilder.Path;
if (path.StartsWith(TablesPathPrefix + TablePrefix))
{
path = TablesPathPrefix + TablePrefix +
this.tableSuffix + path.Substring(TablesPathPrefix.Length + TablePrefix.Length);
uriBuilder.Path = path;
request.RequestUri = uriBuilder.Uri;
}
return base.SendAsync(request, cancellationToken);
}
}
}
using Microsoft.WindowsAzure.MobileServices;
using Newtonsoft.Json;
namespace SO_25246280_Portable
{
[DataTable(AppSpecificTableNamesHandler.TablePrefix)]
public class MyType
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment