TypeInitializationException: инициализатор типа для 'System.Data.Entity.Internal.AppConfig' вызвал исключение - PullRequest
0 голосов
/ 26 декабря 2018

В настоящее время я пытаюсь создать ASP.NET Core API для приложения под названием «Приложение».Он содержит клиентов и продукты.Когда я делаю запрос на получение всех клиентов через мой CustomerController, я получаю следующую ошибку:

System.TypeInitializationException HResult = 0x80131534 Сообщение = Инициализатор типа для 'System.Data.Entity.Internal.AppConfig'бросил исключение.Source = EntityFramework StackTrace: в System.Data.Entity.Internal.LazyInternalConnection..ctor (контекст DbContext, String nameOrConnectionString) в System.Data.Entity.DbContext..ctor (String nameOrConnectionString) в Application.TestAPI.Logic.Dxt.App...ctor (String connString) в C: \ Users \ Chris \ Source \ repos \ Application.TestAPI \ Application.TestAPI.Logic \ Database \ ApplicationDatabaseContext.cs: строка 9 в Application.TestAPI.Logic.Init. <> c__DisplayClass0_0.b__0 (IServiceProvider _) в C: \ Users \ Chris \ Source \ repos \ Application.TestAPI \ Application.TestAPI.Logic \ Init.cs: строка 19 в Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory (FactoryCallSiteScopePiderScopePiderScopePiderScopePiderScopePiderScopePiderScopePiderScopePiderScopePiderScopePiderScopeSiderScopeCallScopePiderScopePiderScopeSiteScopePiderSourceSCriceCenterScopeSiderScopeCallScopeSiderScopeCallScopeSiderScopeCiteScopeSiderScopeCallScopeSiderScopeCallScopeSiderScopeCenterScopeSiderScopeCallScopeSiderScopeCricePecerSiderSCallSiderScopePCenterSiteScopePecerSiderCallScopeSiderSCriceCriceCroneServiceSiderSCallScopePerviceCallScopeSiderCallSider фабрикаCallC)область) в Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite (IServiceCallSite callSite, аргумент TArgument) в Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructили (ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope) в Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite (IServiceCallSite, callSite, аргумент TArgument) в scope.в Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite (IServiceCallSite callSite, аргумент TArgument) в Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine .Exjection.Inc.ServiceLookup.ServiceProviderEngine.GetService (Тип serviceType, ServiceProviderEngineScope serviceProviderEngineScope) в Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService (Тип serviceType) в MicrosoftdencyExjec.tion.ActivatorUtilities.GetService (IServiceProvider зр, тип тип, тип requiredBy, булева isDefaultParameterRequired) при Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider. <> c__DisplayClass4_0.b__0 (ControllerContext controllerContext) при Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider. <> c__DisplayClass5_0.g__CreateController | 0 (ControllerContext controllerContext) в Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next (State & next, Scope & scope, Object & state, Boolean & isCompleted) в Microsoft.AspNetCternalInrol.Conv.Внутреннее исключение 1: FileNotFoundException: не удалось загрузить файл или сборку 'System.Configuration.ConfigurationManager, версия = 0.0.0.0, культура = нейтральная, PublicKeyToken = cc7b13ffcd2ddd51'.Det går inte att hitta filen.

Код / класс, в котором происходит сбой:

using Application.TestAPI.Logic.Models;
using System.Data.Entity;
using System.Diagnostics;

namespace Application.TestAPI.Logic.Database
{
    public class ApplicationDatabaseContext : DbContext
    {
        public ApplicationDatabaseContext(string connString) : base(connString)
        {
           //Database.Log = s => Debug.WriteLine(s);
        }

    public DbSet<ApplicationCustomer> Customers { get; set; }
    public DbSet<ApplicationContract> Contracts { get; set; }
    }
}

Строка подключения находится не в моем App.config для библиотеки классов, а вмой appsettings.json в API-проекте.

Параметр connString является допустимой строкой соединения, которая, как мне известно, работает и передается из следующей строки кода:

services.AddScoped(_ => new ApplicationDatabaseContext(applicationConfiguration.GetValue<string>("ConnectionString"))); 

My * .csproj-файл выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{6347E02C-B759-404B-8E2F-20B67E2524B2}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Application.TestAPI.Logic</RootNamespace>
    <AssemblyName>Application.TestAPI.Logic</AssemblyName>
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.EntityFrameworkCore">
      <HintPath>..\..\..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore\2.1.1\lib\netstandard2.0\Microsoft.EntityFrameworkCore.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Configuration.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Configuration.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Configuration.Binder, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Configuration.Binder.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Binder.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Options, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Options.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Options.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Options.ConfigurationExtensions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Options.ConfigurationExtensions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Options.ConfigurationExtensions.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Extensions.Primitives, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Extensions.Primitives.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll</HintPath>
    </Reference>
    <Reference Include="Application.Core, Version=6.6.2.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\Application.Core.6.6.2\lib\net46\Application.Core.dll</HintPath>
    </Reference>
    <Reference Include="Application.Core.Dto, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\Application.Core.6.6.2\lib\net46\Application.Core.Dto.dll</HintPath>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="StructureMap">
      <HintPath>Lib\StructureMap.dll</HintPath>
    </Reference>
    <Reference Include="StructureMap.Net4">
      <HintPath>Lib\StructureMap.Net4.dll</HintPath>
    </Reference>
    <Reference Include="StructureMap.Web">
      <HintPath>Lib\StructureMap.Web.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Buffers.4.5.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
    </Reference>
    <Reference Include="System.ComponentModel.DataAnnotations" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Core" />
    <Reference Include="System.Data.OracleClient" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.IO.Pipelines, Version=4.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.IO.Pipelines.4.5.2\lib\netstandard2.0\System.IO.Pipelines.dll</HintPath>
    </Reference>
    <Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
    </Reference>
    <Reference Include="System.Net" />
    <Reference Include="System.Numerics" />
    <Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.AccessControl, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Security.AccessControl.4.5.0\lib\net461\System.Security.AccessControl.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Permissions, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Security.Permissions.4.5.0\lib\net461\System.Security.Permissions.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Principal.Windows, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Security.Principal.Windows.4.5.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
    </Reference>
    <Reference Include="System.ServiceProcess" />
    <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
    </Reference>
    <Reference Include="System.Transactions" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Configuration\ApplicationOptions.cs" />
    <Compile Include="Connector\ApplicationConnector.cs" />
    <Compile Include="Database\CustomerConfiguration.cs" />
    <Compile Include="Database\ApplicationDatabaseContext.cs" />
    <Compile Include="Init.cs" />
    <Compile Include="Models\Customer.cs" />
    <Compile Include="Models\CustomerDto.cs" />
    <Compile Include="Models\Filter.cs" />
    <Compile Include="Models\ApplicationContract.cs" />
    <Compile Include="Models\ApplicationCustomer.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Services\CustomerService.cs" />
    <Compile Include="Services\FilterService.cs" />
    <Compile Include="Services\ICustomerService.cs" />
    <Compile Include="Services\AppliationCustomerService.cs" />
    <Compile Include="Translators\CustomerTranslator.cs" />
    <Compile Include="Translators\ApplicationCustomerTranslator.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="app.config">
      <SubType>Designer</SubType>
    </None>
    <None Include="packages.config" />
  </ItemGroup>
  <ItemGroup />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Я попытался изменить структуру моего App.config, переустановить EntityFramework, жестко запрограммировать строку подключения и установить System.Configuration.ConfigurationManager через NuGet.Пока ничего не получалось.

Любые предложения / объяснения, что может быть причиной этого?

С уважением,

Крис

...