Exemple #1
0
        public IServerFactory GetServerFactory(string serverFactoryIdentifier)
        {
            if (string.IsNullOrEmpty(serverFactoryIdentifier))
            {
                throw new ArgumentException(string.Empty, "serverFactoryIdentifier");
            }

            var    nameParts    = HostingUtilities.SplitTypeName(serverFactoryIdentifier);
            string typeName     = nameParts.Item1;
            string assemblyName = nameParts.Item2;

            var assembly = Assembly.Load(new AssemblyName(assemblyName));

            if (assembly == null)
            {
                throw new Exception(String.Format("TODO: assembly {0} failed to load message", assemblyName));
            }

            Type type = null;
            Type interfaceInfo;

            if (string.IsNullOrEmpty(typeName))
            {
                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("TODO: type {0} failed to load message", typeName ?? "<null>"));
                }
            }
            else
            {
                type = assembly.GetType(typeName);

                if (type == null)
                {
                    throw new Exception(String.Format("TODO: type {0} failed to load message", typeName ?? "<null>"));
                }

                interfaceInfo = type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(interf => interf.Equals(typeof(IServerFactory)));

                if (interfaceInfo == null)
                {
                    throw new Exception("TODO: IServerFactory interface not found");
                }
            }

            return((IServerFactory)ActivatorUtilities.GetServiceOrCreateInstance(_services, type));
        }
Exemple #2
0
        public static TestServer Create(IServiceProvider provider, Action <IApplicationBuilder> app)
        {
            var appEnv = provider.GetRequiredService <IApplicationEnvironment>();

            var hostingEnv = new HostingEnvironment()
            {
                EnvironmentName = DefaultEnvironmentName,
                WebRoot         = HostingUtilities.GetWebRoot(appEnv.ApplicationBasePath),
            };

            var collection = new ServiceCollection();

            collection.Add(HostingServices.GetDefaultServices());
            collection.AddInstance <IHostingEnvironment>(hostingEnv);

            var appServices = collection.BuildServiceProvider(provider);

            var config = new Configuration();

            return(new TestServer(config, appServices, app));
        }
        public void ReadWebRootFromProjectJson()
        {
            var root = HostingUtilities.GetWebRoot(".");

            Assert.True(root.EndsWith("testroot"));
        }