Unity 应用程序块 2.0 - 给定的程序集名称或代码库无效

发布于 2024-09-28 07:00:53 字数 2783 浏览 4 评论 0原文

接口(在名为“Interfaces”的程序集中。在项目中:- 接口)

namespace Interfaces
{
    public interface IDoSomeWork1
    {
        string DoSomeWork1();
    }
}

namespace Interfaces
{
    public interface IDoSomeWork2
    {
        string DoSomeWork2();
    }
}

依赖项(在名为“Entities”的程序集中。在项目中:- 实体)

namespace Entities
{
    public class ClassB : IDoSomeWork1
    {
        public string DoSomeWork1()
        {
            return this.ToString();
        }
    }
}

namespace Entities
{
    public class ClassC : IDoSomeWork2
    {
        public string DoSomeWork2()
        {
            return this.ToString();
        }
    }
}

类(在项目中:-UsingUnity)

public class ClassA
    {
        [Dependency]
        public IDoSomeWork1 DoSomeWork1 { get; set; }
        [Dependency]
        public IDoSomeWork2 DoSomeWork2 { get; set; }


        public void SomeMethodInClassA()
        {
            Console.WriteLine(DoSomeWork1.DoSomeWork1());
            Console.WriteLine(DoSomeWork2.DoSomeWork2());
        }
    }

App.Config(在控制台应用程序项目中:-ConsoleUsingUnity)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                   Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type type="Interfaces.IDoSomeWork1, Interfaces"
                          mapTo="Entities.ClassB, Entities" />
                    <type type="Interfaces.IDoSomeWork2, Interfaces"
                          mapTo="Entities.ClassC, Entities" />
                </types>
            </container>
        </containers>
    </unity>
</configuration>

客户端(在控制台应用程序项目中:-ConsoleUsingUnity)

public class Class1
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();

            // Load from config file
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(container);

            ClassA classA = container.Resolve<ClassA>();
            classA.SomeMethodInClassA();
        }
    }

当我运行客户端时,我在section.Configure(container);:- 处收到以下错误

给定的程序集名称或代码库 无效。 (HRESULT 的异常: 0x80131047)

我不确定配置或类型是否有问题。有人可以指出这里的错误吗?

Interfaces (In the assembly named "Interfaces". In project :- Interfaces)

namespace Interfaces
{
    public interface IDoSomeWork1
    {
        string DoSomeWork1();
    }
}

namespace Interfaces
{
    public interface IDoSomeWork2
    {
        string DoSomeWork2();
    }
}

Dependencies (In the assembly named "Entities". In project :- Entities)

namespace Entities
{
    public class ClassB : IDoSomeWork1
    {
        public string DoSomeWork1()
        {
            return this.ToString();
        }
    }
}

namespace Entities
{
    public class ClassC : IDoSomeWork2
    {
        public string DoSomeWork2()
        {
            return this.ToString();
        }
    }
}

Class (In project :- UsingUnity)

public class ClassA
    {
        [Dependency]
        public IDoSomeWork1 DoSomeWork1 { get; set; }
        [Dependency]
        public IDoSomeWork2 DoSomeWork2 { get; set; }


        public void SomeMethodInClassA()
        {
            Console.WriteLine(DoSomeWork1.DoSomeWork1());
            Console.WriteLine(DoSomeWork2.DoSomeWork2());
        }
    }

App.Config (In a console application project :- ConsoleUsingUnity)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                   Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type type="Interfaces.IDoSomeWork1, Interfaces"
                          mapTo="Entities.ClassB, Entities" />
                    <type type="Interfaces.IDoSomeWork2, Interfaces"
                          mapTo="Entities.ClassC, Entities" />
                </types>
            </container>
        </containers>
    </unity>
</configuration>

The client (In a console application project :- ConsoleUsingUnity)

public class Class1
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();

            // Load from config file
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Configure(container);

            ClassA classA = container.Resolve<ClassA>();
            classA.SomeMethodInClassA();
        }
    }

And when I run the client, I get the following error at section.Configure(container);:-

The given assembly name or codebase
was invalid. (Exception from HRESULT:
0x80131047)

I am not sure If there is anything wrong with the config or the type. Could anyone please point out the mistake here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

还不是爱你 2024-10-05 07:00:54

现在有更好的办法了!
Unity 有一个新版本(当前为 2.1.505.2),它清楚地报告了详细信息,让您立即了解情况。

您可以在此处找到二进制文件和源代码:http://unity.codeplex.com/releases

There is a better way now!
Unity has a new version (currently 2.1.505.2) which clearly reports the details and lets you get to the bottom of it immediately.

You can find binaries and source here: http://unity.codeplex.com/releases

猫弦 2024-10-05 07:00:54

我发现查找确切绑定失败的 Type 的最耗时方法如下:

  1. 转到 codeplex http://unity.codeplex.com/SourceControl/list/changesets
  2. 选择一个版本并下载源代码
  3. Build DEBUG 版本UnityUnity.Configuration(如果您的项目使用更多的 Unity 程序集,也构建它们)
  4. 从项目中删除对 Unity 的引用,并添加对步骤 3
  5. 中的 程序集的引用Visual Studio 转到调试>;异常并确保公共语言运行时异常抛出列中有一个复选框。

