How do I reference assemblies outside the bin folder in an ASP.net application?

拜拜、爱过 提交于 2019-11-27 20:49:06
Vinay Sajip

According to the MSDN documentation,

Referenced assemblies outside the application's root directory must have strong names and must either be installed in the global assembly cache or specified using the <codeBase> element.

So, it might appear that you're out of luck. Here's what I'd try:

  1. Create an NTFS junction point under your application base directory which points to the directory containing your shared code. This is the key step. For example, in your application base directory, run linkd SharedCode c:\NotMyCode. This makes <yourappbase>\SharedCode effectively be an alias for c:\NotMyCode.
  2. Tell ASP.NET to probe for assemblies in this path, using a <probing> element, referencing the junction point SharedCode. Since this is under your application base, it should work. Alternatively, use AppDomainSetup.PrivateBinPath to set the path probed for assemblies.

I'm quite curious to see if this works :-)

You could always use the: AppDomain.CurrentDomain.AssemblyResolve Event and call

Assemly.Load(Path.Combine(@"c:\NotMyStuff",args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"))

See link for more info.

You could always put your referenced assemblies in the GAC, then the location would not matter. You can install the component by dragging it into the GAC (C:\windows\assembly) or running GACUtil.exe.

This article may help. It describes the "Best Practices for Assembly Loading."

At first I thought you could used the <probing privatePath="bin\debug"/> element in the runtime/assemblyBinding in the web.config, but the probing will only allow you to specify subdirectories under the root location.

I think you can reference specific assembly paths in code as well.

AppDomain.CurrentDomain.AppendPrivatePath("C:\\NotMyCode");

Doing that in your Global.asax Application_Start should do the trick.

You can put in GAC and access it from there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!