从2.0.0-preview1-final升级到2.0.0-preview2-final后的System.MissingMethodException

时间:2017-07-18 04:25:46

标签: c# asp.net-core .net-core

我刚刚更新了我的visual studio以预览4并将我的所有软件包从Core 2.0 Preview 1 Final更新到Core 2.0 Preview 2 Final。所有升级后,下面是我的csproj文件。

<ItemGroup>
    <PackageReference Include="HtmlAgilityPack" Version="1.5.2-beta2" />
    <PackageReference Include="Humanizer.Core" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0-preview2-final" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.0-preview2-final" />
    <PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="2.0.0-dev-10164" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="2.4.0" />
    <PackageReference Include="Serilog.Sinks.Literate" Version="3.0.1-dev-00044" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
    <PackageReference Include="AutoMapper" Version="6.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.0.0-preview2-final" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.0-preview2-final" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
  </ItemGroup>

以下是原始异常详情。

System.MissingMethodException: Method not found: 'Microsoft.Extensions.Configuration.IConfiguration Microsoft.Extensions.Logging.LoggerFactory.get_Configuration()'.
   at Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter.<>c__DisplayClass0_1.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Program.cs的

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using System.IO;

namespace Phoenix
{
    public class AppProgram
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .ConfigureLogging(factory =>
                {
                    factory.AddConsole();
                    factory.AddDebug();
                })
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Startup.cs(我必须注释掉loggerFactory.AddSeriLog()调用,因为它在更新所有软件包之后就产生了错误。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddSwaggerGen(c =>
    {
        // removed for brevity
    });

    // DI statements have been removed for brevity.

    Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(Configuration)
        .CreateLogger();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //loggerFactory.AddSerilog();

    app.UseMvc();

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

}

根据loggerFactory.AddSerilog();的错误消息中的建议,我尝试在services.AddSeriLog()方法中添加ConfigureServices

我也尝试过安装包Microsoft.Extensions.Logging,但也没有修复。

我还尝试从新模板中替换Program.cs文件的内容,如下所示。这也行不通。

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

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

有人可以帮我解决这个问题吗?

更新

添加包Microsoft.AspNetCore.All解决了这个问题。但是我真的不想添加这个包,因为这会将许多不需要的引用打包到这个包中。

1 个答案:

答案 0 :(得分:1)

必需的包是

Microsoft.Extensions.Logging.Configuration

我添加了解决问题的软件包。

<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.0.0-preview2-final" />