-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSignalRTriggerBindingProvider.cs
187 lines (161 loc) · 8.01 KB
/
SignalRTriggerBindingProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Host.Triggers;
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Extensions.SignalRService
{
internal class SignalRTriggerBindingProvider : ITriggerBindingProvider
{
private readonly ISignalRTriggerDispatcher _dispatcher;
private readonly INameResolver _nameResolver;
private readonly IServiceManagerStore _managerStore;
private readonly Exception _webhookException;
public SignalRTriggerBindingProvider(ISignalRTriggerDispatcher dispatcher, INameResolver nameResolver, IServiceManagerStore managerStore, Exception webhookException)
{
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
_nameResolver = nameResolver ?? throw new ArgumentNullException(nameof(nameResolver));
_managerStore = managerStore ?? throw new ArgumentNullException(nameof(managerStore));
_webhookException = webhookException;
}
public Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var parameterInfo = context.Parameter;
var attribute = parameterInfo.GetCustomAttribute<SignalRTriggerAttribute>(false);
if (attribute == null)
{
return Task.FromResult<ITriggerBinding>(null);
}
if (_webhookException != null)
{
throw new NotSupportedException($"SignalR trigger is disabled due to 'AzureWebJobsStorage' connection string is not set or invalid. {_webhookException}");
}
var resolvedAttribute = GetParameterResolvedAttribute(attribute, parameterInfo);
ValidateSignalRTriggerAttributeBinding(resolvedAttribute);
var accessKeys = _managerStore.GetOrAddByConnectionStringKey(attribute.ConnectionStringSetting).AccessKeys;
return Task.FromResult<ITriggerBinding>(new SignalRTriggerBinding(parameterInfo, resolvedAttribute, _dispatcher, accessKeys));
}
internal SignalRTriggerAttribute GetParameterResolvedAttribute(SignalRTriggerAttribute attribute, ParameterInfo parameterInfo)
{
//TODO: AutoResolve more properties in attribute
var hubName = attribute.HubName;
var category = attribute.Category;
var @event = attribute.Event;
var parameterNames = attribute.ParameterNames ?? Array.Empty<string>();
// We have two models for C#, one is function based model which also work in multiple language
// Another one is class based model, which is highly close to SignalR itself but must keep some conventions.
var method = (MethodInfo)parameterInfo.Member;
var declaredType = method.DeclaringType;
string[] parameterNamesFromAttribute;
if (declaredType != null && declaredType.IsSubclassOf(typeof(ServerlessHub)))
{
// Class based model
if (!string.IsNullOrEmpty(hubName) ||
!string.IsNullOrEmpty(category) ||
!string.IsNullOrEmpty(@event) ||
parameterNames.Length != 0)
{
throw new ArgumentException($"{nameof(SignalRTriggerAttribute)} must use parameterless constructor in class based model.");
}
parameterNamesFromAttribute = method.GetParameters().Where(IsLegalClassBasedParameter).Select(p => p.Name).ToArray();
hubName = declaredType.Name;
category = GetCategoryFromMethodName(method.Name);
@event = GetEventFromMethodName(method.Name, category);
}
else
{
parameterNamesFromAttribute = method.GetParameters().
Where(p => p.GetCustomAttribute<SignalRParameterAttribute>(false) != null).
Select(p => p.Name).ToArray();
if (parameterNamesFromAttribute.Length != 0 && parameterNames.Length != 0)
{
throw new InvalidOperationException(
$"{nameof(SignalRTriggerAttribute)}.{nameof(SignalRTriggerAttribute.ParameterNames)} and {nameof(SignalRParameterAttribute)} can not be set in the same Function.");
}
}
parameterNames = parameterNamesFromAttribute.Length != 0
? parameterNamesFromAttribute
: parameterNames;
return new SignalRTriggerAttribute(hubName, category, @event, parameterNames) { ConnectionStringSetting = attribute.ConnectionStringSetting };
}
private void ValidateSignalRTriggerAttributeBinding(SignalRTriggerAttribute attribute)
{
if (string.IsNullOrWhiteSpace(attribute.ConnectionStringSetting))
{
throw new InvalidOperationException(
$"{nameof(SignalRTriggerAttribute)}.{nameof(SignalRConnectionInfoAttribute.ConnectionStringSetting)} is not allowed to be null or whitespace.");
}
ValidateParameterNames(attribute.ParameterNames);
}
private string GetCategoryFromMethodName(string name)
{
if (string.Equals(name, Constants.OnConnected, StringComparison.OrdinalIgnoreCase) ||
string.Equals(name, Constants.OnDisconnected, StringComparison.OrdinalIgnoreCase))
{
return Category.Connections;
}
return Category.Messages;
}
private string GetEventFromMethodName(string name, string category)
{
if (category == Category.Connections)
{
if (string.Equals(name, Constants.OnConnected, StringComparison.OrdinalIgnoreCase))
{
return Event.Connected;
}
if (string.Equals(name, Constants.OnDisconnected, StringComparison.OrdinalIgnoreCase))
{
return Event.Disconnected;
}
}
return name;
}
private void ValidateParameterNames(string[] parameterNames)
{
if (parameterNames == null || parameterNames.Length == 0)
{
return;
}
if (parameterNames.Length != parameterNames.Distinct(StringComparer.OrdinalIgnoreCase).Count())
{
throw new ArgumentException("Elements in ParameterNames should be ignore case unique.");
}
}
private bool IsLegalClassBasedParameter(ParameterInfo parameter)
{
// In class based model, we treat all the parameters as a legal parameter except the cases below
// 1. Parameter decorated by [SignalRIgnore]
// 2. Parameter decorated Attribute that has BindingAttribute
// 3. Two special type ILogger and CancellationToken
if (parameter.ParameterType.IsAssignableFrom(typeof(ILogger)) ||
parameter.ParameterType.IsAssignableFrom(typeof(CancellationToken)))
{
return false;
}
if (parameter.GetCustomAttribute<SignalRIgnoreAttribute>() != null)
{
return false;
}
if (HasBindingAttribute(parameter.GetCustomAttributes()))
{
return false;
}
return true;
}
private bool HasBindingAttribute(IEnumerable<Attribute> attributes)
{
return attributes.Any(attribute => attribute.GetType().GetCustomAttribute<BindingAttribute>(false) != null);
}
}
}