2

我正在研究具有不同层的 MVC 项目(主项目 MVC Web 其他项目是类库,其中 Bin 指向 MVC Web Bin 文件夹)使用 Unity 进行依赖注入。

在我的本地机器上运行时,一切看起来都很好。如果我发布我的网站并浏览应用程序正在获取

给定的程序集名称或代码库无效。

var container = new UnityContainer();
container.LoadConfiguration();        //Error line

我花了一些时间查看问题,发现在我的 Published Bin 文件夹中我的业务和数据访问 DLL 丢失了?

这是因为 Unity 吗?因为我的接口通过统一引用业务层?

我错过了什么或我如何发布没有任何问题?

PS:如果我复制 DLL 并放入虚拟目录文件夹,一切看起来都很好。

谢谢

4

1 回答 1

2

In a web project, or any final project that employs dependency injection (could be WPF or Silverlignt application, even classic ASP.NET), all binaries that are used should be made available.

Imagine you have Lib1.dll and Lib1.Impl.dll, respectively an API library and a concrete implementation of the API. Now, in the web project you rely on dependency injection to provide you with concrete implementation of the interfaces from the Lib1.dll. You may never happen to use a class from Lib1.Impl.dll directly in that application. Still, the DI framework needs to access the Lib1.Impl.dll in order to instantiate the dependencies.

Depending on the DI framework and the supported capabilities of loading dependencies, you should provide them accordingly. It seems straightforward to have them all in the bin folder, or referenced directly by the final project - that is probably the reason your project works when you provide the implementation dll-s in the bin folder. Since, however, this goes against the considerations of decoupling, you may have to look at alternative approaches to dynamically load dll-s - by physical location for instance. I am sure Unity supports mechanisms for this, since Modularity in Enterprise Library allows for bootstrapping modules from a filesystem location.

I must say, though, while I mentioned decoupling and keeping the references separate, this is not an issue if you have them all referenced in the final tier of the project hierarchy (the web application), as long as you do not have code that causes coupling in that application.

于 2013-04-18T11:45:47.327 回答