-
Notifications
You must be signed in to change notification settings - Fork 103
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
Set up options and track changes in management SDK. #1081
Changes from 19 commits
f1d6d2c
ef79564
04d9b10
1840a4d
8cb960c
fa833d2
f9b358e
c4f2030
32636be
c83f4c9
17b6585
52dc94b
5315a43
d3a3b68
1399157
d32b810
80874da
6786043
3d6a3ec
37947ab
e3edf86
9a5c796
a9a6153
f6b7d91
2532535
514355a
13ec165
35c083e
4b73ac4
903f1c2
383ad4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Azure.SignalR.Management.Configuration | ||
{ | ||
/// <summary> | ||
/// Sets up TargetOptions from SourceOptions and tracks changes . | ||
/// </summary> | ||
internal abstract class CascadeOptionsSetup<SourceOptions, TargetOptions> : IConfigureOptions<TargetOptions>, IOptionsChangeTokenSource<TargetOptions> | ||
where SourceOptions : class | ||
where TargetOptions : class | ||
{ | ||
private protected readonly IOptionsMonitor<SourceOptions> _monitor; | ||
private readonly IOptionsChangeTokenSource<SourceOptions> _changeTokenSource; | ||
|
||
//Making 'tokenSource' optional avoids error when 'tokenSource' is unavailable. | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public CascadeOptionsSetup(IOptionsMonitor<SourceOptions> monitor, IOptionsChangeTokenSource<SourceOptions> changeTokenSource = null) | ||
{ | ||
_monitor = monitor; | ||
_changeTokenSource = changeTokenSource; | ||
} | ||
|
||
public string Name => Options.DefaultName; | ||
|
||
public abstract void Configure(TargetOptions options); | ||
|
||
public IChangeToken GetChangeToken() | ||
{ | ||
return _changeTokenSource?.GetChangeToken() ?? NullChangeToken.Singleton; | ||
|
||
//We can't return a ChangeToken as null. | ||
//fixes in https://github.com/dotnet/runtime/pull/43306 | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Azure.SignalR.Management.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.SignalR.Management | ||
{ | ||
internal class ServiceManagerContextSetup : CascadeOptionsSetup<ServiceManagerOptions, ServiceManagerContext> | ||
{ | ||
public ServiceManagerContextSetup(IOptionsMonitor<ServiceManagerOptions> monitor, IOptionsChangeTokenSource<ServiceManagerOptions> changeTokenSource = null) : base(monitor, changeTokenSource) | ||
{ | ||
} | ||
|
||
public override void Configure(ServiceManagerContext options) | ||
{ | ||
var serviceManagerOptions = _monitor.CurrentValue; | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.ServiceEndpoints = serviceManagerOptions.ServiceEndpoints ?? (serviceManagerOptions.ServiceEndpoint != null | ||
? (new ServiceEndpoint[] { serviceManagerOptions.ServiceEndpoint }) | ||
: (new ServiceEndpoint[] { new ServiceEndpoint(serviceManagerOptions.ConnectionString) })); | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.ApplicationName = serviceManagerOptions.ApplicationName; | ||
options.ConnectionCount = serviceManagerOptions.ConnectionCount; | ||
options.Proxy = serviceManagerOptions.Proxy; | ||
options.ServiceTransportType = serviceManagerOptions.ServiceTransportType; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.FileProviders; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.Azure.SignalR.Management.Configuration | ||
{ | ||
internal class ServiceManagerOptionsSetup : IConfigureOptions<ServiceManagerOptions>, IOptionsChangeTokenSource<ServiceManagerOptions> | ||
{ | ||
private readonly IConfiguration _configuration; | ||
|
||
public ServiceManagerOptionsSetup(IConfiguration configuration = null) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public string Name => Options.DefaultName; | ||
|
||
public void Configure(ServiceManagerOptions options) | ||
{ | ||
if (_configuration != null) | ||
{ | ||
_configuration.GetSection(Constants.Keys.ServiceManagerOptionsSectionKey).Bind(options); | ||
} | ||
} | ||
|
||
public IChangeToken GetChangeToken() | ||
{ | ||
return _configuration?.GetReloadToken() ?? NullChangeToken.Singleton; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Azure.SignalR.Management.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.SignalR.Management | ||
{ | ||
internal class ServiceOptionsSetup : CascadeOptionsSetup<ServiceManagerContext, ServiceOptions> | ||
{ | ||
public ServiceOptionsSetup(IOptionsMonitor<ServiceManagerContext> monitor, IOptionsChangeTokenSource<ServiceManagerContext> changeTokenSource = null) : base(monitor, changeTokenSource) | ||
{ | ||
} | ||
|
||
public override void Configure(ServiceOptions options) | ||
{ | ||
var context = _monitor.CurrentValue; | ||
options.ApplicationName = context.ApplicationName; | ||
options.Endpoints = context.ServiceEndpoints; | ||
options.Proxy = context.Proxy; | ||
options.ConnectionCount = context.ConnectionCount; | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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.ComponentModel; | ||
using System.Reflection; | ||
using Microsoft.Azure.SignalR.Management.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.SignalR.Management | ||
{ | ||
internal static class DependencyInjectionExtensions //TODO: not ready for public use | ||
{ | ||
/// <summary> | ||
/// Adds the essential SignalR Service Manager services to the specified services collection and configures <see cref="ServiceManagerOptions"/> with configuration instance registered in service collection. | ||
/// </summary> | ||
public static IServiceCollection AddSignalRServiceManager(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<ServiceManagerOptionsSetup>() | ||
.AddSingleton<IConfigureOptions<ServiceManagerOptions>>(sp => sp.GetService<ServiceManagerOptionsSetup>()) | ||
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerOptions>>(sp => sp.GetService<ServiceManagerOptionsSetup>()); | ||
return services.AddSignalRServiceManagerCore(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the essential SignalR Service Manager services to the specified services collection and registers an action used to configure <see cref="ServiceManagerOptions"/> | ||
/// </summary> | ||
public static IServiceCollection AddSignalRServiceManager(this IServiceCollection services, Action<ServiceManagerOptions> configure) | ||
{ | ||
services.Configure(configure); | ||
return services.AddSignalRServiceManager(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the minimum essential SignalR Service Manager services to the specified services collection. | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <returns></returns> | ||
internal static IServiceCollection AddSignalRServiceManagerCore(this IServiceCollection services) | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
services.PostConfigure<ServiceManagerOptions>(o => o.ValidateOptions()); | ||
services.AddSingleton<ServiceManagerContextSetup>() | ||
.AddSingleton<IConfigureOptions<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>()) | ||
.AddSingleton<IOptionsChangeTokenSource<ServiceManagerContext>>(sp => sp.GetService<ServiceManagerContextSetup>()); | ||
services.AddSingleton<ServiceOptionsSetup>() | ||
.AddSingleton<IConfigureOptions<ServiceOptions>>(sp => sp.GetService<ServiceOptionsSetup>()) | ||
.AddSingleton<IOptionsChangeTokenSource<ServiceOptions>>(sp => sp.GetService<ServiceOptionsSetup>()); | ||
return services.TrySetProductInfo(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds product info to <see cref="ServiceManagerContext"/> | ||
/// </summary> | ||
/// <returns></returns> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public static IServiceCollection WithAssembly(this IServiceCollection services, Assembly assembly) | ||
{ | ||
var productInfo = ProductInfo.GetProductInfo(assembly); | ||
return services.Configure<ServiceManagerContext>(o => o.ProductInfo = productInfo); | ||
} | ||
|
||
private static IServiceCollection TrySetProductInfo(this IServiceCollection services) | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this anyway executes even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but if |
||
var productInfo = ProductInfo.GetProductInfo(assembly); | ||
return services.Configure<ServiceManagerContext>(o => o.ProductInfo = o.ProductInfo ?? productInfo); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,32 +4,47 @@ | |
using System; | ||
using System.ComponentModel; | ||
using System.Reflection; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
namespace Microsoft.Azure.SignalR.Management | ||
{ | ||
/// <summary> | ||
/// A builder for configuring <see cref="IServiceManager"/> instances. | ||
/// </summary> | ||
public class ServiceManagerBuilder : IServiceManagerBuilder | ||
public class ServiceManagerBuilder : IServiceManagerBuilder, IDisposable | ||
{ | ||
private readonly ServiceManagerOptions _options = new ServiceManagerOptions(); | ||
private readonly IServiceCollection _services = new ServiceCollection(); | ||
private Assembly _assembly; | ||
private ServiceProvider _serviceProvider; | ||
|
||
/// <summary> | ||
/// Configures the <see cref="IServiceManager"/> instances. | ||
/// Registers an action used to configure <see cref="IServiceManager"/>. | ||
/// </summary> | ||
/// <param name="configure">A callback to configure the <see cref="IServiceManager"/>.</param> | ||
/// <returns>The same instance of the <see cref="ServiceManagerBuilder"/> for chaining.</returns> | ||
public ServiceManagerBuilder WithOptions(Action<ServiceManagerOptions> configure) | ||
{ | ||
configure?.Invoke(_options); | ||
_services.Configure(configure); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Registers a configuration instance to configure <see cref="IServiceManager"/> | ||
/// </summary> | ||
/// <param name="config">The configuration instance.</param> | ||
/// <returns>The same instance of the <see cref="ServiceManagerBuilder"/> for chaining.</returns> | ||
internal ServiceManagerBuilder WithConfiguration(IConfiguration config) | ||
{ | ||
_services.Configure<ServiceManagerOptions>(config.GetSection(Constants.Keys.ServiceManagerOptionsSectionKey)); | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public ServiceManagerBuilder WithCallingAssembly() | ||
{ | ||
_assembly = Assembly.GetCallingAssembly(); | ||
_services.WithAssembly(_assembly); | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
|
@@ -39,16 +54,24 @@ public ServiceManagerBuilder WithCallingAssembly() | |
/// <returns>The instance of the <see cref="IServiceManager"/>.</returns> | ||
public IServiceManager Build() | ||
{ | ||
_options.ValidateOptions(); | ||
|
||
_services.AddSignalRServiceManagerCore(); | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_serviceProvider = _services.BuildServiceProvider(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check added, throw InvalidOperationException() if called twice. |
||
var context = _serviceProvider.GetRequiredService<IOptions<ServiceManagerContext>>().Value; | ||
var productInfo = ProductInfo.GetProductInfo(_assembly); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read from context? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
var context = new ServiceManagerContext() | ||
{ | ||
ProductInfo = productInfo | ||
}; | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
context.SetValueFromOptions(_options); | ||
var restClientBuilder = new RestClientFactory(productInfo); | ||
return new ServiceManager(context, restClientBuilder); | ||
} | ||
|
||
/// <summary> | ||
/// Dispose unmanaged resources accociated with the builder and the instance built from it. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_serviceProvider?.Dispose(); | ||
_serviceProvider = null; | ||
} | ||
|
||
//tests only | ||
Y-Sindo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
internal ServiceProvider GetServiceProvider() => _serviceProvider; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a more common name if it is in Common project, or you can move the const to the Management project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the key to
ServiceManagerOptions
as a const string namedSection
.