Skip to content

Commit

Permalink
Enable ASP.NET CORE usage with BotBuilder
Browse files Browse the repository at this point in the history
 - Fix simple sandwich bot and add TODO comment
 - Improve Service resolution for multi-platform support
 - Remove remaining #IF NET45 compiler conditionals
 - Remove #IF NET45 conditionals merge from upstream
  • Loading branch information
andreesteve committed Feb 24, 2017
1 parent aa72e0f commit f2bc432
Show file tree
Hide file tree
Showing 29 changed files with 791 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
using Microsoft.Bot.Connector;
using Microsoft.Bot.Builder.Autofac.Base;
using System.Configuration;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

namespace Microsoft.Bot.Builder.Dialogs.Internals
{
Expand All @@ -67,32 +70,14 @@ public static ILifetimeScope BeginLifetimeScope(ILifetimeScope scope, IMessageAc
return inner;
}

// TODO andrees move this to its own file
private class BotConfiguration : IBotConfiguration
{
public string MicrosoftAppId
{
get
{
return ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppIdKey];
}
}

public string MicrosoftAppPassword
{
get
{
return ConfigurationManager.AppSettings[MicrosoftAppCredentials.MicrosoftAppPasswordKey];
}
}
}

protected override void Load(ContainerBuilder builder)
{
base.Load(builder);

builder.RegisterModule(new FiberModule<DialogTask>());

RegisterBotConnectorImplementation(builder);

// singleton components

builder
Expand Down Expand Up @@ -120,13 +105,9 @@ protected override void Load(ContainerBuilder builder)
.AsSelf()
.InstancePerMatchingLifetimeScope(LifetimeScopeTag);

builder.Register(c => new BotConfiguration())
.AsImplementedInterfaces()
.SingleInstance();

// components not marked as [Serializable]
builder
.RegisterType<MicrosoftAppCredentials>()
.Register(c => new MicrosoftAppCredentials(c.Resolve<ServiceProvider>(), null, null, null))
.AsSelf()
.SingleInstance();

Expand Down Expand Up @@ -384,6 +365,43 @@ protected override void Load(ContainerBuilder builder)
)
.InstancePerLifetimeScope();
}

/// <summary>
/// Registers (platform specific) bot connector implementations.
/// </summary>
/// <param name="builder">The builder used to register dependencies.</param>
private void RegisterBotConnectorImplementation(ContainerBuilder builder)
{
if (ServiceProvider.IsRegistered)
{
builder.Register<ServiceProvider>(c => ServiceProvider.Instance)
.AsSelf()
.SingleInstance();
}
else
{
// Finds the bot connector being used in this application domain
// that is not the common connector assembly
System.Reflection.Assembly connectorAssembly = AppDomain.CurrentDomain.
GetAssemblies()
.Where(a => a.FullName.StartsWith("Microsoft.Bot.Connector"))
.Where(a => a != typeof(BotData).Assembly)
.FirstOrDefault();

if (connectorAssembly != null)
{
// register the IServiceProvider instance provided in the connector with the Common connector
builder.RegisterAssemblyTypes(connectorAssembly)
.Where(t => t.IsAssignableTo<IServiceProvider>())
.AsImplementedInterfaces()
.SingleInstance();

builder.Register<ServiceProvider>(c => { ServiceProvider.RegisterServiceProvider(c.Resolve<IServiceProvider>()); return ServiceProvider.Instance; })
.AsSelf()
.SingleInstance();
}
}
}
}

public sealed class DialogModule_MakeRoot : Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,74 @@
<HintPath>..\..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.1.1.0\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.2.3.5\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Configuration" />
<Reference Include="System.Console, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Console.4.3.0\lib\net46\System.Console.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Globalization.Calendars, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Net.Sockets, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
Expand All @@ -89,6 +136,10 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.ReaderWriter, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Base\Extensions.cs" />
Expand Down
41 changes: 41 additions & 0 deletions CSharp/Library/Microsoft.Bot.Builder.Autofac/packages.config
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="3.5.2" targetFramework="net461" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.5" targetFramework="net461" />
<package id="Microsoft.Win32.Primitives" version="4.3.0" targetFramework="net461" />
<package id="NETStandard.Library" version="1.6.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net461" />
<package id="System.AppContext" version="4.3.0" targetFramework="net461" />
<package id="System.Collections" version="4.3.0" targetFramework="net461" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net461" />
<package id="System.Console" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net461" />
<package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net461" />
<package id="System.Globalization" version="4.3.0" targetFramework="net461" />
<package id="System.Globalization.Calendars" version="4.3.0" targetFramework="net461" />
<package id="System.IO" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Compression.ZipFile" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Linq" version="4.3.0" targetFramework="net461" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" />
<package id="System.ObjectModel" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Handles" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net461" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net461" />
<package id="System.Threading" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net461" />
<package id="System.Threading.Timer" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net461" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net461" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
#if NET45
using System.Diagnostics;
using System.Web;
#endif


namespace Microsoft.Bot.Connector
{
Expand Down Expand Up @@ -80,11 +76,8 @@ public BotAuthenticator(ICredentialProvider credentialProvider)
public static HttpResponseMessage GenerateUnauthorizedResponse(HttpRequestMessage request)
{
string host = request.RequestUri.DnsSafeHost;
#if NET45
var response = request.CreateResponse(HttpStatusCode.Unauthorized);
#else
var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
#endif

response.Headers.Add("WWW-Authenticate", string.Format("Bearer realm=\"{0}\"", host));
return response;
}
Expand All @@ -104,9 +97,7 @@ public void TrustServiceUrls(IdentityToken identityToken, IEnumerable<IActivity>
}
else
{
#if NET45
Trace.TraceWarning("No ServiceUrls added to trusted list");
#endif
ServiceProvider.Instance.CreateLogger().LogWarning("No ServiceUrls added to trusted list");
}
}
}
Expand Down Expand Up @@ -174,14 +165,6 @@ public void TrustServiceUrls(IdentityToken identityToken, IEnumerable<IActivity>

if (identity != null)
{
#if NET45
Thread.CurrentPrincipal = new ClaimsPrincipal(identity);

// Inside of ASP.NET this is required
if (HttpContext.Current != null)
HttpContext.Current.User = Thread.CurrentPrincipal;
#endif

return new IdentityToken(true, identity);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
namespace Microsoft.Bot.Connector
{
using System;
#if NET45
using System.IdentityModel.Tokens;
#else
using Microsoft.IdentityModel.Tokens;
#endif

/// <summary>
/// Configuration for JWT tokens
Expand Down

0 comments on commit f2bc432

Please sign in to comment.