コード例 #1
0
        public void CreateWebHostBuilderPattern__InvalidReturnType_CanFindServiceProvider()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(CreateWebHostBuilderInvalidSignature.Program).Assembly);

            Assert.NotNull(factory);
            Assert.Null(factory(Array.Empty <string>()));
        }
コード例 #2
0
        public void NoSpecialEntryPointPatternHangs()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPatternHangs.Program).Assembly, s_WaitTimeout);

            Assert.NotNull(factory);
            Assert.Throws <InvalidOperationException>(() => factory(Array.Empty <string>()));
        }
コード例 #3
0
        public void CreateWebHostBuilderPattern_CanFindServiceProvider()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(CreateWebHostBuilderPatternTestSite.Program).Assembly);

            Assert.NotNull(factory);
            Assert.IsAssignableFrom <IServiceProvider>(factory(Array.Empty <string>()));
        }
コード例 #4
0
        public void NoSpecialEntryPointPatternMainNoArgs()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPatternMainNoArgs.Program).Assembly, s_WaitTimeout);

            Assert.NotNull(factory);
            Assert.IsAssignableFrom <IServiceProvider>(factory(Array.Empty <string>()));
        }
コード例 #5
0
        public void CreateHostBuilderPattern__Invalid_CantFindServiceProvider()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(CreateHostBuilderInvalidSignature.Program).Assembly, s_WaitTimeout);

            Assert.NotNull(factory);
            Assert.Throws <InvalidOperationException>(() => factory(Array.Empty <string>()));
        }
コード例 #6
0
        public void TopLevelStatements()
        {
            var assembly = Assembly.Load("TopLevelStatements");
            var factory  = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);

            Assert.NotNull(factory);
            Assert.IsAssignableFrom <IServiceProvider>(factory(Array.Empty <string>()));
        }
コード例 #7
0
        public void TopLevelStatementsTestsTimeout()
        {
            var assembly = Assembly.Load("TopLevelStatementsTestsTimeout");
            var factory  = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);

            Assert.NotNull(factory);
            Assert.Throws <InvalidOperationException>(() => factory(Array.Empty <string>()));
        }
コード例 #8
0
    private IServiceProvider?CreateFromHosting(string[] args)
    {
        _reporter.WriteVerbose(DesignStrings.FindingHostingServices);

        var serviceProviderFactory = HostFactoryResolver.ResolveServiceProviderFactory(_startupAssembly);

        if (serviceProviderFactory == null)
        {
            _reporter.WriteVerbose(DesignStrings.NoCreateHostBuilder);

            return(null);
        }

        var aspnetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        var dotnetEnvironment     = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
        var environment           = aspnetCoreEnvironment
                                    ?? dotnetEnvironment
                                    ?? "Development";

        if (aspnetCoreEnvironment == null)
        {
            Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
        }

        if (dotnetEnvironment == null)
        {
            Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", environment);
        }

        _reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));

        try
        {
            var services = serviceProviderFactory(args);
            if (services == null)
            {
                _reporter.WriteWarning(DesignStrings.MalformedCreateHostBuilder);

                return(null);
            }

            _reporter.WriteVerbose(DesignStrings.UsingHostingServices);

            return(services.CreateScope().ServiceProvider);
        }
        catch (Exception ex)
        {
            if (ex is TargetInvocationException)
            {
                ex = ex.InnerException !;
            }

            _reporter.WriteVerbose(ex.ToString());
            _reporter.WriteWarning(DesignStrings.InvokeCreateHostBuilderFailed(ex.Message));

            return(null);
        }
    }
コード例 #9
0
        public void NoSpecialEntryPointPatternThrows()
        {
            using var _ = RemoteExecutor.Invoke(() =>
            {
                var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPatternThrows.Program).Assembly, s_WaitTimeout);

                Assert.NotNull(factory);
                Assert.Throws <Exception>(() => factory(Array.Empty <string>()));
            });
        }
コード例 #10
0
        public void NoSpecialEntryPointPattern()
        {
            using var _ = RemoteExecutor.Invoke(() =>
            {
                var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPattern.Program).Assembly, s_WaitTimeout);

                Assert.NotNull(factory);
                Assert.IsAssignableFrom <IServiceProvider>(factory(Array.Empty <string>()));
            });
        }
