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 all commits
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
@@ -1,12 +1,9 @@
// 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.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Azure.WebJobs.Host.Protocols;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -37,7 +34,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 +43,14 @@ protected override Task<IValueProvider> BuildAsync(SignalRConnectionInfoAttribut

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

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 +61,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
Expand Up @@ -9,23 +9,47 @@ namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
internal class SignalRValueProvider : IValueProvider
{
private object value;
private readonly Task<object> value;

public SignalRValueProvider(object value)
public Type Type { get; }

private SignalRValueProvider(object value, Type valueType)
{
this.value = value;
this.value = Task.FromResult(value);

Type = valueType;
}

internal static IValueProvider Create<T>(T value) where T : class
=> value == null
? NullOf<T>()
: new SignalRValueProvider(value, value.GetType());

internal static IValueProvider NullOf<T>() where T : class
=> SignalRNullValueProvider<T>.Instance;

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

public string ToInvokeString()
=> value.ToString();

private class SignalRNullValueProvider<T> : IValueProvider
{
return value?.ToString();
}
private readonly Task<object> nullObjectTask = Task.FromResult<object>(null);

public Type Type { get; }
internal static readonly SignalRNullValueProvider<T> Instance = new SignalRNullValueProvider<T>();

public Type Type { get; }

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

public Task<object> GetValueAsync()
=> nullObjectTask;

public string ToInvokeString()
=> null;
}
}
}
}