本文介绍了从 2.0.0-preview1-final 升级到 2.0.0-preview2-final 后出现 System.MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just updated my visual studio to preview 4 and updated all my packages from Core 2.0 Preview 1 Final to Core 2.0 Preview 2 Final. Below is my csproj file after all upgradation.

<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>

以下是原始异常详细信息.

Below is the raw exception details.

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()

程序.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() 调用,因为它在我更新所有包后产生错误.

Startup.cs (I had to comment out loggerFactory.AddSeriLog() call because it was generating error just after I updated all the packages.

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(); 的错误消息所建议的,我尝试在我的 ConfigureServices 方法中添加 services.AddSeriLog().

As suggested in the error message from loggerFactory.AddSerilog();, I tried adding services.AddSeriLog() in my ConfigureServices method.

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

I also tried installing package Microsoft.Extensions.Logging, also didn't fix.

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

I also tried replacing content of Program.cs file from the new template as below. This also didn't work.

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

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

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

Can someone please help me fixing this issue?

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

Adding package Microsoft.AspNetCore.All solves the issue. But I really do not want to add this package as this adds many unwanted references packaged into this single package.

推荐答案

所需的包是

Microsoft.Extensions.Logging.Configuration

我添加了解决问题的包.

I added the package which resolved the issue.

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

这篇关于从 2.0.0-preview1-final 升级到 2.0.0-preview2-final 后出现 System.MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:20