简体   繁体   中英

ASP.net VirtualPathProvider to dynamically load controls

I'm using VirtualPathProvider to load controls (ascx) that are not present at compile time. So when a certain path structure is requested, the VirtualPathProvider rewrites the path to the ascx and loads the dll that contains the code for that control.

Everything works fine except the dll loading. I can load the assembly but the site can't find it. If I put it on the bin folder everything works fine.

To load the assembly I'm using:

System.Reflection.Assembly.LoadFrom(file.FullName);

How can I load this assembly so it can be used when the ascx is rendered on the page?

Again, I could put the dll on the bin folder of the site but as this is dynamic content I prefer to keep it all as isolated.

So you're calling

System.Reflection.Assembly.LoadFrom(file.FullName);

But it doesn't throw an exception doing that, but still doesn't find your assembly when the ascx file is rendered? Are you sure your ascx file references the fully qualified assembly name?

Chances are you'll need to handle the AssemblyResolve event:

AppDomain.CurrentDomain.AssemblyResolve += OnCurrentDomainAssemblyResolve

private static Assembly OnCurrentDomainAssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "myDynamicAssemblyName") return _myPreviouslyLoadedDynamicAssemblyObtainedFromAssemblyLoadFrom;

    return null;
}

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