0

I have some ASP.NET application which composed of some modules. The web application has not direct reference to these modules. But in some part of its code it Loads those assemblies using following code in MyWebApplication.dll:

// modules: Module1 (Module1.dll), Module2 (Module2.dll), ...
foreach(module in modules)
{        
    Assembly.Load(module)
}

But when I'm running the application, it can't load the module.

To debug it, I checked where the other assemblies are located using:

typeof(SomeType).Assembly.Location

and the result was:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\99db3519\a290da98\assembly\dl3\b95f323e\b38a9c7e_53bdd001

In the following folder

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\99db3519\a290da98\assembly\dl3

there are the other assemblies each one in a separate folder. But it seems that ASP.NET brings just the required dll files here. And it seems that it brings the assemblies that are referenced in some way from the WebApplication.

Unfortunately my module assemblies are not referenced from WebApplication as they're being loaded dynamically at run time.

Question: How can I say to ASP.NET to copy and bring my modules too, so I can load them at run time?

mehrandvd
  • 8,806
  • 12
  • 64
  • 111
  • Why can't you load them from original location (probably root-of-the-site\bin\)? – Alexei Levenkov Jul 13 '15 at 15:22
  • @AlexeiLevenkov You mean that I load them by using `Assembly.LoadFrom`? How could I obtain the original location from the code? I reached this location using `typeof(SomeType).Assembly.Location` – mehrandvd Jul 13 '15 at 18:58
  • There are probably thousands questions on how to get path to file in ASP.Net (usually looking for images), but answer always Server.MapPath http://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath... – Alexei Levenkov Jul 13 '15 at 20:40
  • @AlexeiLevenkov your solution is great. I think it would be the best answer! Why don't you post it as an answer!? – mehrandvd Jul 14 '15 at 09:39

3 Answers3

1

Unless you concerned with unloading assemblies use Assembly.LoadFrom with full path.

To get full path in ASP.Net use Server.MapPath (see following question to get correct arguments: Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference? ).

Notes:

  • you can't unload assemblies unless using custom separate AppDomain (just warning, probably not concern in this case)
  • Load, LoadFrom and LoadFile have very subtle differences - when manually loading assemblies make sure to carefully read MSDN articles for corresponding methods, related question on SO like Difference between LoadFile and LoadFrom with .NET Assemblies? and Suzanne Cook's articles (not much changed with these methods since 2005).
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

You can copy your modules to the project's bin directory in a post build event.

MichaelDotKnox
  • 1,312
  • 1
  • 14
  • 17
0

You can create a folder in your web application called "DynamicReferences" and store a copy of each dll in that folder. Then for each dll in that folder set the Copy to Output Directory property to "Copy Always" or "Copy if Newer"

John Davidson
  • 637
  • 5
  • 11
  • I've already did that. All modules exists in `bin` folder. The problem is that when I'm running the project, at the line which I'm loading assemblies, it says that can not find the assembly. – mehrandvd Jul 13 '15 at 19:00