find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Error occurred while access Microsoft.Extensions.Hosting Services. Could not parse JSON file

Error occurred while access Microsoft.Extensions.Hosting Services. Could not parse JSON file

PM> add-migration add

Build started...

Build succeeded.

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Could not parse the JSON file.

Unable to create an object of type 'MyDBContent'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728.

Here is my DbContent.cs 

public class MyDBContent:DbContext
    {
        private IConfigurationRoot _config;
        public MyDBContent(IConfigurationRoot config, DbContextOptions options) : base(options)
        {
            _config = config;
        }
        public DbSet<Category> Category { get; set; }
        public DbSet<SubCategory> SubCategory { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer(_config["ConnectionStrings:DefaultConnection"]);//connection string get by appsetting.json
        }
    }

Answer

Option 1.

Open your Startup.cs and past this code as given below.

       private IConfigurationRoot _config;
        public Startup(IConfiguration configuration,IWebHostEnvironment env/*add this here*/)
        {
            Configuration = configuration;
            var ConfigBuilder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appsettings.json");
            _config = ConfigBuilder.Build();
        }
        public IConfiguration Configuration { get; }

and aso past this code in ConfigureServices function as given below.

      //This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(_config);//here
            services.AddDbContext<MyDBContent>();//call here like that 
            services.AddControllersWithViews();         }
and your appsetting.json like as 
{
  "ConnectionStrings": {
    "windowsAuthentication": true,
    "DefaultConnection": "Data Source=<your-sesrver_name>;Database=<datbase_name>;Trusted_Connection=True;MultipleActiveResultSets=true" //this is windows athenticaiton    },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

And run Add-Migration in your project it will woked.

Option 2.

If It turns out that your appsettings.json was missing a closing curly brace. Adding that in fixed the problem

Optioni 3.

In the some  similar issue when following This MVC guide. If anyone is having similar issue, check your appsettings.json file that your are not missing something like as comma(,),open close '{' or '} something like that.

Selena  Kandakar

Selena Kandakar

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response