简体   繁体   中英

Load Assemblies from a folder other than bin

I created a mvc 3 project, the namespace is [POC.MVC.PluginHost]. The controller namespace is [POC.MVC.PluginHost.Controllers]. I created a class library project and change the name space of it to [POC.MVC.PluginHost.Controllers].

class library project code :

namespace POC.MVC.PluginHost.Controllers
{
    public class BasicExampleController : Controller
    {
        public ActionResult Index()
        {
            // Add action logic here
            throw new NotImplementedException();
        }
        public ActionResult Display()
        {

            return Content("");

        }
    }
}

I compile it and copy to bin directory of mvc project, when I browse http://localhost:xxxx/BasicExample/display it works fine, but I want to copy this compiled dll of class library in a other folder like [plugin] but it is not work, it is working only when I copy it to the bin folder of my mvc project. Is there is a way to solve this problem?

edit .....................................

i test in my web.config like :

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <probing privatePath="Plugin" />
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
  </dependentAssembly>
</assemblyBinding>

but this not work !!!!

and i test this But this still does not work .....

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string assembliesDir = "Plugin";

        string aa = System.IO.Path.Combine(assembliesDir, args.Name + ".dll");
        aa = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, aa);
        Assembly asm = Assembly.LoadFrom(aa);
        return asm;
    }

    protected void Application_Start()
    {

        RegisterRoutes(RouteTable.Routes);
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        AppDomain.CurrentDomain.Load("SamplePlg");

        System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider());

    }

See also

C#: Custom assembly directory

You can add additional search paths to your app.config that it looks in to load assemblies. For example

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <probing privatePath="lib" />
  </assemblyBinding>
</runtime>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM