【问题标题】:Unity 3 - The type name or alias xxxx could not be resolvedUnity 3 - 无法解析类型名称或别名 xxxx
【发布时间】:2015-08-08 07:13:06
【问题描述】:

我在这里阅读了几个相关的问题,但我似乎仍然无法让我的 Unity XML 配置正常工作。

这是我的配置文件...

<?xml version="1.0" encoding="utf-8"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="System.Security" />
    <namespace name="System.Security.Cryptography" />
    <container name="Default">
        <register type="HashAlgorithm" mapTo="SHA256Managed" />
        <register type="SymmetricAlgorithm" mapTo="AesCryptoServiceProvider" />
    </container>
</unity>

我得到的错误信息是...

类型名称或别名 AesCryptoServiceProvider 无法解析。 请检查您的配置文件并验证此类型名称。

HashAlgorithm 正常解析,只是 AES 提供程序没有解析。

我的项目中引用了System.Security程序集,如果我这样做可以解决SymmetricAlgorithm类型...

IUnityContainer Container = new UnityContainer ();

Container.RegisterType<SymmetricAlgorithm, AesCryptoServiceProvider> ();

...但如果可能,我希望将配置保留在代码之外。

谁能帮我解决这个问题?

【问题讨论】:

    标签: c# .net xml configuration unity-container


    【解决方案1】:

    问题是AesCryptoServiceProvider 不在System.Security 中。您将在 System.Core 程序集中找到它。

    所以你需要修复你的统一配置以包括该程序集,如下所示:

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
      <namespace name="System.Security.Cryptography" />
      <assembly name="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <container name="Default">
        <register type="HashAlgorithm" mapTo="SHA256Managed" />
        <register type="SymmetricAlgorithm" mapTo="AesCryptoServiceProvider" />
      </container>
    </unity>
    

    注意:您需要提供程序集的全名(包括版本、文化和令牌)。这是因为 Unity 只会将命名空间和程序集名称与类型名称连接起来,然后它会检查结果类型是否存在(检查 documentation here)。使用名称加载类型时,大多数情况下您将需要完全限定名称,例如参见 Type.GetType 中关于类型名称的备注:

    要获取的类型的程序集限定名称。请参阅 AssemblyQualifiedName。如果类型在当前执行的程序集中或 Mscorlib.dll 中,则提供由其命名空间限定的类型名称就足够了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多