简体   繁体   English

在ASP.NET中从Bin加载程序集

[英]Load an Assembly from Bin in ASP.NET

I have a file name, like "Foo.dll," for a library that I know is in the bin directory. 我有一个文件名,比如“Foo.dll”,我知道这个库位于bin目录中。 I want to create an Assembly object for it. 我想为它创建一个Assembly对象。 I'm trying to instantiate this object from a class that's not a page, so I don't have the Request object to get the path. 我试图从一个不是页面的类中实例化这个对象,所以我没有Request对象来获取路径。 How do I get the path I need to use Assembly.Load()? 如何获得使用Assembly.Load()所需的路径?

Assembly.Load should not require a file path, rather it requires an AssemblyName. Assembly.Load不应该要求文件路径,而是需要AssemblyName。 If you know that your assembly is in the standard search path (ie the bin directory), you should not need to know the disk path of the assembly...you only need to know its assembly name. 如果您知道程序集位于标准搜索路径(即bin目录)中,则不需要知道程序集的磁盘路径...您只需要知道其程序集名称。 In the case of your assembly, assuming you don't need a specific version, culture, etc., the assembly name should just be "Foo": 对于程序集,假设您不需要特定的版本,文化等,程序集名称应该只是“Foo”:

Assembly fooAssembly = Assembly.Load("Foo");

If you do need to load a specific version, you would do the following: 如果确实需要加载特定版本,则可以执行以下操作:

Assembly fooAssembly = Assembly.Load("Foo, Version=1.1.2, Culture=neutral");

Generally, you want to use Assembly.Load, rather than Assembly.LoadFrom or Assembly.LoadFile. 通常,您希望使用Assembly.Load,而不是Assembly.LoadFrom或Assembly.LoadFile。 LoadFrom and LoadFile work outside of the standard fusion process, and can lead to assemblies being loaded more than once, loaded from insecure locations, etc. Assembly.Load performs a "standard" load, searching the standard assembly locations such as bin, the GAC, etc., and applies all the standard security checks. LoadFrom和LoadFile在标准融合过程之外工作,并且可能导致组件被多次加载,从不安全的位置加载等.Compam.Load执行“标准”加载,搜索标准组件位置,例如bin,GAC等,并应用所有标准安全检查。

Assembly.LoadFile(...)是否有效?

From your description it sounds like this is an web application, so unless you are on an asynchronous thread you spawned from a request, you should still have access to the HttpContext . 根据您的描述,这听起来像是一个Web应用程序,所以除非您是在一个异步线程上,而是从请求中产生,否则您仍然可以访问HttpContext From there you can use Server.MapPath() to the file you need. 从那里,您可以使用Server.MapPath()到您需要的文件。

A complete example as I use, if it helps. 我使用的完整示例,如果有帮助的话。 Resources is a folder under the root of the DLL Library (Assembly) Resources是DLL库(程序集)根目录下的文件夹

        public static string ReadAssemblyResourceFile(string resourcefilename)
        {
using (var stream = Assembly.Load("GM.B2U.DAL").GetManifestResourceStream("GM.B2U.DAL.Resources."
    + resourcefilename))            {
                    if (stream == null) throw new MyExceptionDoNotLog($"GM.B2U.DAL.Resources.{resourcefilename} not found in the Assembly GM.B2U.DAL.dll !");
                    using (var reader = new StreamReader(stream))
                    {
                        return reader.ReadToEnd();
                    }           
                }
        }

to call the function: 调用函数:

[TestMethod()]
public void ReadAssemblyResourceFileTest()
{
    var res = SetupEngine.ReadAssemblyResourceFile("newdb.sql");
    Assert.IsNotNull(res);
}

ps. PS。 Do not forget to to mark the "Build Action" as "Embedded Resource" (in the properties window) of each resource file. 不要忘记将“构建操作”标记为每个资源文件的“嵌入资源”(在属性窗口中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM