Skip to content

Commit

Permalink
Switch to Microsoft.Extensions.HostFactoryResolver.Sources
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Jan 15, 2019
1 parent 78a06a2 commit 5724d29
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 91 deletions.
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Dependencies>
<ProductDependencies>
<Dependency Name="Internal.WebHostBuilderFactory.Sources" Version="3.0.0-preview.18617.2">
<Uri>https://github.com/aspnet/Extensions</Uri>
<Sha>0000000000000000000000000000000000000000</Sha>
</Dependency>
<Dependency Name="Microsoft.CSharp" Version="4.6.0-preview.19064.4">
<Uri>https://github.com/dotnet/corefx</Uri>
<Sha>60f871b05ac8db8de316dc9ed5e7647808011225</Sha>
Expand Down Expand Up @@ -37,6 +33,10 @@
<Uri>https://github.com/dotnet/core-setup</Uri>
<Sha>90ba7b628ff397caa35c50e8ecbf50f96308d1c3</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.HostFactoryResolver.Sources" Version="3.0.0-preview.19064.2">
<Uri>https://github.com/aspnet/Extensions</Uri>
<Sha>73f75d90945a519ab2f0bace4fe9b10de5eb0dd4</Sha>
</Dependency>
<Dependency Name="Microsoft.Extensions.Logging" Version="3.0.0-preview.19064.2">
<Uri>https://github.com/aspnet/Extensions</Uri>
<Sha>73f75d90945a519ab2f0bace4fe9b10de5eb0dd4</Sha>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
<SystemInteractiveAsyncPackageVersion>3.2.0</SystemInteractiveAsyncPackageVersion>
</PropertyGroup>
<PropertyGroup Label="Dependencies from aspnet/Extensions">
<InternalWebHostBuilderFactorySourcesPackageVersion>3.0.0-preview-18571-0016</InternalWebHostBuilderFactorySourcesPackageVersion>
<MicrosoftExtensionsCachingMemoryPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsCachingMemoryPackageVersion>
<MicrosoftExtensionsConfigurationPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsConfigurationPackageVersion>
<MicrosoftExtensionsConfigurationAbstractionsPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsConfigurationAbstractionsPackageVersion>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
<MicrosoftExtensionsConfigurationJsonPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsConfigurationJsonPackageVersion>
<MicrosoftExtensionsDependencyInjectionPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsDependencyInjectionPackageVersion>
<MicrosoftExtensionsHostFactoryResolverSourcesPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsHostFactoryResolverSourcesPackageVersion>
<MicrosoftExtensionsLoggingPackageVersion>3.0.0-preview.19064.2</MicrosoftExtensionsLoggingPackageVersion>
</PropertyGroup>
<PropertyGroup Label="Dependencies from dotnet/corefx">
Expand Down
50 changes: 19 additions & 31 deletions src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Diagnostics;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Hosting.WebHostBuilderFactory;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Microsoft.EntityFrameworkCore.Design.Internal
{
Expand Down Expand Up @@ -38,29 +38,20 @@ public virtual IServiceProvider Create([NotNull] string[] args)
{
_reporter.WriteVerbose(DesignStrings.FindingServiceProvider);

return CreateFromBuildWebHost(args)
return CreateFromHosting(args)
?? CreateEmptyServiceProvider();
}

private IServiceProvider CreateFromBuildWebHost(string[] args)
private IServiceProvider CreateFromHosting(string[] args)
{
_reporter.WriteVerbose(DesignStrings.FindingBuildWebHost);
_reporter.WriteVerbose(DesignStrings.FindingHostingServices);

var webHostFactoryResult = WebHostFactoryResolver.ResolveWebHostFactory<object, object>(_startupAssembly);
switch (webHostFactoryResult.ResultKind)
var serviceProviderFactory = HostFactoryResolver.ResolveServiceProviderFactory(_startupAssembly);
if (serviceProviderFactory == null)
{
case FactoryResolutionResultKind.Success:
break;
case FactoryResolutionResultKind.NoEntryPoint:
_reporter.WriteVerbose(DesignStrings.NoEntryPoint(_startupAssembly.GetName().Name));
return null;
case FactoryResolutionResultKind.NoCreateWebHostBuilder:
case FactoryResolutionResultKind.NoBuildWebHost:
_reporter.WriteVerbose(DesignStrings.NoBuildWebHost(webHostFactoryResult.ProgramType.DisplayName()));
return null;
default:
Debug.Fail("Unexpected value: " + webHostFactoryResult.ResultKind);
return null;
_reporter.WriteVerbose(DesignStrings.NoCreateHostBuilder);

return null;
}

// TODO: Remove when dotnet/cli#6617 is fixed
Expand All @@ -72,14 +63,18 @@ private IServiceProvider CreateFromBuildWebHost(string[] args)
}

_reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));
_reporter.WriteVerbose(DesignStrings.UsingBuildWebHost(webHostFactoryResult.ProgramType.ShortDisplayName()));

