gpt4 book ai didi

c# - Dotnet 运行和 System.TypeLoadException

转载 作者:行者123 更新时间:2023-11-30 16:42:39 28 4
gpt4 key购买 nike

今天我的应用程序遇到了一个非常奇怪的问题。在我将它移动到另一个文件夹并调用命令 dotnet run application 后,出现错误:

 Unhandled Exception: System.TypeLoadException: Method 'get_Name' in type 'Micros
oft.Extensions.Options.ConfigurationChangeTokenSource`1' from assembly 'Microsof
t.Extensions.Options.ConfigurationExtensions, Version=1.1.2.0, Culture=neutral,
PublicKeyToken=adb9793829ddae60' does not have an implementation.
at Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollec
tionExtensions.Configure[TOptions](IServiceCollection services, IConfiguration c
onfig)
at LicenseManager.Api.Startup.ConfigureServices(IServiceCollection services)
in D:\Projekty\LicenseManager\src\LicenseManager.Api\Startup.cs:line 61
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(ISer
viceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at LicenseManager.Api.Program.Main(String[] args) in D:\Projekty\LicenseManag
er\src\LicenseManager.Api\Program.cs:line 15

在我搬家之前它正在工作。

这是我的启动类:

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

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc()
.AddJsonOptions(x => x.SerializerSettings.Formatting = Formatting.Indented);
services.AddCors(o => o.AddPolicy("DefaultPolicy", builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.AllowAnyHeader();
}));
services.AddScoped<IComputerRepository, InMemoryComputerRepository>();
services.AddScoped<ILicenseRepository, InMemoryLicenseRepository>();
services.AddScoped<ILicenseTypeRepository, InMemoryLicenseTypeRepository>();
services.AddScoped<IRoomRepository, InMemoryRoomRepository>();
services.AddScoped<IUserRepository, InMemoryUserRepository>();
services.AddScoped<IRoomService, RoomService>();
services.AddScoped<ILicenseTypeService, LicenseTypeService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<ILicenseService, LicenseService>();
services.AddScoped<IComputerService, ComputerService>();
services.AddScoped<IDataInitializer,DataInitializer>();
services.AddSingleton(AutoMapperConfig.Initialize());
services.Configure<AppSettings>(Configuration.GetSection("app"));


}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
//loggerFactory.AddDebug();
//app.UseCors("DefaultPolicy");
app.UseCors(builder =>
builder.WithOrigins("http://localhost:5050").AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
loggerFactory.AddNLog();
app.AddNLogWeb();
env.ConfigureNLog("nlog.config");
SeedData(app);

app.UseMvc();

}

private void SeedData(IApplicationBuilder app)
{
var settings = app.ApplicationServices.GetService<IOptions<AppSettings>>();
if(settings.Value.SeedData)
{
var dataInitializer = app.ApplicationServices.GetService<IDataInitializer>();
dataInitializer.SeedAsync();
}
}
}

我试图评论第 61 行

services.Configure<AppSettings>(Configuration.GetSection("app"));

但是应用程序给了我另一个 TypeLoadException。当我把它推到 github 时是一样的。

最佳答案

我在我的 csproj 中添加了两个引用

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />

现在 dotnet run 开始工作了,但是当我发送例如 Http.Get 请求时,红隼向我发送了这条消息:

2017-09-28 10:52:28.8575|WARN|Microsoft.AspNetCore.Server.Kestrel|Connection processing ended abnormally.

编辑

Finnaly 应用程序开始工作这是新 NLog.Extensions.Logging 包版本 1.0.0-rtm-beta6 的问题。我回到了 1.0.0-rtm-beta5 版本。应用程序开始工作。

关于c# - Dotnet 运行和 System.TypeLoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461828/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com