Skip to content

Implements Type in SignalRValueProvider #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ public Task<IValueProvider> BindAsync(object value, ValueBindingContext context)
throw new NotSupportedException($"Argument {nameof(HttpRequest)} is null. {nameof(SecurityTokenValidationAttribute)} must work with HttpTrigger.");
}

if (securityTokenValidator == null)
{
return Task.FromResult((IValueProvider)new SignalRValueProvider(null));
}

return Task.FromResult((IValueProvider)new SignalRValueProvider(securityTokenValidator.ValidateToken(request)));
return Task.FromResult(SignalRValueProvider.Create(
securityTokenValidator?.ValidateToken(request)));
}

public Task<IValueProvider> BindAsync(BindingContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override Task<IValueProvider> BuildAsync(SignalRConnectionInfoAttribut
{
var info = azureSignalRClient.GetClientConnectionInfo(attrResolved.UserId, attrResolved.IdToken,
attrResolved.ClaimTypeList);
return Task.FromResult((IValueProvider)new SignalRValueProvider(info));
return Task.FromResult(SignalRValueProvider.Create(info));
}

var request = bindingData[HttpRequestName] as HttpRequest;
Expand All @@ -46,14 +46,14 @@ protected override Task<IValueProvider> BuildAsync(SignalRConnectionInfoAttribut

if (tokenResult.Status != SecurityTokenStatus.Valid)
{
return Task.FromResult((IValueProvider)new SignalRValueProvider(null));
return Task.FromResult((IValueProvider)SignalRNullValueProvider<SignalRConnectionInfo>.Instance);
}

if (signalRConnectionInfoConfigurer == null)
{
var info = azureSignalRClient.GetClientConnectionInfo(attrResolved.UserId, attrResolved.IdToken,
attrResolved.ClaimTypeList);
return Task.FromResult((IValueProvider)new SignalRValueProvider(info));
return Task.FromResult(SignalRValueProvider.Create(info));
}

var signalRConnectionDetail = new SignalRConnectionDetail
Expand All @@ -64,7 +64,7 @@ protected override Task<IValueProvider> BuildAsync(SignalRConnectionInfoAttribut
signalRConnectionInfoConfigurer.Configure(tokenResult, request, signalRConnectionDetail);
var customizedInfo = azureSignalRClient.GetClientConnectionInfo(signalRConnectionDetail.UserId,
signalRConnectionDetail.Claims);
return Task.FromResult((IValueProvider)new SignalRValueProvider(customizedInfo));
return Task.FromResult(SignalRValueProvider.Create(customizedInfo));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.SignalRService.Internal;
using Microsoft.Azure.WebJobs.Host.Bindings;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
internal class SignalRNullValueProvider<T> : IValueProvider
{
internal static readonly SignalRNullValueProvider<T> Instance = new SignalRNullValueProvider<T>();

public Type Type { get; }

private SignalRNullValueProvider()
=> Type = typeof(T);

public Task<object> GetValueAsync()
=> NullObjectTask.Result;

public string ToInvokeString()
=> null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
internal class SignalRValueProvider : IValueProvider
{
private object value;
private readonly object value;

public SignalRValueProvider(object value)
private SignalRValueProvider(object value, Type valueType)
{
this.value = value;
}

public Task<object> GetValueAsync()
{
return Task.FromResult(value);
Type = valueType;
}

public string ToInvokeString()
{
return value?.ToString();
}
internal static IValueProvider Create<T>(T value) where T : class
=> value == null
? (IValueProvider) SignalRNullValueProvider<T>.Instance
: new SignalRValueProvider(value, typeof(T));

public Type Type { get; }

public Task<object> GetValueAsync()
=> Task.FromResult(value);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wanlwanl is this a "hot" path? Would it be better to create a field holding a Task.FromResult(value) and set in in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the change I meant. If no good, I can revert.


public string ToInvokeString()
=> value.ToString();
}
}
}
12 changes: 12 additions & 0 deletions src/SignalRServiceExtension/Internal/NullObjectTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Threading.Tasks;

namespace Microsoft.Azure.WebJobs.Extensions.SignalRService.Internal
{
internal static class NullObjectTask
{
internal static readonly Task<object> Result = Task.FromResult<object>(null);
}
}