try
{
var webHost = webHostFactoryResult.WebHostFactory(args);
var webHostType = webHost.GetType();
var servicesProperty = webHostType.GetTypeInfo().GetDeclaredProperty("Services");
var services = (IServiceProvider)servicesProperty.GetValue(webHost);
var services = serviceProviderFactory(args);
if (services == null)
{
_reporter.WriteWarning(DesignStrings.MalformedCreateHostBuilder);

return null;
}

_reporter.WriteVerbose(DesignStrings.UsingHostingServices);

return services.CreateScope().ServiceProvider;
}
Expand All @@ -91,19 +86,12 @@ private IServiceProvider CreateFromBuildWebHost(string[] args)
}

_reporter.WriteVerbose(ex.ToString());
_reporter.WriteWarning(DesignStrings.InvokeBuildWebHostFailed(webHostFactoryResult.ProgramType.ShortDisplayName(), ex.Message));
_reporter.WriteWarning(DesignStrings.InvokeCreateHostBuilderFailed(ex.Message));

return null;
}
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected virtual Type FindProgramClass()
=> _startupAssembly.EntryPoint?.DeclaringType;

private IServiceProvider CreateEmptyServiceProvider()
{
_reporter.WriteVerbose(DesignStrings.NoServiceProvider);
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Internal.WebHostBuilderFactory.Sources" PrivateAssets="All" Version="$(InternalWebHostBuilderFactorySourcesPackageVersion)" />
<PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.HostFactoryResolver.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsHostFactoryResolverSourcesPackageVersion)" />
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersPackageVersion)" PrivateAssets="All" />
</ItemGroup>

Expand Down
70 changes: 32 additions & 38 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,12 @@ Change your target project to the migrations project by using the Package Manage
<data name="UsingEnvironment" xml:space="preserve">
<value>Using environment '{environment}'.</value>
</data>
<data name="FindingBuildWebHost" xml:space="preserve">
<value>Finding IWebHost accessor...</value>
</data>
<data name="FindingServiceProvider" xml:space="preserve">
<value>Finding application service provider...</value>
</data>
<data name="NoBuildWebHost" xml:space="preserve">
<value>No CreateWebHostBuilder(string[]) method was found on type '{programClass}'.</value>
</data>
<data name="NoEntryPoint" xml:space="preserve">
<value>No entry point was found for assembly '{startupAssembly}'.</value>
</data>
<data name="NoServiceProvider" xml:space="preserve">
<value>No application service provider was found.</value>
</data>
<data name="UsingBuildWebHost" xml:space="preserve">
<value>Using application service provider from IWebHost accessor on '{programClass}'.</value>
</data>
<data name="FoundDbContext" xml:space="preserve">
<value>Found DbContext '{contextType}'.</value>
</data>
Expand Down Expand Up @@ -339,9 +327,6 @@ Change your target project to the migrations project by using the Package Manage
<data name="SequencesRequireName" xml:space="preserve">
<value>Sequence name cannot be null or empty. Entity Framework cannot model a sequence that does not have a name.</value>
</data>
<data name="InvokeBuildWebHostFailed" xml:space="preserve">
<value>An error occurred while accessing the IWebHost on class '{startupClass}'. Continuing without the application service provider. Error: {error}</value>
</data>
<data name="NoLanguageService" xml:space="preserve">
<value>The project language '{language}' isn't supported by the built-in {service} service. You can try looking for an additional NuGet package which supports this language; moving your DbContext type to a C# class library referenced by this project; or manually implementing and registering the design-time service for programming language.</value>
</data>
Expand All @@ -360,4 +345,19 @@ Change your target project to the migrations project by using the Package Manage
<data name="VersionMismatch" xml:space="preserve">
<value>The EF Core tools version '{toolsVersion}' is older than that of the runtime '{runtimeVersion}'. Update the tools for the latest features and bug fixes.</value>
</data>
<data name="FindingHostingServices" xml:space="preserve">
<value>Finding Microsoft.Extensions.Hosting service provider...</value>
</data>
<data name="InvokeCreateHostBuilderFailed" xml:space="preserve">
<value>An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: {error}</value>
</data>
<data name="MalformedCreateHostBuilder" xml:space="preserve">
<value>An unexpected return type was encountered while accessing the Microsoft.Extensions.Hosting services. Method 'CreateHostBuilder(string[])' should return an object of type 'IHostBuilder'. Continuing without the application service provider.</value>
</data>
<data name="NoCreateHostBuilder" xml:space="preserve">
<value>No static method 'CreateHostBuilder(string[])' was found on class 'Program'.</value>
</data>
<data name="UsingHostingServices" xml:space="preserve">
<value>Using applicaiton service provider from Microsoft.Extensions.Hosting.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Create_works_when_BuildWebHost_throws()
Assert.NotNull(services);
Assert.Contains(
"warn: " +
DesignStrings.InvokeBuildWebHostFailed(nameof(ProgramWithThrowingBuildWebHost), "This is a test."),
DesignStrings.InvokeCreateHostBuilderFailed("This is a test."),
reporter.Messages);
}

Expand Down

0 comments on commit 5724d29

Please sign in to comment.