Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet run and System.TypeLoadException

Today I have really strange problem with my application. After I moved it to another folder and called the command dotnet run application gave me an error:

 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

Before I moved it was working.

Here is my Startup class:

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

I tried to comment line 61 which is

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

But an application gave me another TypeLoadException. It's the same thing when i push it to github.

like image 509
Kerni Avatar asked Sep 07 '25 13:09

Kerni


1 Answers

I added two references to my 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" />

And now dotnet run start working, but when I send for example Http.Get request the kestrel send me this message:

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

EDIT

Finnaly application get work It was a problem with new NLog.Extensions.Logging package version 1.0.0-rtm-beta6. I went back to version 1.0.0-rtm-beta5. And application starts work.

like image 53
Kerni Avatar answered Sep 10 '25 05:09

Kerni