c# - Program.cs 中的 System.MissingMethodException

标签 c# asp.net-core

这个异常只是在创建宿主变量时出现在Program.cs中,我没有更新Program.cs中的任何东西所以我不知道为什么会出现。 我已经尝试重新启动 VS 并删除解决方案中所有项目中的 bin 和 obj。

异常:

Method not found: 'System.Collections.Generic.Dictionary`2 Microsoft.Extensions.Configuration.IConfigurationBuilder.get_Properties()'.

程序.cs:

using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace LC.Smokers
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseApplicationInsights()
                .Build();

            host.Run();
        }
    }
}

Startup.cs:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddSingleton<IActionContextAccessor, Models.ActionContextAccessor>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddSingleton<AppHelper>();
        services.AddTransient<SessionHelper>();

        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            options.Cookie.Name = ".Smokers.Session";
            options.IdleTimeout = TimeSpan.FromHours(2);
        });

        services.AddMvc()
            .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Shop/Error");
        }

        List<CultureInfo> supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("no-NB"),
            new CultureInfo("en-US")
        };

        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("no-NB"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        });

        app.UseSession();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Shop}/{action=Index}/{id?}");
        });
    }
}

最佳答案

原来这是使用 2.0.0 版的 Microsoft.AspNetCore.Session 包的问题,​​而项目的其余部分使用的是 2.0.0 预览版 2 最终版本。

我降级了软件包并且它有效。

关于c# - Program.cs 中的 System.MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45646931/

相关文章:

c# - 编写处理不同消息的单个函数的方法

c# - 如何在 UML 中正式记录 C# 属性?

asp.net-core - 对 SSPI 的调用失败 GSSAPI 操作失败并出现错误 - 提供了无效的状态代码(SPNEGO 找不到协商机制)

angular - 如何在 [FromBody] ASP.NET CORE 2 和 Angular 5 中接收文件

c# - 使用 NEST 进行 Elasticsearch - 异步操作

c# - 向 ASP.NET Web API Controller 添加显式操作路由

c# - 一个mysql连接适用于C#中的所有表单

c# - 从 URL 中隐藏 Controller 名称

c# - 池化的 HttpClient 实例是否保留 CookieContainer?

docker - Visual Studio 2017 预览版 5.0