diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs index 9d868fe532..305888997d 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingUtilities.cs @@ -10,19 +10,6 @@ namespace Microsoft.AspNet.Hosting.Internal { public static class HostingUtilities { - internal static Tuple SplitTypeName(string identifier) - { - string typeName = null; - var assemblyName = identifier.Trim(); - var parts = identifier.Split(new[] { ',' }, 2); - if (parts.Length == 2) - { - typeName = parts[0].Trim(); - assemblyName = parts[1].Trim(); - } - return new Tuple(typeName, assemblyName); - } - public static string GetWebRoot(string applicationBasePath) { var webroot = applicationBasePath; diff --git a/src/Microsoft.AspNet.Hosting/Server/IServerLoader.cs b/src/Microsoft.AspNet.Hosting/Server/IServerLoader.cs index abf67b4263..3902135cab 100644 --- a/src/Microsoft.AspNet.Hosting/Server/IServerLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Server/IServerLoader.cs @@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Hosting.Server { public interface IServerLoader { - IServerFactory LoadServerFactory(string serverName); + IServerFactory LoadServerFactory(string assemblyName); } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Server/ServerLoader.cs b/src/Microsoft.AspNet.Hosting/Server/ServerLoader.cs index 51cdecfe36..367f7bea05 100644 --- a/src/Microsoft.AspNet.Hosting/Server/ServerLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Server/ServerLoader.cs @@ -4,8 +4,8 @@ using System; using System.Linq; using System.Reflection; -using Microsoft.AspNet.Hosting.Internal; using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Hosting.Server { @@ -18,59 +18,29 @@ namespace Microsoft.AspNet.Hosting.Server _services = services; } - public IServerFactory LoadServerFactory(string serverFactoryIdentifier) + public IServerFactory LoadServerFactory([NotNull] string assemblyName) { - if (string.IsNullOrEmpty(serverFactoryIdentifier)) + if (string.IsNullOrEmpty(assemblyName)) { - throw new ArgumentException(string.Empty, nameof(serverFactoryIdentifier)); + throw new ArgumentException(string.Empty, nameof(assemblyName)); } - var nameParts = HostingUtilities.SplitTypeName(serverFactoryIdentifier); - var typeName = nameParts.Item1; - var assemblyName = nameParts.Item2; - var assembly = Assembly.Load(new AssemblyName(assemblyName)); if (assembly == null) { throw new Exception(string.Format("The assembly {0} failed to load.", assemblyName)); } - Type type = null; - Type interfaceInfo; - if (string.IsNullOrEmpty(typeName)) + var serverTypeInfo = assembly.DefinedTypes.Where( + t => t.ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory))) != null) + .FirstOrDefault(); + + if (serverTypeInfo == null) { - foreach (var typeInfo in assembly.DefinedTypes) - { - interfaceInfo = typeInfo.ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory))); - if (interfaceInfo != null) - { - type = typeInfo.AsType(); - } - } - - if (type == null) - { - throw new Exception(string.Format("The type {0} failed to load.", typeName ?? "")); - } - } - else - { - type = assembly.GetType(typeName); - - if (type == null) - { - throw new Exception(String.Format("The type {0} failed to load.", typeName ?? "")); - } - - interfaceInfo = type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory))); - - if (interfaceInfo == null) - { - throw new Exception(string.Format("The type '{0}' does not implement the '{1}' interface.", type, typeof(IServerFactory).FullName)); - } + throw new InvalidOperationException($"No server type found that implements IServerFactory in assembly: {assemblyName}."); } - return (IServerFactory)ActivatorUtilities.GetServiceOrCreateInstance(_services, type); + return (IServerFactory)ActivatorUtilities.GetServiceOrCreateInstance(_services, serverTypeInfo.AsType()); } } } \ No newline at end of file