0

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.

1

1 Answer 1

0

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;
}
4
  • This looks good but I'm having trouble putting it to work. Where should I handle this event? I'm trying on the Global.asax Applicatimon_Start but it never gets fired. Can I handle this event inside my VirtualPathProvider?
    – AlexCode
    Jul 26, 2011 at 23:51
  • It should happen in application start (before your VirtualPathProvider gets added). So the event never fires? What if you bind to the TypeResolve event too? Does that get fired?
    – Jeff
    Jul 27, 2011 at 13:54
  • No, nothing gets called for my type. I've tried to catch other relevant events like the TypeResolve that you mentioned and the AssemblyLoad. The last one is called but never for my type either (which makes sense as I haven't load it).
    – AlexCode
    Jul 27, 2011 at 19:46
  • Only the VirtualPathProvider I believe won't be of much use. I'm doing this on a demo VS2010 project, here's the link to it on my Dropbox: db.tt/BBAq7IK, Thanks!
    – AlexCode
    Jul 27, 2011 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.