コード例 #11
0
        public void ApplicationNameSetFromArgument()
        {
            Assembly         assembly        = Assembly.Load("ApplicationNameSetFromArgument");
            var              factory         = HostFactoryResolver.ResolveServiceProviderFactory(assembly, s_WaitTimeout);
            IServiceProvider?serviceProvider = factory(Array.Empty <string>());

            var configuration = (IConfiguration)serviceProvider.GetService(typeof(IConfiguration));

            Assert.Contains("ApplicationNameSetFromArgument", configuration["applicationName"]);
        }
コード例 #12
0
    public int Process()
    {
        var assemblyName   = new AssemblyName(_context.AssemblyName);
        var assembly       = Assembly.Load(assemblyName);
        var entryPointType = assembly.EntryPoint?.DeclaringType;

        if (entryPointType == null)
        {
            _reporter.WriteError(Resources.FormatMissingEntryPoint(_context.AssemblyPath));
            return(3);
        }

        try
        {
            var serviceFactory = HostFactoryResolver.ResolveServiceProviderFactory(assembly);
            if (serviceFactory == null)
            {
                _reporter.WriteError(Resources.FormatMethodsNotFound(
                                         HostFactoryResolver.BuildWebHost,
                                         HostFactoryResolver.CreateHostBuilder,
                                         HostFactoryResolver.CreateWebHostBuilder,
                                         entryPointType));

                return(4);
            }

            var services = serviceFactory(Array.Empty <string>());
            if (services == null)
            {
                _reporter.WriteError(Resources.FormatServiceProviderNotFound(
                                         typeof(IServiceProvider),
                                         HostFactoryResolver.BuildWebHost,
                                         HostFactoryResolver.CreateHostBuilder,
                                         HostFactoryResolver.CreateWebHostBuilder,
                                         entryPointType));

                return(5);
            }

            var success = GetDocuments(services);
            if (!success)
            {
                return(6);
            }
        }
        catch (Exception ex)
        {
            _reporter.WriteError(ex.ToString());
            return(7);
        }

        return(0);
    }
コード例 #13
0
        private IServiceProvider CreateFromHosting(string[] args)
        {
            _reporter.WriteVerbose(DesignStrings.FindingHostingServices);

            var serviceProviderFactory = HostFactoryResolver.ResolveServiceProviderFactory(_startupAssembly);
            if (serviceProviderFactory == null)
            {
                _reporter.WriteVerbose(DesignStrings.NoCreateHostBuilder);

                return null;
            }

            // TODO: Remove when dotnet/cli#6617 is fixed
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            if (environment == null)
            {
                environment = "Development";
                Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);
            }

            _reporter.WriteVerbose(DesignStrings.UsingEnvironment(environment));

            try
            {
                var services = serviceProviderFactory(args);
                if (services == null)
                {
                    _reporter.WriteWarning(DesignStrings.MalformedCreateHostBuilder);

                    return null;
                }

                _reporter.WriteVerbose(DesignStrings.UsingHostingServices);

                return services.CreateScope().ServiceProvider;
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException)
                {
                    ex = ex.InnerException;
                }

                _reporter.WriteVerbose(ex.ToString());
                _reporter.WriteWarning(DesignStrings.InvokeCreateHostBuilderFailed(ex.Message));

                return null;
            }
        }
コード例 #14
0
        public void NoSpecialEntryPointPatternCanRunInParallel()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(NoSpecialEntryPointPattern.Program).Assembly, s_WaitTimeout);

            Assert.NotNull(factory);

            var tasks = new Task <IServiceProvider> [30];
            int index = 0;

            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[index++] = Task.Run(() => factory(Array.Empty <string>()));
            }

            Task.WaitAll(tasks);

            foreach (var t in tasks)
            {
                Assert.IsAssignableFrom <IServiceProvider>(t.Result);
            }
        }
コード例 #15
0
        public void BuildWebHostPattern__Invalid_CantFindServiceProvider()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(BuildWebHostInvalidSignature.Program).Assembly);

            Assert.NotNull(factory);
        }
コード例 #16
0
        public void CreateHostBuilderPattern__Invalid_CantFindServiceProvider()
        {
            var factory = HostFactoryResolver.ResolveServiceProviderFactory(typeof(CreateHostBuilderInvalidSignature.Program).Assembly);

            Assert.Null(factory);
        }