Fix streaming hub methods combined with async (#1544)

This commit is contained in:
James Newton-King 2018-03-07 20:07:06 +13:00 committed by GitHub
parent 1b9313287b
commit 1c44e8febf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

View File

@ -372,6 +372,9 @@ namespace Microsoft.AspNetCore.SignalR.Internal
private static bool IsStreamed(Type resultType) private static bool IsStreamed(Type resultType)
{ {
// TODO: cache reflection for performance, on HubMethodDescriptor maybe?
resultType = UnwrapTask(resultType);
var observableInterface = IsIObservable(resultType) ? var observableInterface = IsIObservable(resultType) ?
resultType : resultType :
resultType.GetInterfaces().FirstOrDefault(IsIObservable); resultType.GetInterfaces().FirstOrDefault(IsIObservable);
@ -393,6 +396,9 @@ namespace Microsoft.AspNetCore.SignalR.Internal
{ {
if (result != null) if (result != null)
{ {
// TODO: cache reflection for performance, on HubMethodDescriptor maybe?
resultType = UnwrapTask(resultType);
var observableInterface = IsIObservable(resultType) ? var observableInterface = IsIObservable(resultType) ?
resultType : resultType :
resultType.GetInterfaces().FirstOrDefault(IsIObservable); resultType.GetInterfaces().FirstOrDefault(IsIObservable);
@ -420,6 +426,16 @@ namespace Microsoft.AspNetCore.SignalR.Internal
} }
} }
private static Type UnwrapTask(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
{
return type.GetGenericArguments()[0];
}
return type;
}
private static bool IsIObservable(Type iface) private static bool IsIObservable(Type iface)
{ {
return iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IObservable<>); return iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IObservable<>);

View File

@ -513,6 +513,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests.HubEndpointTestUtils
return new CountingObservable(count); return new CountingObservable(count);
} }
public async Task<IObservable<string>> CounterObservableAsync(int count)
{
await Task.Yield();
return CounterObservable(count);
}
public ChannelReader<string> CounterChannel(int count) public ChannelReader<string> CounterChannel(int count)
{ {
var channel = Channel.CreateUnbounded<string>(); var channel = Channel.CreateUnbounded<string>();
@ -529,6 +535,12 @@ namespace Microsoft.AspNetCore.SignalR.Tests.HubEndpointTestUtils
return channel.Reader; return channel.Reader;
} }
public async Task<ChannelReader<string>> CounterChannelAsync(int count)
{
await Task.Yield();
return CounterChannel(count);
}
public ChannelReader<string> BlockingStream() public ChannelReader<string> BlockingStream()
{ {
return Channel.CreateUnbounded<string>().Reader; return Channel.CreateUnbounded<string>().Reader;

View File

@ -1453,7 +1453,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests
{ {
get get
{ {
foreach (var method in new[] { nameof(StreamingHub.CounterChannel), nameof(StreamingHub.CounterObservable) }) foreach (var method in new[] { nameof(StreamingHub.CounterChannel), nameof(StreamingHub.CounterChannelAsync), nameof(StreamingHub.CounterObservable), nameof(StreamingHub.CounterObservableAsync) })
{ {
foreach (var protocol in new IHubProtocol[] { new JsonHubProtocol(), new MessagePackHubProtocol() }) foreach (var protocol in new IHubProtocol[] { new JsonHubProtocol(), new MessagePackHubProtocol() })
{ {