Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How find bin directory in ASP.NET Core 3.1?

My EF project is in one project and my ASP.NET Core project in another project ,

D:\AspProjects\DatabaseHafez> <=== my ef model is in this folder

D:\AspProjects\Hafez> <=== my aspnet core 3 is in this folder

so each project has one bin folder.

The below builder(ConfigurationBuilder) should have the path of appsettings.json file for reading connections string.so the below has this path =>

D:\AspProjects\DatabaseHafez\bin\Debug\netcoreapp3.1\appsettings.json

but my appsettins.json file in my asp.net core project so after bulilding is will copy to output folder =>

D:\AspProjects\Hafez\bin\Debug\netcoreapp3.1\appsettings.json

so How can i find the output folder path?

public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
    AppDbContext IDesignTimeDbContextFactory<AppDbContext>.CreateDbContext(string[] args)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        IConfigurationRoot configuration = builder.Build();

        var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();

        optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

        var context = new AppDbContext(optionsBuilder.Options);

        context.Database.EnsureCreated();
        return context;
    }
}

Now I want to add migrations But I get an error

D:\AspProjects\DatabaseHafez>dotnet ef migrations add changed98112601  

Build started...  
Build succeeded.  

System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\AspProjects\DatabaseHafez\bin\Debug\netcoreapp3.1\appsettings.json'.

at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at DatabaseHafez.AppDbContextFactory.Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory<DatabaseHafez.AppDbContext>.CreateDbContext(String[] args) in D:\AspProjects\DatabaseHafez\AppDbContextFactory.cs:line 27
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_1.<FindContextTypes>b__9()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\AspProjects\DatabaseHafez\bin\Debug\netcoreapp3.1\appsettings.json'.

Now I want to know how can I find the bin folder of my ASP.NET Core project?

like image 895
mohsen Avatar asked Feb 15 '20 07:02

mohsen


People also ask

What is bin folder in asp net?

The Bin folder is used for managed-code assemblies, not for native-code (unmanaged-code) assemblies. For more information, see Loading C++ Assemblies in ASP.Net. In ASP.NET 2.0 and later versions, you can put strong-named (signed) assemblies in the Bin folder.


1 Answers

Try to get it line this:

var runDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) 

GetCurrentDirectory might also work, but there is this bug here: https://github.com/dotnet/project-system/issues/589 so I would avoid it.

like image 103
Athanasios Kataras Avatar answered Oct 14 '22 02:10

Athanasios Kataras