Skip to content

Commit

Permalink
Fixing tests. Renaming DI types to have consistent terminology.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed Aug 6, 2018
1 parent 77ff387 commit 70495fa
Show file tree
Hide file tree
Showing 23 changed files with 456 additions and 307 deletions.
2 changes: 1 addition & 1 deletion sample/host.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"watchDirectories": [ "Shared" ],
"watchDirectories": [ "Shared", "Test" ],
"sendGrid": {
"from": "Azure Functions <[email protected]>"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public ScriptHostOptionsSetup(IConfiguration configuration, IOptions<ScriptWebHo

public void Configure(ScriptHostOptions options)
{
// Add the standard built in watched directories set to any the user may have specified
options.WatchDirectories.Add("node_modules");

// Bind to all configuration properties
IConfigurationSection jobHostSection = _configuration.GetSection(ConfigurationSectionNames.JobHost);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class ScriptHostLifetime : IHostLifetime
public class JobHostHostLifetime : IHostLifetime
{
public Task StopAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
/// <summary>
/// An <see cref="IServiceProviderFactory{TContainerBuilder}"/> implementation that creates
/// and populates an <see cref="ScriptHostServiceProvider"/> that can be used as the <see cref="IServiceProvider"/>
/// and populates an <see cref="JobHostServiceProvider"/> that can be used as the <see cref="IServiceProvider"/>
/// </summary>
public class ScriptHostScopedServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
public class JobHostScopedServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
{
private readonly IServiceProvider _rootProvider;
private readonly IServiceScopeFactory _rootScopeFactory;
private ScriptHostServiceProvider _provider;
private JobHostServiceProvider _provider;

public ScriptHostScopedServiceProviderFactory(IServiceProvider rootProvider, IServiceScopeFactory rootScopeFactory)
public JobHostScopedServiceProviderFactory(IServiceProvider rootProvider, IServiceScopeFactory rootScopeFactory)
{
_rootProvider = rootProvider ?? throw new ArgumentNullException(nameof(rootProvider));
_rootScopeFactory = rootScopeFactory ?? throw new ArgumentNullException(nameof(rootScopeFactory));
Expand All @@ -31,7 +31,7 @@ public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilde
{
if (_provider == null)
{
_provider = new ScriptHostServiceProvider(containerBuilder, _rootProvider, _rootScopeFactory);
_provider = new JobHostServiceProvider(containerBuilder, _rootProvider, _rootScopeFactory);
}

return _provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class ScriptHostServiceProvider : IServiceProvider, IServiceScopeFactory, ISupportRequiredService, IDisposable
public class JobHostServiceProvider : IServiceProvider, IServiceScopeFactory, ISupportRequiredService, IDisposable
{
private const string ScriptJobHostScope = "scriptjobhost";

private static readonly Rules _defaultContainerRules;
private readonly IServiceProvider _rootProvider;
private readonly IServiceScopeFactory _rootScopeFactory;
private readonly Container _container;
private ScriptHostScopedResolver _currentResolver;
private ScopedResolver _currentResolver;

static ScriptHostServiceProvider()
static JobHostServiceProvider()
{
_defaultContainerRules = Rules.Default
.With(FactoryMethod.ConstructorWithResolvableArguments)
.WithFactorySelector(Rules.SelectLastRegisteredFactory())
.WithTrackingDisposableTransients();
}

public ScriptHostServiceProvider(IServiceCollection descriptors, IServiceProvider rootProvider, IServiceScopeFactory rootScopeFactory)
public JobHostServiceProvider(IServiceCollection descriptors, IServiceProvider rootProvider, IServiceScopeFactory rootScopeFactory)
{
if (descriptors == null)
{
Expand All @@ -42,7 +42,7 @@ public ScriptHostServiceProvider(IServiceCollection descriptors, IServiceProvide
_rootScopeFactory = rootScopeFactory ?? throw new ArgumentNullException(nameof(rootScopeFactory));

_container = BuildContainer(descriptors);
_currentResolver = new ScriptHostScopedResolver(_container);
_currentResolver = new ScopedResolver(_container);
}

public IServiceProvider ServiceProvider => throw new NotImplementedException();
Expand All @@ -64,7 +64,7 @@ private Container BuildContainer(IServiceCollection descriptors)

container.Populate(descriptors);
container.UseInstance<IServiceProvider>(this);
container.UseInstance<ScriptHostServiceProvider>(this);
container.UseInstance<JobHostServiceProvider>(this);

return container;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class FunctionsServiceScope : IServiceScope
public class JobHostServiceScope : IServiceScope
{
private readonly ScopedServiceProvider _serviceProvider;

public FunctionsServiceScope(IResolverContext serviceProvider)
public JobHostServiceScope(IResolverContext serviceProvider)
{
_serviceProvider = new ScopedServiceProvider(serviceProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
internal class ScriptHostScopedResolver : IDisposable
internal class ScopedResolver : IDisposable
{
public ScriptHostScopedResolver(IContainer resolver, bool isRootResolver = false)
public ScopedResolver(IContainer resolver, bool isRootResolver = false)
{
Container = resolver ?? throw new ArgumentNullException(nameof(resolver));
IsRootResolver = isRootResolver;
ChildScopes = new HashSet<ScriptHostServiceScope>();
ChildScopes = new HashSet<ServiceScope>();
}

public IContainer Container { get; }

public HashSet<ScriptHostServiceScope> ChildScopes { get; }
public HashSet<ServiceScope> ChildScopes { get; }

public bool IsRootResolver { get; }

Expand All @@ -35,7 +35,7 @@ public void Dispose()
});
}

internal ScriptHostServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
{
var scopedRoot = rootScopeFactory.CreateScope();
Container scopedContext = Container.OpenScope() as Container;
Expand All @@ -51,7 +51,7 @@ internal ScriptHostServiceScope CreateChildScope(IServiceScopeFactory rootScopeF
return new DelegateFactory(_ => scopedRoot.ServiceProvider.GetService(request.ServiceType));
}));

var scope = new ScriptHostServiceScope(resolver, scopedRoot);
var scope = new ServiceScope(resolver, scopedRoot);
ChildScopes.Add(scope);

scope.DisposalTask.ContinueWith(t => ChildScopes.Remove(scope));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class ScriptHostServiceScope : IServiceScope
public class ServiceScope : IServiceScope
{
private readonly TaskCompletionSource<object> _activeTcs;
private readonly ScopedServiceProvider _serviceProvider;
private readonly IServiceScope _rootScope;

public ScriptHostServiceScope(IResolverContext serviceProvider, IServiceScope rootScope)
public ServiceScope(IResolverContext serviceProvider, IServiceScope rootScope)
{
_activeTcs = new TaskCompletionSource<object>();
_serviceProvider = new ScopedServiceProvider(serviceProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
public class FunctionsServiceProvider : IServiceProvider, IServiceScopeFactory
public class WebHostServiceProvider : IServiceProvider, IServiceScopeFactory
{
private static readonly Rules _defaultContainerRules;
private readonly Container _container;
private ScriptHostScopedResolver _currentResolver;
private ScopedResolver _currentResolver;

static FunctionsServiceProvider()
static WebHostServiceProvider()
{
_defaultContainerRules = Rules.Default
.With(FactoryMethod.ConstructorWithResolvableArguments)
.WithFactorySelector(Rules.SelectLastRegisteredFactory())
.WithTrackingDisposableTransients();
}

public FunctionsServiceProvider(IServiceCollection descriptors)
public WebHostServiceProvider(IServiceCollection descriptors)
{
if (descriptors == null)
{
Expand All @@ -34,7 +34,7 @@ public FunctionsServiceProvider(IServiceCollection descriptors)
_container.UseInstance<IServiceProvider>(this);
_container.UseInstance<IServiceScopeFactory>(this);

_currentResolver = new ScriptHostScopedResolver(_container);
_currentResolver = new ScopedResolver(_container);
}

public object GetService(Type serviceType)
Expand All @@ -44,7 +44,7 @@ public object GetService(Type serviceType)

public IServiceScope CreateScope()
{
return new FunctionsServiceScope(_container.OpenScope());
return new JobHostServiceScope(_container.OpenScope());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection
{
internal class FunctionsServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
internal class WebHostServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
{
public IServiceCollection CreateBuilder(IServiceCollection services)
{
Expand All @@ -15,7 +15,7 @@ public IServiceCollection CreateBuilder(IServiceCollection services)

public IServiceProvider CreateServiceProvider(IServiceCollection services)
{
return new FunctionsServiceProvider(services);
return new WebHostServiceProvider(services);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ public FunctionInvocationMiddleware(RequestDelegate next)
_next = next;
}

public async Task Invoke(HttpContext context, IScriptJobHost scriptHost)
public async Task Invoke(HttpContext context, IScriptJobHost scriptHost = null)
{
// flow required context through the request pipeline
// downstream middleware and filters rely on this
context.Items[ScriptConstants.AzureFunctionsHostKey] = scriptHost;
if (scriptHost != null)
{
// flow required context through the request pipeline
// downstream middleware and filters rely on this
context.Items[ScriptConstants.AzureFunctionsHostKey] = scriptHost;
}
SetRequestId(context.Request);

if (_next != null)
Expand Down
2 changes: 1 addition & 1 deletion src/WebJobs.Script.WebHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args = null)
return Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(new FunctionsServiceProviderFactory()));
services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(new WebHostServiceProviderFactory()));
})
.ConfigureAppConfiguration((builderContext, config) =>
{
Expand Down
57 changes: 56 additions & 1 deletion src/WebJobs.Script.WebHost/ScriptHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,80 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Timers;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Azure.WebJobs.Script.Binding;
using Microsoft.Azure.WebJobs.Script.BindingExtensions;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Eventing;
using Microsoft.Azure.WebJobs.Script.Extensibility;
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.Azure.WebJobs.Script.WebHost
{
public static class ScriptHostBuilderExtensions
{
public static IHostBuilder AddScriptHost(this IHostBuilder builder, IOptions<ScriptWebHostOptions> webHostOptions)
public static IHostBuilder AddScriptHost(this IHostBuilder builder, IServiceProvider rootServiceProvider,
IServiceScopeFactory rootScopeFactory, IOptions<ScriptWebHostOptions> webHostOptions)
{
// Host configuration
builder.UseServiceProviderFactory(new JobHostScopedServiceProviderFactory(rootServiceProvider, rootScopeFactory))
.ConfigureLogging(b =>
{
b.AddConsole(c => { c.DisableColors = false; });
b.SetMinimumLevel(LogLevel.Trace);
b.AddFilter(f => true);

// TODO: DI (FACAVAL) Temporary - replace with proper logger factory using
// job host configuration
b.Services.AddSingleton<ILoggerFactory, CustomFactory>();

b.Services.AddSingleton<ILoggerProvider, SystemLoggerProvider>();
b.Services.AddSingleton<ILoggerProvider, HostFileLoggerProvider>();
b.Services.AddSingleton<ILoggerProvider, FunctionFileLoggerProvider>();
})
.ConfigureServices(s =>
{
s.AddSingleton<IHostLifetime, JobHostHostLifetime>();
})
.ConfigureAppConfiguration(c =>
{
c.Add(new HostJsonFileConfigurationSource(webHostOptions));
});

// WebJobs configuration
builder.AddScriptHostCore(webHostOptions);

// HACK: Remove previous IHostedService registration
// TODO: DI (FACAVAL) Remove this and move HttpInitialization to webjobs configuration
builder.ConfigureServices(s =>
{
s.RemoveAll<IHostedService>();
s.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, PrimaryHostCoordinator>());
s.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, JobHostService>());
s.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, HttpInitializationService>());
s.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, FileMonitoringService>());
});

// If there is a script host builder registered, allow it to configure
// the host builder
var scriptBuilder = rootServiceProvider.GetService<IScriptHostBuilder>();
scriptBuilder?.Configure(builder);

return builder;
}

public static IHostBuilder AddScriptHostCore(this IHostBuilder builder, IOptions<ScriptWebHostOptions> webHostOptions)
{
builder.ConfigureWebJobsHost(o =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace Microsoft.Azure.WebJobs.Script.WebHost
{
public static class WebJobsServiceCollectionExtensions
public static class WebHostServiceCollectionExtensions
{
public static IServiceCollection AddWebJobsScriptHostRouting(this IServiceCollection services)
{
Expand Down Expand Up @@ -124,7 +124,7 @@ public static void AddWebJobsScriptHost(this IServiceCollection services, IConfi

// Configuration
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<ScriptWebHostOptions>, ScriptWebHostOptionsSetup>());
}
}

// TODO: DI (FACAVAL) Removing this. We need to ensure system logs are properly written now when using the default provider.
//private static ILoggerFactory CreateLoggerFactory(string hostInstanceId, ScriptSettingsManager settingsManager, IEventGenerator eventGenerator, WebHostSettings settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static IApplicationBuilder UseWebJobsScriptHost(this IApplicationBuilder
builder.UseMiddleware<HttpExceptionMiddleware>();
builder.UseMiddleware<ResponseBufferingMiddleware>();
builder.UseMiddleware<HomepageMiddleware>();
// builder.UseMiddleware<FunctionInvocationMiddleware>();
builder.UseMiddleware<FunctionInvocationMiddleware>();
builder.UseMiddleware<HostWarmupMiddleware>();

builder.UseWhen(context => !context.Request.Path.StartsWithSegments("/admin"), config =>
Expand Down
Loading

0 comments on commit 70495fa

Please sign in to comment.