From a5b8480686c6a73b4fd4ae89b0269e91ca1cea47 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 1 Sep 2017 13:57:09 -0700 Subject: [PATCH] Get/Set API endpoint address from/to isolated storage --- .../SampleUserControl.xaml.cs | 6 ++ .../SubscriptionKeyPage.xaml | 33 ++++++-- .../SubscriptionKeyPage.xaml.cs | 82 +++++++++++++------ 3 files changed, 86 insertions(+), 35 deletions(-) diff --git a/SampleUserControlLibrary/SampleUserControl.xaml.cs b/SampleUserControlLibrary/SampleUserControl.xaml.cs index 53d4160..b42d7c4 100644 --- a/SampleUserControlLibrary/SampleUserControl.xaml.cs +++ b/SampleUserControlLibrary/SampleUserControl.xaml.cs @@ -98,6 +98,12 @@ public string SubscriptionKey set; } + public string EndpointAddress + { + get; + set; + } + public SampleScenarios() { InitializeComponent(); diff --git a/SampleUserControlLibrary/SubscriptionKeyPage.xaml b/SampleUserControlLibrary/SubscriptionKeyPage.xaml index ff16284..960a8c2 100644 --- a/SampleUserControlLibrary/SubscriptionKeyPage.xaml +++ b/SampleUserControlLibrary/SubscriptionKeyPage.xaml @@ -28,17 +28,32 @@ - - To use the service, you need to ensure that you have right subscription key. + + + + + + + + + + + + + To use the service, you need to ensure that you have right subscription key. Please note that each service (Face, Emotion, Speech, etc) has its own subscription key. + You must specify the endpoint that matches the API key. If you do not have key yet, please use the link to get a key first, then paste the key into the textbox below. - - - + + diff --git a/SampleUserControlLibrary/SubscriptionKeyPage.xaml.cs b/SampleUserControlLibrary/SubscriptionKeyPage.xaml.cs index 8ba2fde..1d74785 100644 --- a/SampleUserControlLibrary/SubscriptionKeyPage.xaml.cs +++ b/SampleUserControlLibrary/SubscriptionKeyPage.xaml.cs @@ -31,6 +31,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // +using System; using System.ComponentModel; using System.IO; using System.IO.IsolatedStorage; @@ -47,8 +48,10 @@ public partial class SubscriptionKeyPage : Page, INotifyPropertyChanged { private readonly string _isolatedStorageSubscriptionKeyFileName = "Subscription.txt"; private readonly string _defaultSubscriptionKeyPromptMessage = "Paste your subscription key here to start"; + private readonly string _defaultEndpointAddressPromptMessage = "Paste your endpoint address here to start"; private static string s_subscriptionKey; + private static string s_endpointAddress; private SampleScenarios _sampleScenarios; public SubscriptionKeyPage(SampleScenarios sampleScenarios) @@ -57,7 +60,9 @@ public SubscriptionKeyPage(SampleScenarios sampleScenarios) _sampleScenarios = sampleScenarios; DataContext = this; - SubscriptionKey = GetSubscriptionKeyFromIsolatedStorage(); + var info = GetSubscriptionInfoFromIsolatedStorage(); + SubscriptionKey = info.Item1; + EndpointAddress = info.Item2; } /// @@ -78,6 +83,24 @@ public string SubscriptionKey } } + /// + /// Gets or sets subscription key + /// + public string EndpointAddress + { + get + { + return s_endpointAddress; + } + + set + { + s_endpointAddress = value; + OnPropertyChanged(); + _sampleScenarios.EndpointAddress = s_endpointAddress; + } + } + /// /// Implement INotifyPropertyChanged interface /// @@ -101,10 +124,11 @@ private void OnPropertyChanged([CallerMemberName]string caller = null) /// /// Gets the subscription key from isolated storage. /// - /// - private string GetSubscriptionKeyFromIsolatedStorage() + /// Tuple of (subscription-key, endpoint-address) + private Tuple GetSubscriptionInfoFromIsolatedStorage() { string subscriptionKey = null; + string endpointAddress = null; using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) { @@ -115,26 +139,35 @@ private string GetSubscriptionKeyFromIsolatedStorage() using (var reader = new StreamReader(iStream)) { subscriptionKey = reader.ReadLine(); + if (!reader.EndOfStream) + { + endpointAddress = reader.ReadLine(); + } } } } catch (FileNotFoundException) { - subscriptionKey = null; + // Override below } } if (string.IsNullOrEmpty(subscriptionKey)) { subscriptionKey = _defaultSubscriptionKeyPromptMessage; } - return subscriptionKey; + if (string.IsNullOrEmpty(endpointAddress)) + { + endpointAddress = _defaultEndpointAddressPromptMessage; + } + return new Tuple(subscriptionKey, endpointAddress); } /// /// Saves the subscription key to isolated storage. /// /// The subscription key. - private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey) + /// Endpoint address + private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey, string endpointAddress) { using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null)) { @@ -143,48 +176,45 @@ private void SaveSubscriptionKeyToIsolatedStorage(string subscriptionKey) using (var writer = new StreamWriter(oStream)) { writer.WriteLine(subscriptionKey); + writer.WriteLine(endpointAddress); } } } } - /// - /// Handles the Click event of the subscription key save button. - /// - /// The source of the event. - /// The instance containing the event data. - private void SaveKey_Click(object sender, RoutedEventArgs e) + private void GetKeyButton_Click(object sender, RoutedEventArgs e) + { + System.Diagnostics.Process.Start("https://www.microsoft.com/cognitive-services/en-us/sign-up"); + } + + private void Save_Click(object sender, RoutedEventArgs e) { try { - SaveSubscriptionKeyToIsolatedStorage(SubscriptionKey); - MessageBox.Show("Subscription key is saved in your disk.\nYou do not need to paste the key next time.", "Subscription Key"); + SaveSubscriptionKeyToIsolatedStorage(SubscriptionKey, EndpointAddress); + MessageBox.Show("Subscription info is saved in your disk.\nYou do not need to paste the key next time.", "Subscription Info"); } catch (System.Exception exception) { - MessageBox.Show("Fail to save subscription key. Error message: " + exception.Message, - "Subscription Key", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show("Fail to save subscription info. Error message: " + exception.Message, + "Subscription Info", MessageBoxButton.OK, MessageBoxImage.Error); } } - private void DeleteKey_Click(object sender, RoutedEventArgs e) + private void Delete_Click(object sender, RoutedEventArgs e) { try { SubscriptionKey = _defaultSubscriptionKeyPromptMessage; - SaveSubscriptionKeyToIsolatedStorage(""); - MessageBox.Show("Subscription key is deleted from your disk.", "Subscription Key"); + EndpointAddress = _defaultEndpointAddressPromptMessage; + SaveSubscriptionKeyToIsolatedStorage("", ""); + MessageBox.Show("Subscription info is deleted from your disk.", "Subscription Info"); } catch (System.Exception exception) { - MessageBox.Show("Fail to delete subscription key. Error message: " + exception.Message, - "Subscription Key", MessageBoxButton.OK, MessageBoxImage.Error); + MessageBox.Show("Fail to delete subscription info. Error message: " + exception.Message, + "Subscription Info", MessageBoxButton.OK, MessageBoxImage.Error); } } - - private void GetKeyButton_Click(object sender, RoutedEventArgs e) - { - System.Diagnostics.Process.Start("https://www.microsoft.com/cognitive-services/en-us/sign-up"); - } } }