Skip to content

Commit

Permalink
Update AppServiceProviderFactory to use WebHostFactoryResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed Oct 20, 2017
1 parent 16e99c5 commit 28a5815
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
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)
{
_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();
}
}
}

0 comments on commit 28a5815

Please sign in to comment.