From e8c5a84c1bbc5d92d1cbb42c42c3912495548062 Mon Sep 17 00:00:00 2001 From: maurok Date: Fri, 19 May 2017 17:17:08 -0300 Subject: [PATCH 1/5] # [ImageCaption] Updating SDK 3.8.0 --- .../Controllers/MessagesController.cs | 2 +- .../ImageCaption.csproj | 20 +++++++++---------- CSharp/intelligence-ImageCaption/README.md | 10 ++++++++-- .../intelligence-ImageCaption/packages.config | 6 +++--- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs index c49f934018..46d52c7036 100644 --- a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs +++ b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs @@ -42,7 +42,7 @@ public async Task Post([FromBody]Activity activity) } catch (Exception e) { - message = "Oops! Something went wrong. Try again later."; + message = "Oops! Something went wrong. Try again later (hint: also check your APIKEY at web.config)."; Trace.TraceError(e.ToString()); } diff --git a/CSharp/intelligence-ImageCaption/ImageCaption.csproj b/CSharp/intelligence-ImageCaption/ImageCaption.csproj index be82c39166..70f29b350f 100644 --- a/CSharp/intelligence-ImageCaption/ImageCaption.csproj +++ b/CSharp/intelligence-ImageCaption/ImageCaption.csproj @@ -50,21 +50,21 @@ packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.Autofac.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.Autofac.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Connector.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Connector.dll True - - packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.3.308261200\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll + + packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll True @@ -84,8 +84,8 @@ True - - packages\System.IdentityModel.Tokens.Jwt.4.0.3.308261200\lib\net45\System.IdentityModel.Tokens.Jwt.dll + + packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll True diff --git a/CSharp/intelligence-ImageCaption/README.md b/CSharp/intelligence-ImageCaption/README.md index d47aad8bd6..c2145f4dc8 100644 --- a/CSharp/intelligence-ImageCaption/README.md +++ b/CSharp/intelligence-ImageCaption/README.md @@ -14,10 +14,16 @@ The minimum prerequisites to run this sample are: * The Bot Framework Emulator. To install the Bot Framework Emulator, download it from [here](https://emulator.botframework.com/). Please refer to [this documentation article](https://github.com/microsoft/botframework-emulator/wiki/Getting-Started) to know more about the Bot Framework Emulator. * This sample currently uses a free trial Microsoft Cognitive service key with limited QPS. Please subscribe to Vision API services [here](https://www.microsoft.com/cognitive-services/en-us/subscriptions) and update the `MicrosoftVisionApiKey` key in [Web.config](Web.config) file to try it out further. +````XML + + + +```` + ### Code Highlights Microsoft Computer Vision API provides a number of methods that allows you to analyze an image. Check out [IVisionServiceClient.cs](https://github.com/Microsoft/Cognitive-Vision-Windows/blob/master/ClientLibrary/IVisionServiceClient.cs) for a complete reference of the methods available. In this sample we are using `AnalyzeImageAsync(string url, ...)` and `AnalyzeImageAsync(Stream imageStream, ...)` to analyze an image from a URL and stream respectively. The `AnalyzeImageAsync` method returns [AnalysisResult.cs](https://github.com/Microsoft/Cognitive-Vision-Windows/blob/master/ClientLibrary/Contract/AnalysisResult.cs) class which contains rich information about the image: -```C# +````C# namespace Microsoft.ProjectOxford.Vision.Contract { public class AnalysisResult @@ -35,7 +41,7 @@ namespace Microsoft.ProjectOxford.Vision.Contract public Tag[] Tags { get; set; } } } -``` +```` In this sample we are using the API to get the image description and send it back to the user. Check out the use of the `MicrosoftCognitiveCaptionService.GetCaptionAsync()` method in the [MicrosoftCognitiveCaptionService.cs](Services/MicrosoftCognitiveCaptionService.cs) class. diff --git a/CSharp/intelligence-ImageCaption/packages.config b/CSharp/intelligence-ImageCaption/packages.config index 874bce462f..07fa5b06a7 100644 --- a/CSharp/intelligence-ImageCaption/packages.config +++ b/CSharp/intelligence-ImageCaption/packages.config @@ -6,11 +6,11 @@ - - + + - + \ No newline at end of file From 25b0194d4ee2d5714ceeaf583516df8df417a0ef Mon Sep 17 00:00:00 2001 From: maurok Date: Fri, 19 May 2017 17:17:38 -0300 Subject: [PATCH 2/5] # Updating hint message --- .../intelligence-ImageCaption/Controllers/MessagesController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs index 46d52c7036..532312c313 100644 --- a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs +++ b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs @@ -42,7 +42,7 @@ public async Task Post([FromBody]Activity activity) } catch (Exception e) { - message = "Oops! Something went wrong. Try again later (hint: also check your APIKEY at web.config)."; + message = "Oops! Something went wrong. Try again later (hint: check your APIKEY at web.config)."; Trace.TraceError(e.ToString()); } From 8af04b24ed79711c889678c0bff5d1e6df6e1c3f Mon Sep 17 00:00:00 2001 From: maurok Date: Fri, 19 May 2017 17:19:16 -0300 Subject: [PATCH 3/5] # Adding hint message only if clientexception contains 'access denied' (code is zero) --- .../Controllers/MessagesController.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs index 532312c313..3d01ea5ae7 100644 --- a/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs +++ b/CSharp/intelligence-ImageCaption/Controllers/MessagesController.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using System.Web.Http; using Microsoft.Bot.Connector; + using Microsoft.ProjectOxford.Vision; using Services; [BotAuthentication] @@ -42,7 +43,11 @@ public async Task Post([FromBody]Activity activity) } catch (Exception e) { - message = "Oops! Something went wrong. Try again later (hint: check your APIKEY at web.config)."; + message = "Oops! Something went wrong. Try again later"; + if (e is ClientException && (e as ClientException).Error.Message.ToLowerInvariant().Contains("access denied")) + { + message += " (access denied - hint: check your APIKEY at web.config)."; + } Trace.TraceError(e.ToString()); } From 410473320f8defd7ad95f0b87dccae73573cb26f Mon Sep 17 00:00:00 2001 From: maurok Date: Fri, 19 May 2017 17:23:18 -0300 Subject: [PATCH 4/5] # [STT] Using version 3.8.0 --- .../Controllers/MessagesController.cs | 14 ++++++++++++- CSharp/intelligence-SpeechToText/README.md | 6 ++++++ .../Services/Authentication.cs | 7 ++++++- .../MicrosoftCognitiveSpeechService.cs | 6 ++++++ .../SpeechToText.csproj | 20 +++++++++---------- .../intelligence-SpeechToText/packages.config | 6 +++--- 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs b/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs index 2a36f15756..743d2bbd2e 100644 --- a/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs +++ b/CSharp/intelligence-SpeechToText/Controllers/MessagesController.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; + using System.Web; using System.Web.Http; using Microsoft.Bot.Connector; using Services; @@ -44,7 +45,18 @@ public async Task Post([FromBody]Activity activity) } catch (Exception e) { - message = "Oops! Something went wrong. Try again later."; + message = "Oops! Something went wrong. Try again later"; + if (e is HttpException) + { + var httpCode = (e as HttpException).GetHttpCode(); + if (httpCode == 401 || httpCode == 403) + { + message += $" [{e.Message} - hint: check your API KEY at web.config]"; + } else if (httpCode == 408) + { + message += $" [{e.Message} - hint: try send an audio shorter than 15 segs]"; + } + } Trace.TraceError(e.ToString()); } diff --git a/CSharp/intelligence-SpeechToText/README.md b/CSharp/intelligence-SpeechToText/README.md index 0fc8af0f15..82348d2ebe 100644 --- a/CSharp/intelligence-SpeechToText/README.md +++ b/CSharp/intelligence-SpeechToText/README.md @@ -15,6 +15,12 @@ The minimum prerequisites to run this sample are: * **[Recommended]** Visual Studio Code for IntelliSense and debugging, download it from [here](https://code.visualstudio.com/) for free. * This sample currently uses a free trial Microsoft Cognitive service key with limited QPS. Please subscribe to Bing Speech Api services [here](https://www.microsoft.com/cognitive-services/en-us/subscriptions) and update the `MicrosoftSpeechApiKey` key in key in [Web.config](Web.config) file to try it out further. +````XML + + + +```` + ### Usage Attach an audio file (wav format). diff --git a/CSharp/intelligence-SpeechToText/Services/Authentication.cs b/CSharp/intelligence-SpeechToText/Services/Authentication.cs index 7e339aefa2..ddb61dddc4 100644 --- a/CSharp/intelligence-SpeechToText/Services/Authentication.cs +++ b/CSharp/intelligence-SpeechToText/Services/Authentication.cs @@ -3,6 +3,7 @@ using System; using System.Net.Http; using System.Threading; + using System.Web; using System.Web.Configuration; public sealed class Authentication @@ -59,8 +60,12 @@ private static string GetNewToken() using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey); - + var response = client.PostAsync("https://api.cognitive.microsoft.com/sts/v1.0/issueToken", null).Result; + if (response.StatusCode != System.Net.HttpStatusCode.OK) + { + throw new HttpException((int)response.StatusCode, $"({response.StatusCode}) {response.ReasonPhrase}"); + } return response.Content.ReadAsStringAsync().Result; } diff --git a/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs b/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs index 9fab8bea8f..e28f327be4 100644 --- a/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs +++ b/CSharp/intelligence-SpeechToText/Services/MicrosoftCognitiveSpeechService.cs @@ -4,6 +4,7 @@ using System.IO; using System.Net.Http; using System.Threading.Tasks; + using System.Web; using Newtonsoft.Json; public class MicrosoftCognitiveSpeechService @@ -27,6 +28,11 @@ public async Task GetTextFromAudioAsync(Stream audiostream) binaryContent.Headers.TryAddWithoutValidation("content-type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000"); var response = await client.PostAsync(requestUri, binaryContent); + if (response.StatusCode != System.Net.HttpStatusCode.OK) + { + throw new HttpException((int)response.StatusCode, $"({response.StatusCode}) {response.ReasonPhrase}"); + } + var responseString = await response.Content.ReadAsStringAsync(); try { diff --git a/CSharp/intelligence-SpeechToText/SpeechToText.csproj b/CSharp/intelligence-SpeechToText/SpeechToText.csproj index 4fb5aa6940..f595b4c60a 100644 --- a/CSharp/intelligence-SpeechToText/SpeechToText.csproj +++ b/CSharp/intelligence-SpeechToText/SpeechToText.csproj @@ -50,21 +50,21 @@ packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.Autofac.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.Autofac.dll True - - packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Connector.dll + + packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Connector.dll True - - packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.3.308261200\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll + + packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll True @@ -80,8 +80,8 @@ True - - packages\System.IdentityModel.Tokens.Jwt.4.0.3.308261200\lib\net45\System.IdentityModel.Tokens.Jwt.dll + + packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll True diff --git a/CSharp/intelligence-SpeechToText/packages.config b/CSharp/intelligence-SpeechToText/packages.config index 0d7029d0f0..c17846aff5 100644 --- a/CSharp/intelligence-SpeechToText/packages.config +++ b/CSharp/intelligence-SpeechToText/packages.config @@ -6,10 +6,10 @@ - - + + - + \ No newline at end of file From 34fc91f35dfbad56f8e1a662d036b3c6fd8355c0 Mon Sep 17 00:00:00 2001 From: Pablo Costantini Date: Tue, 13 Jun 2017 16:50:17 -0300 Subject: [PATCH 5/5] Replace ContainerBuilder.Update with Conversation.UpdateContainer --- CSharp/core-CustomState/Global.asax.cs | 18 ++++++------ .../core-GlobalMessageHandlers/Global.asax.cs | 12 ++++---- CSharp/core-Middleware/Global.asax.cs | 7 +++-- .../public-TestBot/Global.asax.cs | 27 +++++++++-------- .../demo-Search/JobListingBot/Global.asax.cs | 29 +++++++++---------- .../demo-Search/RealEstateBot/Global.asax.cs | 29 +++++++++---------- 6 files changed, 59 insertions(+), 63 deletions(-) diff --git a/CSharp/core-CustomState/Global.asax.cs b/CSharp/core-CustomState/Global.asax.cs index fc6e7d9910..f44451214d 100644 --- a/CSharp/core-CustomState/Global.asax.cs +++ b/CSharp/core-CustomState/Global.asax.cs @@ -17,17 +17,17 @@ protected void Application_Start() Uri docDbServiceEndpoint = new Uri(ConfigurationManager.AppSettings["DocumentDbServiceEndpoint"]); string docDbEmulatorKey = ConfigurationManager.AppSettings["DocumentDbAuthKey"]; - var builder = new ContainerBuilder(); + Conversation.UpdateContainer(builder => + { + builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly())); - builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly())); + var store = new DocumentDbBotDataStore(docDbServiceEndpoint, docDbEmulatorKey); + builder.Register(c => store) + .Keyed>(AzureModule.Key_DataStore) + .AsSelf() + .SingleInstance(); - var store = new DocumentDbBotDataStore(docDbServiceEndpoint, docDbEmulatorKey); - builder.Register(c => store) - .Keyed>(AzureModule.Key_DataStore) - .AsSelf() - .SingleInstance(); - - builder.Update(Conversation.Container); + }); GlobalConfiguration.Configure(WebApiConfig.Register); } diff --git a/CSharp/core-GlobalMessageHandlers/Global.asax.cs b/CSharp/core-GlobalMessageHandlers/Global.asax.cs index 1a0e02e037..138a0e5c6d 100644 --- a/CSharp/core-GlobalMessageHandlers/Global.asax.cs +++ b/CSharp/core-GlobalMessageHandlers/Global.asax.cs @@ -16,13 +16,11 @@ protected void Application_Start() private void RegisterBotModules() { - var builder = new ContainerBuilder(); - - builder.RegisterModule(new ReflectionSurrogateModule()); - - builder.RegisterModule(); - - builder.Update(Conversation.Container); + Conversation.UpdateContainer(builder => + { + builder.RegisterModule(new ReflectionSurrogateModule()); + builder.RegisterModule(); + }); } } } diff --git a/CSharp/core-Middleware/Global.asax.cs b/CSharp/core-Middleware/Global.asax.cs index 946ef8d110..c15750cd38 100644 --- a/CSharp/core-Middleware/Global.asax.cs +++ b/CSharp/core-Middleware/Global.asax.cs @@ -8,9 +8,10 @@ public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { - var builder = new ContainerBuilder(); - builder.RegisterType().AsImplementedInterfaces().InstancePerDependency(); - builder.Update(Conversation.Container); + Conversation.UpdateContainer(builder => + { + builder.RegisterType().AsImplementedInterfaces().InstancePerDependency(); + }); GlobalConfiguration.Configure(WebApiConfig.Register); } diff --git a/CSharp/demo-CardsAttachments/public-TestBot/Global.asax.cs b/CSharp/demo-CardsAttachments/public-TestBot/Global.asax.cs index e7d45fc5d3..0c5a7f99fb 100644 --- a/CSharp/demo-CardsAttachments/public-TestBot/Global.asax.cs +++ b/CSharp/demo-CardsAttachments/public-TestBot/Global.asax.cs @@ -21,23 +21,22 @@ protected void Application_Start() private void RegisterBotDependencies() { - var builder = new ContainerBuilder(); - - builder - .Register(c => new ActivityLogger(c.Resolve())) - .As() - .InstancePerLifetimeScope(); - - foreach (var commandType in CommandsHelper.GetRegistrableTypes()) + Conversation.UpdateContainer(builder => { builder - .RegisterType(commandType) - .Keyed(commandType.Name, commandType) - .AsImplementedInterfaces() - .InstancePerMatchingLifetimeScope(DialogModule.LifetimeScopeTag); - } + .Register(c => new ActivityLogger(c.Resolve())) + .As() + .InstancePerLifetimeScope(); - builder.Update(Conversation.Container); + foreach (var commandType in CommandsHelper.GetRegistrableTypes()) + { + builder + .RegisterType(commandType) + .Keyed(commandType.Name, commandType) + .AsImplementedInterfaces() + .InstancePerMatchingLifetimeScope(DialogModule.LifetimeScopeTag); + } + }); } } } \ No newline at end of file diff --git a/CSharp/demo-Search/JobListingBot/Global.asax.cs b/CSharp/demo-Search/JobListingBot/Global.asax.cs index 99323d63fe..abccc623a9 100644 --- a/CSharp/demo-Search/JobListingBot/Global.asax.cs +++ b/CSharp/demo-Search/JobListingBot/Global.asax.cs @@ -14,23 +14,22 @@ public class WebApiApplication : System.Web.HttpApplication { protected void Application_Start() { - ContainerBuilder builder = new ContainerBuilder(); + Conversation.UpdateContainer(builder => + { + builder.RegisterType() + .As>() + .InstancePerDependency(); - builder.RegisterType() - .As>() - .InstancePerDependency(); + builder.RegisterType() + .Keyed>(FiberModule.Key_DoNotSerialize) + .AsImplementedInterfaces() + .SingleInstance(); - builder.RegisterType() - .Keyed>(FiberModule.Key_DoNotSerialize) - .AsImplementedInterfaces() - .SingleInstance(); - - builder.RegisterType() - .Keyed(FiberModule.Key_DoNotSerialize) - .AsImplementedInterfaces() - .SingleInstance(); - - builder.Update(Conversation.Container); + builder.RegisterType() + .Keyed(FiberModule.Key_DoNotSerialize) + .AsImplementedInterfaces() + .SingleInstance(); + }); GlobalConfiguration.Configure(WebApiConfig.Register); } diff --git a/CSharp/demo-Search/RealEstateBot/Global.asax.cs b/CSharp/demo-Search/RealEstateBot/Global.asax.cs index c52e29096a..176d3d9aea 100644 --- a/CSharp/demo-Search/RealEstateBot/Global.asax.cs +++ b/CSharp/demo-Search/RealEstateBot/Global.asax.cs @@ -15,23 +15,22 @@ public class WebApiApplication : HttpApplication { protected void Application_Start() { - ContainerBuilder builder = new ContainerBuilder(); + Conversation.UpdateContainer(builder => + { + builder.RegisterType() + .As>() + .InstancePerDependency(); - builder.RegisterType() - .As>() - .InstancePerDependency(); + builder.RegisterType() + .Keyed>(FiberModule.Key_DoNotSerialize) + .AsImplementedInterfaces() + .SingleInstance(); - builder.RegisterType() - .Keyed>(FiberModule.Key_DoNotSerialize) - .AsImplementedInterfaces() - .SingleInstance(); - - builder.RegisterType() - .Keyed(FiberModule.Key_DoNotSerialize) - .AsImplementedInterfaces() - .SingleInstance(); - - builder.Update(Conversation.Container); + builder.RegisterType() + .Keyed(FiberModule.Key_DoNotSerialize) + .AsImplementedInterfaces() + .SingleInstance(); + }); GlobalConfiguration.Configure(WebApiConfig.Register); }