Skip to content
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

Update AppServiceProviderFactory to use WebHostFactoryResolver #10100

Merged
merged 1 commit into from
Oct 20, 2017
Merged
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
35 changes: 19 additions & 16 deletions src/EFCore.Design/Design/Internal/AppServiceProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

using System;
using System.Reflection;
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting.WebHostBuilderFactory;

namespace Microsoft.EntityFrameworkCore.Design.Internal
{
Expand Down Expand Up @@ -44,20 +46,21 @@ private IServiceProvider CreateFromBuildWebHost(string[] args)
{
_reporter.WriteVerbose(DesignStrings.FindingBuildWebHost);

var programType = FindProgramClass();
if (programType == null)
var webHostFactoryResult = WebHostFactoryResolver.ResolveWebHostFactory<object, object>(_startupAssembly);
switch (webHostFactoryResult.ResultKind)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be important to preserve all these messages. If you think it's more maintainable to replace them with a generic "can't find web host factory" message, I won't push back.

{
_reporter.WriteVerbose(DesignStrings.NoEntryPoint(_startupAssembly.GetName().Name));

return null;
}

var buildWebHostMethod = programType.GetTypeInfo().GetDeclaredMethod("BuildWebHost");
if (buildWebHostMethod == null)
{
_reporter.WriteVerbose(DesignStrings.NoBuildWebHost(programType.DisplayName()));

return 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;
}

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

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

try
{
var webHost = buildWebHostMethod.Invoke(null, new object[] { args });
var webHost = webHostFactoryResult.WebHostFactory(args);
var webHostType = webHost.GetType();
var servicesProperty = webHostType.GetTypeInfo().GetDeclaredProperty("Services");
var services = (IServiceProvider)servicesProperty.GetValue(webHost);
Expand All @@ -88,7 +91,7 @@ private IServiceProvider CreateFromBuildWebHost(string[] args)
}

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

return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Sources" />
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersVersion)" PrivateAssets="All">
<!-- Build-only assets are exempt from using a version from lineups. -->
<NoWarn>KRB4002</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ namespace Microsoft.EntityFrameworkCore.TestUtilities
{
public class TestAppServiceProviderFactory : AppServiceProviderFactory
{
private readonly Type _programType;

public TestAppServiceProviderFactory(Assembly startupAssembly, Type programType, IOperationReporter reporter = null)
: base(startupAssembly, reporter ?? new TestOperationReporter())
=> _programType = programType;

protected override Type FindProgramClass()
=> _programType;
: base(startupAssembly, reporter ?? new TestOperationReporter()) { }
}
}
34 changes: 32 additions & 2 deletions test/EFCore.Tests/TestUtilities/MockAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;

Expand All @@ -11,20 +12,49 @@ namespace Microsoft.EntityFrameworkCore.TestUtilities
public class MockAssembly : Assembly
{
public static Assembly Create(params Type[] definedTypes)
=> Create(definedTypes, new MockMethodInfo(definedTypes.First()));

public static Assembly Create(Type[] definedTypes, MethodInfo entryPoint)
{
var definedTypeInfos = definedTypes.Select(t => t.GetTypeInfo()).ToArray();

return new MockAssembly(definedTypeInfos);
return new MockAssembly(definedTypeInfos, entryPoint);
}

public MockAssembly(IEnumerable<TypeInfo> definedTypes)
public MockAssembly(IEnumerable<TypeInfo> definedTypes, MethodInfo entryPoint)
{
DefinedTypes = definedTypes;
EntryPoint = entryPoint;
}

public override MethodInfo EntryPoint { get; }

public override IEnumerable<TypeInfo> DefinedTypes { get; }

public override AssemblyName GetName()
=> new AssemblyName(nameof(MockAssembly));

private class MockMethodInfo : MethodInfo
{
public MockMethodInfo(Type declaringType)
{
DeclaringType = declaringType;
}

public override Type DeclaringType { get; }

public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotImplementedException();
public override RuntimeMethodHandle MethodHandle => throw new NotImplementedException();
public override MethodAttributes Attributes => throw new NotImplementedException();
public override string Name => throw new NotImplementedException();
public override Type ReflectedType => throw new NotImplementedException();
public override MethodInfo GetBaseDefinition() => throw new NotImplementedException();
public override object[] GetCustomAttributes(bool inherit) => throw new NotImplementedException();
public override object[] GetCustomAttributes(Type attributeType, bool inherit) => throw new NotImplementedException();
public override MethodImplAttributes GetMethodImplementationFlags() => throw new NotImplementedException();
public override ParameterInfo[] GetParameters() => throw new NotImplementedException();
public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) => throw new NotImplementedException();
public override bool IsDefined(Type attributeType, bool inherit) => throw new NotImplementedException();
}
}
}