Merge pull request #852 from Microsoft/stevenic/turnState
Renamed TurnContext.Services to TurnContext.TurnState
This commit is contained in:
Коммит
fb65b0063c
|
@ -126,9 +126,9 @@ namespace Microsoft.Bot.Builder
|
|||
new Claim(AuthenticationConstants.AppIdClaim, botAppId),
|
||||
});
|
||||
|
||||
context.Services.Add<IIdentity>(BotIdentityKey, claimsIdentity);
|
||||
context.TurnState.Add<IIdentity>(BotIdentityKey, claimsIdentity);
|
||||
var connectorClient = await CreateConnectorClientAsync(reference.ServiceUrl, claimsIdentity, cancellationToken).ConfigureAwait(false);
|
||||
context.Services.Add(connectorClient);
|
||||
context.TurnState.Add(connectorClient);
|
||||
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
@ -195,10 +195,10 @@ namespace Microsoft.Bot.Builder
|
|||
|
||||
using (var context = new TurnContext(this, activity))
|
||||
{
|
||||
context.Services.Add<IIdentity>(BotIdentityKey, identity);
|
||||
context.TurnState.Add<IIdentity>(BotIdentityKey, identity);
|
||||
|
||||
var connectorClient = await CreateConnectorClientAsync(activity.ServiceUrl, identity, cancellationToken).ConfigureAwait(false);
|
||||
context.Services.Add(connectorClient);
|
||||
context.TurnState.Add(connectorClient);
|
||||
|
||||
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
|
@ -206,7 +206,7 @@ namespace Microsoft.Bot.Builder
|
|||
// the Bot will return a specific body and return code.
|
||||
if (activity.Type == ActivityTypes.Invoke)
|
||||
{
|
||||
Activity invokeResponse = context.Services.Get<Activity>(InvokeReponseKey);
|
||||
Activity invokeResponse = context.TurnState.Get<Activity>(InvokeReponseKey);
|
||||
if (invokeResponse == null)
|
||||
{
|
||||
// ToDo: Trace Here
|
||||
|
@ -275,7 +275,7 @@ namespace Microsoft.Bot.Builder
|
|||
}
|
||||
else if (activity.Type == ActivityTypesEx.InvokeResponse)
|
||||
{
|
||||
context.Services.Add(InvokeReponseKey, activity);
|
||||
context.TurnState.Add(InvokeReponseKey, activity);
|
||||
|
||||
// No need to create a response. One will be created below.
|
||||
}
|
||||
|
@ -285,12 +285,12 @@ namespace Microsoft.Bot.Builder
|
|||
}
|
||||
else if (!string.IsNullOrWhiteSpace(activity.ReplyToId))
|
||||
{
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
response = await connectorClient.Conversations.ReplyToActivityAsync(activity, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
response = await connectorClient.Conversations.SendToConversationAsync(activity, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
@ -329,7 +329,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// <seealso cref="ITurnContext.OnUpdateActivity(UpdateActivityHandler)"/>
|
||||
public override async Task<ResourceResponse> UpdateActivityAsync(ITurnContext context, Activity activity, CancellationToken cancellationToken)
|
||||
{
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
return await connectorClient.Conversations.UpdateActivityAsync(activity, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
@ -345,7 +345,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// <seealso cref="ITurnContext.OnDeleteActivity(DeleteActivityHandler)"/>
|
||||
public override async Task DeleteActivityAsync(ITurnContext context, ConversationReference reference, CancellationToken cancellationToken)
|
||||
{
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
await connectorClient.Conversations.DeleteActivityAsync(reference.Conversation.Id, reference.ActivityId, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
@ -369,7 +369,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException("BotFrameworkAdapter.deleteConversationMember(): missing conversation.id");
|
||||
}
|
||||
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
|
||||
string conversationId = context.Activity.Conversation.Id;
|
||||
|
||||
|
@ -401,7 +401,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException("BotFrameworkAdapter.GetActivityMembers(): missing conversation.id");
|
||||
}
|
||||
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
var conversationId = context.Activity.Conversation.Id;
|
||||
|
||||
IList<ChannelAccount> accounts = await connectorClient.Conversations.GetActivityMembersAsync(conversationId, activityId, cancellationToken).ConfigureAwait(false);
|
||||
|
@ -427,7 +427,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException("BotFrameworkAdapter.GetActivityMembers(): missing conversation.id");
|
||||
}
|
||||
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
var conversationId = context.Activity.Conversation.Id;
|
||||
|
||||
IList<ChannelAccount> accounts = await connectorClient.Conversations.GetConversationMembersAsync(conversationId, cancellationToken).ConfigureAwait(false);
|
||||
|
@ -484,7 +484,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// </remarks>
|
||||
public async Task<ConversationsResult> GetConversationsAsync(ITurnContext context, string continuationToken, CancellationToken cancellationToken)
|
||||
{
|
||||
var connectorClient = context.Services.Get<IConnectorClient>();
|
||||
var connectorClient = context.TurnState.Get<IConnectorClient>();
|
||||
var results = await connectorClient.Conversations.GetConversationsAsync(continuationToken, cancellationToken).ConfigureAwait(false);
|
||||
return results;
|
||||
}
|
||||
|
@ -598,8 +598,8 @@ namespace Microsoft.Bot.Builder
|
|||
claimsIdentity.AddClaim(new Claim(AuthenticationConstants.AppIdClaim, credentials.MicrosoftAppId));
|
||||
claimsIdentity.AddClaim(new Claim(AuthenticationConstants.ServiceUrlClaim, serviceUrl));
|
||||
|
||||
context.Services.Add<IIdentity>(BotIdentityKey, claimsIdentity);
|
||||
context.Services.Add(connectorClient);
|
||||
context.TurnState.Add<IIdentity>(BotIdentityKey, claimsIdentity);
|
||||
context.TurnState.Add(connectorClient);
|
||||
await RunPipelineAsync(context, callback, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
@ -632,7 +632,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// <returns>An OAuth client for the bot.</returns>
|
||||
protected OAuthClient CreateOAuthApiClient(ITurnContext context)
|
||||
{
|
||||
var client = context.Services.Get<IConnectorClient>() as ConnectorClient;
|
||||
var client = context.TurnState.Get<IConnectorClient>() as ConnectorClient;
|
||||
if (client == null)
|
||||
{
|
||||
throw new ArgumentNullException("CreateOAuthApiClient: OAuth requires a valid ConnectorClient instance");
|
||||
|
|
|
@ -92,13 +92,13 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var cachedState = context.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = context.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
var storageKey = GetStorageKey(context);
|
||||
if (force || cachedState == null || cachedState.State == null)
|
||||
{
|
||||
var items = await _storage.ReadAsync(new[] { storageKey }, cancellationToken).ConfigureAwait(false);
|
||||
items.TryGetValue(storageKey, out object val);
|
||||
context.Services[_contextServiceKey] = new CachedBotState((IDictionary<string, object>)val ?? new Dictionary<string, object>());
|
||||
context.TurnState[_contextServiceKey] = new CachedBotState((IDictionary<string, object>)val ?? new Dictionary<string, object>());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var cachedState = context.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = context.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
if (force || (cachedState != null && cachedState.IsChanged()))
|
||||
{
|
||||
var key = GetStorageKey(context);
|
||||
|
@ -144,10 +144,10 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(context));
|
||||
}
|
||||
|
||||
var cachedState = context.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = context.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
if (cachedState != null)
|
||||
{
|
||||
context.Services[_contextServiceKey] = new CachedBotState();
|
||||
context.TurnState[_contextServiceKey] = new CachedBotState();
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
@ -174,7 +174,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(propertyName));
|
||||
}
|
||||
|
||||
var cachedState = turnContext.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = turnContext.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
|
||||
// if there is no value, this will throw, to signal to IPropertyAccesor that a default value should be computed
|
||||
// This allows this to work with value types
|
||||
|
@ -200,7 +200,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(propertyName));
|
||||
}
|
||||
|
||||
var cachedState = turnContext.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = turnContext.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
cachedState.State.Remove(propertyName);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ namespace Microsoft.Bot.Builder
|
|||
throw new ArgumentNullException(nameof(propertyName));
|
||||
}
|
||||
|
||||
var cachedState = turnContext.Services.Get<CachedBotState>(_contextServiceKey);
|
||||
var cachedState = turnContext.TurnState.Get<CachedBotState>(_contextServiceKey);
|
||||
cachedState.State[propertyName] = value;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
|
|
@ -90,10 +90,10 @@ namespace Microsoft.Bot.Builder
|
|||
BotAdapter Adapter { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the services registered on this context object.
|
||||
/// Collection of values cached with the context object for the lifetime of the turn.
|
||||
/// </summary>
|
||||
/// <value>The collection of services registered on this context object.</value>
|
||||
TurnContextServiceCollection Services { get; }
|
||||
TurnContextStateCollection TurnState { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the incoming request.
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// Gets the services registered on this context object.
|
||||
/// </summary>
|
||||
/// <value>The services registered on this context object.</value>
|
||||
public TurnContextServiceCollection Services { get; } = new TurnContextServiceCollection();
|
||||
public TurnContextStateCollection TurnState { get; } = new TurnContextStateCollection();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the activity associated with this turn; or <c>null</c> when processing
|
||||
|
@ -368,7 +368,7 @@ namespace Microsoft.Bot.Builder
|
|||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Services.Dispose();
|
||||
TurnState.Dispose();
|
||||
}
|
||||
|
||||
private async Task<ResourceResponse> UpdateActivityInternalAsync(
|
||||
|
|
|
@ -7,30 +7,30 @@ using System.Collections.Generic;
|
|||
namespace Microsoft.Bot.Builder
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a set of collection of services associated with the <see cref="ITurnContext"/>.
|
||||
/// Values persisted for the lifetime of the turn as part of the <see cref="ITurnContext"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// TODO: add more details on what kind of services can/should be stored here, by whom and what the lifetime semantics are, etc.
|
||||
/// TODO: add more details on what kind of values can/should be stored here, by whom and what the lifetime semantics are, etc.
|
||||
/// </remarks>
|
||||
public class TurnContextServiceCollection : Dictionary<string, object>, IDisposable
|
||||
public class TurnContextStateCollection : Dictionary<string, object>, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TurnContextServiceCollection"/> class.
|
||||
/// Initializes a new instance of the <see cref="TurnContextStateCollection"/> class.
|
||||
/// </summary>
|
||||
public TurnContextServiceCollection()
|
||||
public TurnContextStateCollection()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a service by name from the turn's context.
|
||||
/// Gets a cached value by name from the turn's context.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">The type of the service.</typeparam>
|
||||
/// <typeparam name="T">The type of the service.</typeparam>
|
||||
/// <param name="key">The name of the service.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
|
||||
/// <returns>The service object; or null if no service is registered by the key, or
|
||||
/// the retrieved object does not match the service type.</returns>
|
||||
public TService Get<TService>(string key)
|
||||
where TService : class
|
||||
public T Get<T>(string key)
|
||||
where T : class
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
|
@ -39,7 +39,7 @@ namespace Microsoft.Bot.Builder
|
|||
|
||||
if (TryGetValue(key, out var service))
|
||||
{
|
||||
if (service is TService result)
|
||||
if (service is T result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
@ -50,53 +50,53 @@ namespace Microsoft.Bot.Builder
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the default service by type from the turn's context.
|
||||
/// Gets the default value by type from the turn's context.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">The type of the service.</typeparam>
|
||||
/// <typeparam name="T">The type of the service.</typeparam>
|
||||
/// <returns>The service object; or null if no default service of the type is registered.</returns>
|
||||
/// <remarks>The default service key is the <see cref="Type.FullName"/> of the service type.</remarks>
|
||||
public TService Get<TService>()
|
||||
where TService : class
|
||||
public T Get<T>()
|
||||
where T : class
|
||||
{
|
||||
return Get<TService>(typeof(TService).FullName);
|
||||
return Get<T>(typeof(T).FullName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a service to the turn's context.
|
||||
/// Adds a value to the turn's context.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">The type of the service.</typeparam>
|
||||
/// <typeparam name="T">The type of the service.</typeparam>
|
||||
/// <param name="key">The name of the service.</param>
|
||||
/// <param name="service">The service object to add.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="key"/> or <paramref name="service"/>
|
||||
/// <param name="value">The value to add.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="key"/> or <paramref name="value"/>
|
||||
/// is null.</exception>
|
||||
public void Add<TService>(string key, TService service)
|
||||
where TService : class
|
||||
public void Add<T>(string key, T value)
|
||||
where T : class
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
}
|
||||
|
||||
if (service == null)
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(service));
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
// note this can throw if teh key is already present
|
||||
base.Add(key, service);
|
||||
base.Add(key, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a service to the turn's context.
|
||||
/// Adds a value to the turn's context.
|
||||
/// </summary>
|
||||
/// <typeparam name="TService">The type of the service.</typeparam>
|
||||
/// <param name="service">The service object to add.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="service"/> is null.</exception>
|
||||
/// <typeparam name="T">The type of the service.</typeparam>
|
||||
/// <param name="value">The service object to add.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
|
||||
/// <remarks>The default service key is the <see cref="Type.FullName"/> of the service type.</remarks>
|
||||
public void Add<TService>(TService service)
|
||||
where TService : class
|
||||
public void Add<T>(T value)
|
||||
where T : class
|
||||
{
|
||||
Add(typeof(TService).FullName, service);
|
||||
Add(typeof(T).FullName, value);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
|
@ -368,7 +368,7 @@ namespace Microsoft.Bot.Builder.AI.QnA.Tests
|
|||
}
|
||||
public BotAdapter Adapter { get; }
|
||||
|
||||
public TurnContextServiceCollection Services => throw new NotImplementedException();
|
||||
public TurnContextStateCollection TurnState => throw new NotImplementedException();
|
||||
|
||||
public Activity Activity { get; }
|
||||
|
||||
|
|
|
@ -372,7 +372,7 @@ namespace Microsoft.Bot.Builder.Tests
|
|||
|
||||
await new TestFlow(adapter, (context, cancellationToken) =>
|
||||
{
|
||||
var obj = context.Services.Get<UserState>();
|
||||
var obj = context.TurnState.Get<UserState>();
|
||||
Assert.IsNull(obj, "context.state should not exist");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
|
|
@ -59,14 +59,14 @@ namespace Microsoft.Bot.Builder.Tests
|
|||
public void GetThrowsOnNullKey()
|
||||
{
|
||||
var c = new TurnContext(new SimpleAdapter(), new Activity());
|
||||
c.Services.Get<object>(null);
|
||||
c.TurnState.Get<object>(null);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetReturnsNullOnEmptyKey()
|
||||
{
|
||||
var c = new TurnContext(new SimpleAdapter(), new Activity());
|
||||
var service = c.Services.Get<object>(string.Empty); // empty key
|
||||
var service = c.TurnState.Get<object>(string.Empty); // empty key
|
||||
Assert.IsNull(service, "Should not have found a service under an empty key");
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ namespace Microsoft.Bot.Builder.Tests
|
|||
public void GetReturnsNullWithUnknownKey()
|
||||
{
|
||||
var c = new TurnContext(new SimpleAdapter(), new Activity());
|
||||
var o = c.Services.Get<object>("test");
|
||||
var o = c.TurnState.Get<object>("test");
|
||||
Assert.IsNull(o);
|
||||
}
|
||||
|
||||
|
@ -83,8 +83,8 @@ namespace Microsoft.Bot.Builder.Tests
|
|||
{
|
||||
var c = new TurnContext(new SimpleAdapter(), new Activity());
|
||||
|
||||
c.Services.Add("bar", "foo");
|
||||
var result = c.Services.Get<string>("bar");
|
||||
c.TurnState.Add("bar", "foo");
|
||||
var result = c.TurnState.Get<string>("bar");
|
||||
|
||||
Assert.AreEqual("foo", result);
|
||||
}
|
||||
|
@ -93,8 +93,8 @@ namespace Microsoft.Bot.Builder.Tests
|
|||
{
|
||||
var c = new TurnContext(new SimpleAdapter(), new Activity());
|
||||
|
||||
c.Services.Add<string>("foo");
|
||||
string result = c.Services.Get<string>();
|
||||
c.TurnState.Add<string>("foo");
|
||||
string result = c.TurnState.Get<string>();
|
||||
|
||||
Assert.AreEqual("foo", result);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче