:错误探测_.Net组装探测:简单组装

:错误探测

探测组件: (Probing Assembly:)

Simple Assembly Probing (a .NET assembly without strong name)

简单程序集探测(不带强名的.NET程序集)

strong-named assemblies. I will discuss strong-named assembly probing in a subsequent next article. Therefore, let's begin with a console application named 强命名程序集的示例应用程序开始。 我将在后续的下一篇文章中讨论强名称程序集探测。 因此,让我们从一个名为 AssemblyResolutionDemo.
using System;

namespace AssemblyResolutionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                FullName.NonGAC.FullName.GetFullName("John", "Smith")
            );
            Console.WriteLine(
                typeof(FullName.NonGAC.FullName).Assembly.CodeBase
            );
            Console.Read();
        }
    }
}

As the code example reflects, AssemblyResolutionDemo references  a project/assembly named FullName.NonGAC.

如代码示例所示,

using System;

namespace FullName.NonGAC
{
    public class FullName
    {
        public static string GetFullName(string firstName, string lastName)
        {
            return string.Format("{0}, {1}", firstName, lastName);
        }
    }
}

Notice that the GetFullName function returns a formatted string, which we are accessing in the main application. The assembly's codebase outputs the file path of the loaded assembly, making it easier for us to validate the location from where the CLR loads the assembly.

注意,

1. The CLR will check the applications’ base directory first.

1. CLR将首先检查应用程序的基本目录。

When the application runs, the CLR checks the bin directory of the application, loading the assembly if it exists.

当应用程序运行时,CLR检查应用程序的bin目录,并加载程序集(如果存在)。

Img003

2. If the assembly does not exist in the bin directory, the CLR checks for a subdirectory with the assembly name (e.g., bin\FullName.NonGAC).

2.如果程序集在bin目录中不存在,则CLR检查带有程序集名称的子目录(例如bin \ FullName.NonGAC)。

To test this step, set the "Copy Local" property of the FullName.NonGAC assembly in the AssemblyResolutionDemo project to false. Therefore, the DLL file will not be in the bin directory. Subsequently, create a subdirectory of the bin folder called FullName.NonGAC with the associated assembly inside.

要测试此步骤,请将AssemblyResolutionDemo项目中FullName.NonGAC程序集的“复制本地”属性设置为false。 因此,该DLL文件将不在bin目录中。 随后,在bin文件夹中创建一个名为FullName.NonGAC的子目录,并在其中包含关联的程序集。

Using the Assembly Binding Log Viewer (fuslogvw.exe), a tool available via the Visual Studio command prompt, we can check the assembly probing process via the log file as we run the application again.

使用程序集绑定日志查看器(fuslogvw.exe)(可通过Visual Studio命令提示符使用的工具 ,我们可以在再次运行应用程序时通过日志文件检查程序集探测过程。

Img004
Ok what if it doesn’t find it in here?
Img005
Img006

3. If neither of the aforementioned cases exists, the CLR checks the app.config file for additional subdirectories to probe.

3.如果上述两种情况均不存在,则CLR将检查app.config文件中是否有要探测的其他子目录。

Now we consider that our DLL exists in a subdirectory of the bin folder named MyDLLs. The CLR will not probe this directory unless we define it as a probing path in the configuration file.

现在,我们认为DLL存在于名为MyDLLs的bin文件夹的子目录中。 除非我们在配置文件中将其定义为探测路径 ,否则CLR不会探测该目录。

<?xml version="1.0"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="MyDLLs" />
    </assemblyBinding>
  </runtime>
</configuration>
<probing privatePath="MyDLLs; DLLs;" />

With the proper app.config, the CLR finds and loads the DLL.

使用适当的app.config,CLR会找到并加载DLL。

Img008
Img009

4. Assemblies outside the application's base directory.

4.在应用程序的基本目录外进行汇编。

MSDN

MSDN

References:

参考文献:

Locating the Assembly through Codebases or Probing

通过代码库或探测找到程序集

http://msdn.microsoft.com/en-us/library/15hyw9x3.aspx  http://msdn.microsoft.com/zh-CN/library/15hyw9x3.aspx

<probing> Element

<探测>元素

http://msdn.microsoft.com/en-us/library/823z9h8w(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/823z9h8w(v=vs.110).aspx

Assembly Binding Log Viewe

程序集绑定日志视图

http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/e74a18c4(v=vs.110).aspx

How to load an assembly at runtime that is located in a folder that is not the bin folder of the application

如何在运行时加载位于应用程序的bin文件夹以外的文件夹中的程序集

http://support.microsoft.com/kb/837908 http://support.microsoft.com/kb/837908

翻译自: https://www.experts-exchange.com/articles/11180/Net-Assembly-Probing-Simple-Assembly.html

:错误探测

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值