Beispiel #1
0
        static void Main(string[] args)
        {
            var hostBuilder = new RuntimeHostBuilder <Startup>();
            var host        = hostBuilder.Build();

            host.Run();
        }
Beispiel #2
0
        public Task<int> Main(string[] args)
        {
            ILogger log = null;
            try
            {
                ApplicationHostOptions options;
                string[] programArgs;
                int exitCode;

                bool shouldExit = ParseArgs(args, out options, out programArgs, out exitCode);
                if (shouldExit)
                {
                    return Task.FromResult(exitCode);
                }
                string traceConfig =
                    string.IsNullOrEmpty(options.Trace) ?
                        Environment.GetEnvironmentVariable(EnvironmentNames.Trace) :
                        options.Trace;
                // Initialize logging
                RuntimeLogging.Initialize(traceConfig, ConfigureLogging);

                // "Program" is a terrible name for a logger and this is the only logger in
                // this namespace :).
                log = RuntimeLogging.Logger("Microsoft.Framework.ApplicationHost");

                log.LogInformation("Application Host Starting");

                // Construct the necessary context for hosting the application
                var builder = RuntimeHostBuilder.ForProjectDirectory(
                    options.ApplicationBaseDirectory,
                    NuGetFramework.Parse(_environment.RuntimeFramework.FullName),
                    _serviceProvider);

                // Configure assembly loading
                builder.Loaders.Add(new ProjectAssemblyLoaderFactory(
                    new LibraryExporter(
                        builder.TargetFramework,
                        builder.PackagePathResolver)));
                builder.Loaders.Add(new PackageAssemblyLoaderFactory(builder.PackagePathResolver));

                if (builder.Project == null)
                {
                    // Failed to load the project
                    Console.Error.WriteLine("Unable to find a project.json file.");
                    return Task.FromResult(3);
                }

                // Boot the runtime
                var host = builder.Build();

                // Get the project and print some information from it
                log.LogInformation($"Project: {host.Project.Name} ({host.Project.BaseDirectory})");

                // Determine the command to be executed
                var command = string.IsNullOrEmpty(options.ApplicationName) ? "run" : options.ApplicationName;
                string replacementCommand;
                if (host.Project.Commands.TryGetValue(command, out replacementCommand))
                {
                    var replacementArgs = CommandGrammar.Process(
                        replacementCommand,
                        GetVariable).ToArray();
                    options.ApplicationName = replacementArgs.First();
                    programArgs = replacementArgs.Skip(1).Concat(programArgs).ToArray();
                }

                if (string.IsNullOrEmpty(options.ApplicationName) ||
                    string.Equals(options.ApplicationName, "run", StringComparison.Ordinal))
                {
                    options.ApplicationName = host.Project.EntryPoint ?? host.Project.Name;
                }

                log.LogInformation($"Executing '{options.ApplicationName}' '{string.Join(" ", programArgs)}'");
                return host.ExecuteApplication(
                    _loaderContainer,
                    _loadContextAccessor,
                    options.ApplicationName,
                    programArgs);
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.LogError($"{ex.GetType().FullName} {ex.Message}", ex);
                }
                Console.Error.WriteLine($"Error loading project: {ex.Message}");
                return Task.FromResult(1);
            }
        }