现在去撞坏那个东西。执行将在 Unity 的 TypeResolverImpl.SearchAssemblies 方法中停止,并且 typeNameOrAlias 参数将保存答案!

I found that the least time consuming method of finding which Type exactly failed to bind is the following:

  1. Go to Sources section of Unity page at codeplex http://unity.codeplex.com/SourceControl/list/changesets
  2. Select a version and download the sources
  3. Build DEBUG version of Unity and Unity.Configuration (if your project uses more of unity assemblies, build them as well)
  4. Remove references to unity from your project and add references to assemblies from step 3
  5. In Visual Studio go to Debug > Exceptions and make sure Common Language Runtime Exceptions has a checkbox in the Thrown column.

Now go crash that thing. Execution will stop in Unitys' TypeResolverImpl.SearchAssemblies method and typeNameOrAlias parameter will hold the answer!

浅浅 2024-10-05 07:00:54

像这样工作

Dim dllData = System.IO.File.ReadAllBytes(dllFullPath)
Dim asb As System.Reflection.Assembly

asb = System.Reflection.Assembly.Load(dllData)

Dim cls As Object = asb.CreateInstance("namespace.class")

works like this

Dim dllData = System.IO.File.ReadAllBytes(dllFullPath)
Dim asb As System.Reflection.Assembly

asb = System.Reflection.Assembly.Load(dllData)

Dim cls As Object = asb.CreateInstance("namespace.class")
昔日梦未散 2024-10-05 07:00:54

请确保您已在 web.config 文件所在的项目中添加了缺少程序集的程序集引用。

我错过了这个。我已经在使用 Unity 解析类的项目中添加了这些程序集引用,但错过了将其添加到配置文件所在的父项目中。这解决了我的问题。

Please make sure, that you have added assembly references of the missing assembly in the project where your web.config file exists.

I was missing this. I already have added those assembly references in the project which was using Unity to resolve the class, but missed to add it in the parent project where configuration file was located. This has resolved my problem.

泪是无色的血 2024-10-05 07:00:53

如果其他人遇到同样的问题 - 我也遇到了这个错误,但问题略有不同。我试图加载一个明显存在的程序集,如下所示:

Assembly.Load("C:\\Program Files\\MyProgram\\MyAssembly.dll");

经过大量试验和错误,我发现您不应该传递路径,并且您当然不应该包含 .dll 扩展。以下解决了我的问题:

Assembly.Load("MyAssembly");

希望这能对其他人有所帮助!

In case anyone else ever has the same problem - I was also getting this error but had a slightly different problem. I was trying to load an assembly that clearly existed like the following:

Assembly.Load("C:\\Program Files\\MyProgram\\MyAssembly.dll");

After lots of trial and error I figured out that you aren't supposed to pass the path and you certainly aren't supposed to include .dll extension. The following fixed my issue:

Assembly.Load("MyAssembly");

Hopefully that helps someone else sooner or later!

浅唱ヾ落雨殇 2024-10-05 07:00:53

在回答我的问题之前,我必须声明上面发布的代码没有给我带来任何问题(构建错误等)。它只是给了我我在问题中指出的错误。 Unity 此时的问题是,它没有提供无法加载哪个程序集或程序集中的哪些类型。这是请求的功能

就我而言,这是一个缺少装配的问题。我没有将实体程序集引用到客户端应用程序项目中。似乎“实体”程序集只能在运行时解析(因为它没有给我任何编译时错误)。然而,运行时错误也根本没有用处。

我查看了 Fusion Log 查看器(它应位于 .NET SDK 文件夹中)。这是多么实用的瑰宝啊。它可以记录所有类型的程序集绑定(全部或仅失败),并且它对无法加载的程序集给出了非常简洁的描述。非常有帮助!
FailedToLoadAssemblyDetected

日志

因此,下次您收到“给定的程序集名称或代码库无效”错误时,请尝试 Fusion Log Viewer。它不会帮助您找到无法加载的类型。但是,至少您将确保所有程序集都正确加载。

Before I answer my question, I must state that the code posted above didn't give me any problem (build error etc.). It just gave me the error I stated in my question. The problem with Unity at this point of time is that It does not provide which assembly or a which types in the assembly could not be loaded. This is a requested feature.

In my case It was a missing assembly problem. I didn't reference Entities assembly in to the client application project. It seems that that "Entities" assembly could be resolved only at the run-time (since it didn't give me any compile time error). However, the run-time error was also not useful at all.

I had a look a Fusion Log viewer (It should be in the .NET SDK folder). What a gem of an utility It is. It can log all kind of assembly bindings (all or only failures) and It give a very neat description of which assembly could not load. Very helpful!
FailedToLoadAssemblyDetected

Log

So, next time, you get this "The given assembly name or codebase was invalid" error, try Fusion Log Viewer. It wont help you in finding which types couldn't be loaded. However,at least you will be sure all your assemblies are getting loaded correctly.

绮筵 2024-10-05 07:00:53

如果您连接域 AssemblyResolve 事件,您可以获得绑定失败的程序集。

 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

If you connect up the domain AssemblyResolve event you can get the assembly that has failed to bind.

 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文