4
respostas

Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions

Desde semana passada estou com dificuldade mas até agora não conseguir solucionar em recebi nenhuma orientação válida.

Não consigo rodar a aplicação, dá erro na linha

EntidadesContext contexto = new EntidadesContext();

Este é o texto da exception:

System.TypeLoadException was unhandled
  HResult=-2146233054
  Message=Não foi possível carregar o tipo Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions do assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60.
  Source=EntityFramework.Core
  TypeName=Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions
  StackTrace:
       em Microsoft.Data.Entity.DbContext.GetOptions(IServiceProvider serviceProvider)
       em Microsoft.Data.Entity.DbContext..ctor()
       em lojaComEntity.EntidadesContext..ctor()
       em lojaComEntity.Program.Main(String[] args) na D:\_Documentos\Visual Studio 2015\Projects\lojaComEntity\lojaComEntity\Program.cs:linha 14
       em System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       em System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       em Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       em System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       em System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       em System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       em System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Não consegui usar o comando "Add-Migration criaUsuario" e criei a tabela na mão, ao rodar o comando "Add-Migration criaUsuario", surgiu o seguinte erro:

PM> Add-Migration criaUsuario
System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at Microsoft.Data.Entity.Design.Internal.StartupInvoker.ConfigureServices()
   at Microsoft.Data.Entity.Design.DbContextOperations..ctor(ILoggerProvider loggerProvider, String assemblyName, String startupAssemblyName, String environment)
   at Microsoft.Data.Entity.Design.MigrationsOperations..ctor(ILoggerProvider loggerProvider, String assemblyName, String startupAssemblyName, String environment, String projectDir, String rootNamespace)
   at Microsoft.Data.Entity.Design.OperationExecutor.<>c__DisplayClass3_0.<.ctor>b__3()
   at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value()
   at Microsoft.Data.Entity.Design.OperationExecutor.<AddMigrationImpl>d__7.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
   at Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
PM>

O André Chaves me pediu o conteúdo do arquivo project.json, mas esse arquivo não foi gerado, pois o projeto é do tipo console.

Envie o conteúdo do packages.config e do App.config

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework.Commands" version="7.0.0-rc1-final" targetFramework="net452" />
  <package id="EntityFramework.Core" version="7.0.0-rc1-final" targetFramework="net452" />
  <package id="EntityFramework.MicrosoftSqlServer" version="7.0.0-rc1-final" targetFramework="net452" />
  <package id="EntityFramework.Relational" version="7.0.0-rc1-final" targetFramework="net452" />
  <package id="EntityFramework.Relational.Design" version="7.0.0-rc1-final" targetFramework="net452" />
  <package id="Ix-Async" version="1.2.5" targetFramework="net452" />
  <package id="Microsoft.EntityFrameworkCore" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.EntityFrameworkCore.Relational" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.EntityFrameworkCore.SqlServer" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Caching.Abstractions" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Caching.Memory" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Configuration" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Configuration.Abstractions" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Configuration.Binder" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.DependencyInjection" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Logging" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Logging.Abstractions" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.Options" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.OptionsModel" version="1.0.0-rc1-final" targetFramework="net452" />
  <package id="Microsoft.Extensions.Primitives" version="1.1.0" targetFramework="net452" />
  <package id="Microsoft.NETCore.Platforms" version="1.1.0" targetFramework="net452" />
  <package id="NETStandard.Library" version="1.6.1" targetFramework="net452" />
  <package id="Remotion.Linq" version="2.2.0-alpha-001" targetFramework="net452" />
  <package id="System.Collections" version="4.3.0" targetFramework="net452" />
  <package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net452" />
  <package id="System.Collections.Immutable" version="1.3.1" targetFramework="net452" />
  <package id="System.ComponentModel" version="4.3.0" targetFramework="net452" />
  <package id="System.ComponentModel.Primitives" version="4.3.0" targetFramework="net452" />
  <package id="System.ComponentModel.TypeConverter" version="4.3.0" targetFramework="net452" />
  <package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net452" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net452" />
  <package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net452" />
  <package id="System.Diagnostics.Tracing" version="4.3.0" targetFramework="net452" />
  <package id="System.Globalization" version="4.3.0" targetFramework="net452" />
  <package id="System.Interactive.Async" version="3.0.0" targetFramework="net452" />
  <package id="System.IO" version="4.3.0" targetFramework="net452" />
  <package id="System.IO.Compression" version="4.3.0" targetFramework="net452" />
  <package id="System.Linq" version="4.3.0" targetFramework="net452" />
  <package id="System.Linq.Expressions" version="4.3.0" targetFramework="net452" />
  <package id="System.Linq.Queryable" version="4.3.0" targetFramework="net452" />
  <package id="System.Net.Http" version="4.3.0" targetFramework="net452" />
  <package id="System.Net.Primitives" version="4.3.0" targetFramework="net452" />
  <package id="System.ObjectModel" version="4.3.0" targetFramework="net452" />
  <package id="System.Reflection" version="4.3.0" targetFramework="net452" />
  <package id="System.Reflection.Extensions" version="4.3.0" targetFramework="net452" />
  <package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net452" />
  <package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net452" />
  <package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net452" />
  <package id="System.Text.Encoding" version="4.3.0" targetFramework="net452" />
  <package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net452" />
  <package id="System.Text.RegularExpressions" version="4.3.0" targetFramework="net452" />
  <package id="System.Threading" version="4.3.0" targetFramework="net452" />
  <package id="System.Threading.Tasks" version="4.3.0" targetFramework="net452" />
  <package id="System.Threading.Timer" version="4.3.0" targetFramework="net452" />
  <package id="System.Xml.ReaderWriter" version="4.3.0" targetFramework="net452" />
  <package id="System.Xml.XDocument" version="4.3.0" targetFramework="net452" />
