简体   繁体   English

Autofac-通过配置从特定文件夹注册程序集

[英]Autofac - Register assembly by config from specific folder

I register my assemblies by autofac in configuration. 我在配置中通过autofac注册我的程序集。 I want the autofac load assemblies from another folder, no 'bin' folder. 我想要从另一个文件夹(没有“ bin”文件夹)中的autofac加载程序集。

I try put it 'type' attribute path of asssembly like this: 我尝试将其的“类型”属性设置为如下所示:

<component type="Amazon.SQS.AmazonSQSClient, amazonDlls/AWSSDK" service="Amazon.SQS.IAmazonSQS, AWSSDK" />

But I get error of invalid assembly name. 但是我收到无效的程序集名称的错误。

How can I do it - register assembly from specific folder? 我该怎么做-从特定文件夹注册程序集?

by the way: I know it is possible from code but I want from config. 顺便说一句:我知道可以从代码中获取,但我想从配置中获取。

It is not possible to provide a path to the assembly in the fully-qualified type name. 不能以完全限定的类型名称提供程序集的路径。 It seems it's not even possible to register types from libraries not located in different directory than "bin" folder. 似乎甚至不可能从不在“ bin”文件夹之外的目录中的库中注册类型。 Even if you prepare configuration file in the same folder as dll you want to register and read it this way: 即使您在与dll相同的文件夹中准备配置文件,也要注册并以这种方式读取它:

builder.RegisterModule(new ConfigurationSettinsgReader("<section-name>", "amazonDlls/app.config"));

you will get the exception of type not found. 您将获得找不到类型的异常。

As noted in the documentation the names of component types are fully-qualified type names , which is a format defined by the .NET specification. 文档中所述,组件类型的名称是完全限定的类型名称 ,这是.NET规范定义的格式。 A fully-qualified type name does not include a path. 完全限定的类型名称不包含路径。

Specifying a path is not possible with the out-of-the-box configuration format. 现成的配置格式无法指定路径。 You have a couple of options. 您有两种选择。

Option 1: Add a Probing Path 选项1:添加探测路径

By default, .NET only searches the bin folder of your application for assemblies. 默认情况下,.NET仅在应用程序的bin文件夹中搜索程序集。 You can read about how .NET locates assemblies on MSDN 您可以阅读有关.NET如何在MSDN上定位程序集的信息

.NET does allow you to specify additional paths to search via app.config or web.config : .NET确实允许您指定其他路径来通过app.configweb.config进行搜索:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="bin;bin\plugins" />
    </assemblyBinding>
  </runtime>
<configuration>

These paths must be subdirectories of the bin folder so that may not work for you. 这些路径必须是bin文件夹的子目录,因此可能对您不起作用。

If it must be outside the bin folder (and not under it in a subdirectory) you can use the <codeBase> element to specify a URL location . 如果它必须在bin文件夹之外(而不是在子目录下) ,则可以使用<codeBase>元素指定URL位置 If you do that, it looks like the assembly must have a strong name, so keep that in mind. 如果这样做,则看起来程序集必须具有强名称,因此请记住这一点。 The probingPath mechanism won't require the strong name. probingPath机制不需要强名称。

And, of course, there are risks inherent with loading assemblies that are in specified locations outside the GAC and outside your bin folder. 而且,当然,在GAC外部和bin文件夹外部的指定位置加载程序集存在固有的风险。 Stuff to consider. 东西考虑。

Option 2: Create Your Own Configuration Reader 选项2:创建自己的配置阅读器

This will be a lot harder. 这将变得更加困难。 You can grab the Autofac.Configuration source and create your own version of the configuration reader that includes file location support. 您可以获取Autofac.Configuration源,并创建自己的配置读取器版本,其中包括文件位置支持。 You'll probably need to look at Assembly.LoadFrom to get that to work, but, again, there are some risks doing that. 您可能需要查看Assembly.LoadFrom才能使它正常工作,但是再次这样做有一些风险。 The Assembly.LoadFrom docs outline the issues. Assembly.LoadFrom文档概述了这些问题。

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

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