0

We are migrating from .aspx web application to MVC app.

Below is the problem while migrating:

I have a third party dll which contains static factory method to return instance by given Interface name.

Controller code:

IBus objBus = (IBus) ObjectFactory.GetInstance("IBus") as IBus;

Here, ObjectFactory is from third party dll with definition as below (no more code details are available on GetInstance method).

public class ObjectFactory
{
    public ObjectFactory();
    public static object GetInstance(string interfaceName);
}

GetInstance method works perfectly in .aspx application - objBus is created successfully. But the same line (Controller code) when executed in MVC controller returns objBus as null.

Please suggest what could be the problem. I guess it may be due to difference in architecture and/or page life cyle of .aspx and mvc apps. Any suggestions on the problem are greatly appreciated.

4

1 回答 1

0

我认为您应该联系您的第三方 DLL 提供商并索取有关如何使用 ASP.NET MVC 的文档。

如果我是你,我会考虑使用 Unity 删除该 DLL,以便为 MVC 项目注入依赖项。您需要做的就是在 Global.asax 的 Application_Start 方法中使用 UnityConfig 和 DependecyResolver 将您的类实例注入到您的任何控制器方法中。

如果您需要详细信息,请查看这些出色的文章。

http://netmvc.blogspot.com/2012/04/dependency-injection-in-aspnet-mvc-4.html

http://dotnetslackers.com/articles/aspnet/Using-Microsoft-Unity-in-ASP-NET-MVC.aspx

http://www.codeproject.com/Articles/99361/How-To-Use-Unity-Container-In-ASP-NET-MVC-Framewor

于 2014-10-02T13:42:09.623 回答