</packages>

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="lojaConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\_Documentos\loja.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Binder" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Remotion.Linq" publicKeyToken="fee00910d6e5f53b" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Caching.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Caching.Memory" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.1.0" newVersion="1.2.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
4 respostas

Olá, Cloves!

Um outro aluno da Alura obteve a mesma mensagem de erro que você, e conseguiu resolver instalando estes dois pacotes no Package Manager Console:

Install-Package EntityFramework.MicrosoftSqlServer -Version 7.0.0-rc1-final -Pre
Install-Package EntityFramework.Commands -Version 7.0.0-rc1-final -pre

Boa sorte e bons estudos!

Eu já tenho esses pacotes instalados, como listado no conteúdo do aquivo de packages.config que colei (linhas 3 e 5).

Mesmo assim tentei executá-los novamente e esta mensagem aparece:

PM> Install-Package EntityFramework.MicrosoftSqlServer -Version 7.0.0-rc1-final -Pre
Package 'EntityFramework.MicrosoftSqlServer.7.0.0-rc1-final' already exists in project 'lojaComEntity'
PM> Install-Package EntityFramework.Commands -Version 7.0.0-rc1-final -pre
Package 'EntityFramework.Commands.7.0.0-rc1-final' already exists in project 'lojaComEntity'

Boa tarde, Cloves!

Infelizmente não estou conseguindo reproduzir o erro que você reportou.

O mais provável é que haja um conflito entre os pacotes da sua solução. Isso é um problema bem comum (e frustrante!), com a disseminação de diversas versões de frameworks que existem por aí.

O que eu sugiro é que você crie um projeto novo, instalando somente os dois pacotes que mencionei:

Install-Package EntityFramework.MicrosoftSqlServer -Version 7.0.0-rc1-final -Pre
Install-Package EntityFramework.Commands -Version 7.0.0-rc1-final -pre

Em seguida, siga os passos iniciais do projeto:

  • criação da fonte de dados (banco de dados Sql Server)
  • criação da classe Usuario, em Usuario.cs:
  • criação da classe EntidadesContext
  • programação do método Main da classe Program:
          static void Main(string[] args)
          {
              EntidadesContext contexto = new EntidadesContext();
              Usuario renan = new Usuario()
              {
                  Nome = "Renan",
                  Senha = "123"
              };
              contexto.Usuarios.Add(renan);
              contexto.SaveChanges();
              contexto.Dispose();
          }

Segui esses passos na minha máquina e funcionou. Criei um projeto que você pode baixar do GitHub e verificar também. Baixe do link abaixo e abra o projeto CCValadares.

https://github.com/marcelooliveira/AluraForum/archive/master.zip

Espero que funcione!

Obrigado Marcelo, tentarei recriar o projeto mais tarde.