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

后端 未结 5 1545
梦谈多话
梦谈多话 2020-12-05 16:14

I\'ve a working XML Web service written in ASP.Net. Now I need to reference certain assemblies lying in a specific folder e.g. c:\\NotMyCode
I do no

相关标签:
5条回答
  • 2020-12-05 16:38

    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.

    0 讨论(0)
  • 2020-12-05 16:45

    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.

    0 讨论(0)
  • 2020-12-05 16:49

    You can put in GAC and access it from there.

    0 讨论(0)
  • 2020-12-05 16:52

    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 :-)

    0 讨论(0)
  • 2020-12-05 16:52

    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.

    0 讨论(0)
提交回复
热议问题