Skip to content

Example code without BuildWebHost breaks Entity Framework 2.0 #10

Closed
@janpieterz

Description

@janpieterz

Took me half a morning before I figured out what was exactly going on.
My Identity Server project used the provided way here to create the host, but Entity Framework relies on the BuildWebHost method to analyse it for contexts.

Let me know if you'd like me to change the sample and docs here to below, I believe it contains all that's needed for a valid Serilog + AspNetCore flow.

Program.cs:

public class Program
    {
        public static IConfiguration Configuration { get; set; }
        public static int Main(string[] args)
        {
            Configuration = CreateConfiguration();

            Logging.Config(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development");

            try
            {
                Log.Information("Starting up application");

                var host = BuildWebHost(args);

                host.Run();

                return 0;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return 1;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        private static IConfiguration CreateConfiguration()
        {
            return new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .Build();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(CreateConfiguration())
                .UseSerilog()
                .UseStartup<Startup>()
                .Build();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions