jgauffin
jgauffin

Reputation: 101176

Force the usage of a bin folder

I have a larger ASP.NET Core project which means that a lot of DLLs are included in the publish.

Since all of the DLLs are placed in the root folder it's cumbersome to navigate the folder structure (to mange configs etc) due to the sheer amount of files.

Is it possible to tell ASP.NET Core that it should load all assemblies from another folder (bin\)?

Upvotes: 3

Views: 626

Answers (2)

Srinivasan Sekar
Srinivasan Sekar

Reputation: 2119

Hi How about the Managed Extensibility Framework , It allows you load assemblies dynamically.

Use BuildManager to load assemblies dynamically,

string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugins");
foreach (string f in Directory.GetDirectories(pluginPath))
{
    string binPath = Path.Combine(f, "bin");
    if (Directory.Exists(binPath))
    {
        foreach (String file in Directory.GetFiles(binPath, "*.dll"))
        {
            Assembly a = Assembly.LoadFrom(file);
            BuildManager.AddReferencedAssembly(a);
        }
    }

Resolve assemblies using below code,

protected virtual void Application_Start(object sender, EventArgs e)
{

    //...

    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach (var assembly in currentAssemblies)
    {
        if (assembly.FullName == args.Name || assembly.GetName().Name == args.Name)
        {
            return assembly;
        }
    }

    return null;
}

Upvotes: 1

Vova Bilyachat
Vova Bilyachat

Reputation: 19514

I would do it in opposite side. If your problem is just config files then relocate them into config folder and keep them there. As of now dotnet will publish your project + framework (if you use self contained flag).

You can configure aspnetcore to use files

config.AddJsonFile("config/appsettings.json", optional: false, reloadOnChange: false);

So then that folder will be on top and better accessible

Yes in root folder there still be web.config but in my project that file usually is same for all environments. But again it depends where you deploy, because if you deploy to non IIS environment then you don't even need it

Upvotes: 1

Related Questions