Skip to content

Commit

Permalink
Merge pull request microsoft#131 from southworkscom/csharp-upgrade-sd…
Browse files Browse the repository at this point in the history
…k3.8.0

[C#] Update to BotBuilder v3.8.0
  • Loading branch information
willportnoy authored Jun 23, 2017
2 parents bc26d30 + 34fc91f commit 11347cc
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 94 deletions.
18 changes: 9 additions & 9 deletions CSharp/core-CustomState/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();

var store = new DocumentDbBotDataStore(docDbServiceEndpoint, docDbEmulatorKey);
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();

builder.Update(Conversation.Container);
});

GlobalConfiguration.Configure(WebApiConfig.Register);
}
Expand Down
12 changes: 5 additions & 7 deletions CSharp/core-GlobalMessageHandlers/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ protected void Application_Start()

private void RegisterBotModules()
{
var builder = new ContainerBuilder();

builder.RegisterModule(new ReflectionSurrogateModule());

builder.RegisterModule<GlobalMessageHandlersBotModule>();

builder.Update(Conversation.Container);
Conversation.UpdateContainer(builder =>
{
builder.RegisterModule(new ReflectionSurrogateModule());
builder.RegisterModule<GlobalMessageHandlersBotModule>();
});
}
}
}
7 changes: 4 additions & 3 deletions CSharp/core-Middleware/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterType<DebugActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
builder.Update(Conversation.Container);
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<DebugActivityLogger>().AsImplementedInterfaces().InstancePerDependency();
});

GlobalConfiguration.Configure(WebApiConfig.Register);
}
Expand Down
27 changes: 13 additions & 14 deletions CSharp/demo-CardsAttachments/public-TestBot/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@ protected void Application_Start()

private void RegisterBotDependencies()
{
var builder = new ContainerBuilder();

builder
.Register(c => new ActivityLogger(c.Resolve<IBotData>()))
.As<IActivityLogger>()
.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<IBotData>()))
.As<IActivityLogger>()
.InstancePerLifetimeScope();

builder.Update(Conversation.Container);
foreach (var commandType in CommandsHelper.GetRegistrableTypes())
{
builder
.RegisterType(commandType)
.Keyed(commandType.Name, commandType)
.AsImplementedInterfaces()
.InstancePerMatchingLifetimeScope(DialogModule.LifetimeScopeTag);
}
});
}
}
}
29 changes: 14 additions & 15 deletions CSharp/demo-Search/JobListingBot/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ContainerBuilder builder = new ContainerBuilder();
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<IntroDialog>()
.As<IDialog<object>>()
.InstancePerDependency();

builder.RegisterType<IntroDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
builder.RegisterType<JobsMapper>()
.Keyed<IMapper<DocumentSearchResult, GenericSearchResult>>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.RegisterType<JobsMapper>()
.Keyed<IMapper<DocumentSearchResult, GenericSearchResult>>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.RegisterType<AzureSearchClient>()
.Keyed<ISearchClient>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.Update(Conversation.Container);
builder.RegisterType<AzureSearchClient>()
.Keyed<ISearchClient>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();
});

GlobalConfiguration.Configure(WebApiConfig.Register);
}
Expand Down
29 changes: 14 additions & 15 deletions CSharp/demo-Search/RealEstateBot/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
ContainerBuilder builder = new ContainerBuilder();
Conversation.UpdateContainer(builder =>
{
builder.RegisterType<IntroDialog>()
.As<IDialog<object>>()
.InstancePerDependency();

builder.RegisterType<IntroDialog>()
.As<IDialog<object>>()
.InstancePerDependency();
builder.RegisterType<RealEstateMapper>()
.Keyed<IMapper<DocumentSearchResult, GenericSearchResult>>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.RegisterType<RealEstateMapper>()
.Keyed<IMapper<DocumentSearchResult, GenericSearchResult>>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.RegisterType<AzureSearchClient>()
.Keyed<ISearchClient>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();

builder.Update(Conversation.Container);
builder.RegisterType<AzureSearchClient>()
.Keyed<ISearchClient>(FiberModule.Key_DoNotSerialize)
.AsImplementedInterfaces()
.SingleInstance();
});

GlobalConfiguration.Configure(WebApiConfig.Register);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Connector;
using Microsoft.ProjectOxford.Vision;
using Services;

[BotAuthentication]
Expand Down Expand Up @@ -42,7 +43,11 @@ public async Task<HttpResponseMessage> 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 ClientException && (e as ClientException).Error.Message.ToLowerInvariant().Contains("access denied"))
{
message += " (access denied - hint: check your APIKEY at web.config).";
}

Trace.TraceError(e.ToString());
}
Expand Down
20 changes: 10 additions & 10 deletions CSharp/intelligence-ImageCaption/ImageCaption.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@
<HintPath>packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Bot.Builder, Version=3.5.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.dll</HintPath>
<Reference Include="Microsoft.Bot.Builder, Version=3.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Bot.Builder.Autofac, Version=3.5.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Builder.Autofac.dll</HintPath>
<Reference Include="Microsoft.Bot.Builder.Autofac, Version=3.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Builder.Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Bot.Connector, Version=3.5.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.5.3\lib\net46\Microsoft.Bot.Connector.dll</HintPath>
<Reference Include="Microsoft.Bot.Connector, Version=3.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bot.Builder.3.8.0\lib\net46\Microsoft.Bot.Connector.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.3.42, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.3.308261200\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ProjectOxford.Vision, Version=1.0.370.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand All @@ -84,8 +84,8 @@
<Private>True</Private>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.30826.1200, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\System.IdentityModel.Tokens.Jwt.4.0.3.308261200\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net" />
Expand Down
10 changes: 8 additions & 2 deletions CSharp/intelligence-ImageCaption/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<appSettings>
<add key="MicrosoftVisionApiKey" value="PUT-YOUR-OWN-API-KEY-HERE" />
</appSettings>
````

### 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
Expand All @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions CSharp/intelligence-ImageCaption/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
<package id="Microsoft.Bot.Builder" version="3.5.3" targetFramework="net46" />
<package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.3.308261200" targetFramework="net46" />
<package id="Microsoft.Bot.Builder" version="3.8.0" targetFramework="net46" />
<package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.4.403061554" targetFramework="net46" />
<package id="Microsoft.ProjectOxford.Vision" version="1.0.370" targetFramework="net46" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.4" targetFramework="net46" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.2.3" targetFramework="net46" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46" />
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.3.308261200" targetFramework="net46" />
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net46" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,18 @@ public async Task<HttpResponseMessage> 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());
}
Expand Down
6 changes: 6 additions & 0 deletions CSharp/intelligence-SpeechToText/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<appSettings>
<add key="MicrosoftSpeechApiKey" value="PUT-YOUR-OWN-API-KEY-HERE" />
</appSettings>
````

### Usage

Attach an audio file (wav format).
Expand Down
7 changes: 6 additions & 1 deletion CSharp/intelligence-SpeechToText/Services/Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Web;
using System.Web.Configuration;

public sealed class Authentication
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;

public class MicrosoftCognitiveSpeechService
Expand All @@ -27,6 +28,11 @@ public async Task<string> 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
{
Expand Down
Loading

0 comments on commit 11347cc

Please sign in to comment.