C# Dotnet运行和System.TypeLoadException
今天我的申请有一个很奇怪的问题。在我将其移动到另一个文件夹并调用命令dotnet run后,应用程序给了我一个错误:C# Dotnet运行和System.TypeLoadException,c#,asp.net,asp.net-core-mvc,.net-core,asp.net-core-2.0,C#,Asp.net,Asp.net Core Mvc,.net Core,Asp.net Core 2.0,今天我的申请有一个很奇怪的问题。在我将其移动到另一个文件夹并调用命令dotnet run后,应用程序给了我一个错误: Unhandled Exception: System.TypeLoadException: Method 'get_Name' in type 'Micros oft.Extensions.Options.ConfigurationChangeTokenSource`1' from assembly 'Microsof t.Extensions.Options.Configu
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();
}
}
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:false,reloadOnChange:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddMvc()
.AddJsonOptions(x=>x.SerializerSettings.Formatting=Formatting.Indented);
services.AddCors(o=>o.AddPolicy(“DefaultPolicy”,builder=>
{
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.AllowAnyHeader();
}));
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.AddSingleton(AutoMapperConfig.Initialize());
services.Configure(Configuration.GetSection(“app”));
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
//loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
//loggerFactory.AddDebug();
//应用程序UseCors(“默认政策”);
app.UseCors(builder=>
建筑商。来源(“http://localhost:5050AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
loggerFactory.AddNLog();
app.AddNLogWeb();
环境配置nlog(“nlog.config”);
种子数据(app);
app.UseMvc();
}
私有数据(IApplicationBuilder应用程序)
{
var settings=app.ApplicationServices.GetService();
if(settings.Value.SeedData)
{
var dataInitializer=app.ApplicationServices.GetService();
dataInitializer.SeedAsync();
}
}
}
我试图对第61行进行注释,该行是
services.Configure<AppSettings>(Configuration.GetSection("app"));
services.Configure(Configuration.GetSection(“app”);
但是一个应用程序给了我另一个TypeLoadException。
当我把它推到github时也是一样。看起来加载设置时出现了问题。 你如何运行这个应用程序?入口在哪里? 您是使用cd“进入”项目中的目录还是使用“dotnet运行my/test/project”?你可以换个房间
.SetBasePath(env.ContentRootPath)
到
GetCurrentDirectory()获取您当前所在的目录,因此您必须位于带有.csproj的目录中才能使用它,或者您可以选择第二个选项。它在输出中搜索设置文件。我在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" />
编辑
最后,新的NLog.Extensions.Logging包版本1.0.0-rtm-beta6出现问题。我回到了1.0.0-rtm-beta5版本。然后应用程序开始工作。谢谢您的回答,但现在应用程序没有看到我的appsettings文件。未找到配置文件“appsettings.json”,并且不是可选的。我正在应用程序文件夹中使用cd命令运行程序。
<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" />
2017-09-28 10:52:28.8575|WARN|Microsoft.AspNetCore.Server.Kestrel|Connection processing ended abnormally.