.NET ConfigurationErrorsException:无法识别的配置节<启动>

时间:2019-09-30 14:09:20

标签: c# dependency-injection .net-core

这是情况: 我创建了一个项目A,它带有一个名为QmlNet的UI库。 我按照教程进行操作,等等,等等。该项目运行良好。 (这些功能可以讨论)

然后我基于项目A(减去UI库)创建了一个项目B,以集成另一个项目(Eto.Forms)

  • 我将TargetPlatformnetcoreapp2.2切换为net461(我尝试使用netcoreapp2.2,但问题仍然存在)。
  • OutputTypeExe更改为WinExe(我试图坚持使用Exe,但仍然有问题)
  • 还原必须通过nuget完成(dotnet还原不会还原packages.config)-这里我不使用Visual Studio,所以我不认为我对此感到担心。
  • 构建必须使用msbuild而不是dotnet builddotnet build不支持CodeTaskFactory

现在,没有在代码中包含新的UI库,而是像这样运行,我得到了这个错误:

System.TypeInitializationException: The type initializer for 'System.Transactions.TransactionManager' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system. ---> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section <startup>

从中获取错误的文件位于./bin/Debug/net461/myapp.app/Contents/MonoBundle/myapp.exe.config

文件的前5行是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <...>
</configuration>

完整的堆栈跟踪在这里:STACK_TRACE

Program.cs

中有几行有趣的代码
using System.Threading.Tasks;
using B129.Database;
using B129.Database.Seed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Data.Sqlite;

namespace B129
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            // Dependency injection.
            var serviceCollection = new ServiceCollection();

            var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "B129.db" };
            var connectionString = connectionStringBuilder.ToString();

            serviceCollection.ConfigureServices();
            serviceCollection.ConfigureDatabase(connectionString);

            var serviceProvider = serviceCollection.BuildServiceProvider();

            // Seed database. Do NOT dispose dbContext, that would destroy our only dbContext singleton instance.

            // it crashes just below :
            var dbContext = serviceProvider.GetRequiredService<B129DbContext>();
            await dbContext.EnsureSeedData();
        }
    }
}

ConfigureServices和ConfigureDatabase是扩展,在此处定义:

    internal static class Bootstrapper
    {
        internal static void ConfigureServices(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSingleton<IAccountRepository, AccountRepository>();
            serviceCollection.AddSingleton<IAccountService, AccountService>();
            serviceCollection.AddSingleton<AccountsController>();
        }

        internal static void ConfigureDatabase(this IServiceCollection serviceCollection, string connectionString)
        {
            serviceCollection.AddDbContext<B129DbContext>(options => options.UseSqlite(connectionString), ServiceLifetime.Singleton);
        }
    }

DbContext就在这里:

using Microsoft.EntityFrameworkCore;
using B129.Database.Mapping;
using B129.Database.Models;

namespace B129.Database
{
    public class B129DbContext : DbContext
    {
        public B129DbContext(DbContextOptions<B129DbContext> options) : 
            base(options)
        {
            // If I comment this line, it does not crash.
            Database.EnsureCreated();
        }

        public DbSet<Account> Accounts { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            AccountMapping.Map(modelBuilder);

            base.OnModelCreating(modelBuilder);
        }
    }
}

如评论中所述,通过用EnsureCreated注释行,不会引发任何错误。

堆栈跟踪非常奇怪,告诉我我的计算机上没有路径(我在Mac上),并且错误本身非常模糊,因为startup是有效的配置节。 / p>

我全部在VSCode上运行。

这是环境问题吗?还是目标问题?

感谢阅读。

1 个答案:

答案 0 :(得分:0)

Microsoft.EntityFrameworkCore在net4xx目标上不起作用。它仅适用于netstandard2.0

我读到netstd应该是net47的一部分,但是通过升级到net471,它可以编译,但是仍然会由于某些(不是)随机的TypeTransaction错误而崩溃。

我发现的解决方案是停止在项目中使用Sqlite,因为已经花了整整3天的时间了。

祝那些必须面对这个问题的人好运。