Unity DI与控制器中的MVC 5

时间:2017-03-21 12:06:14

标签: c# asp.net-mvc razor asp.net-mvc-5 unity-container

我在尝试在MVC控制器中进行依赖注入时遇到了问题。

目前的例外情况如下:

  

当前类型myproject.Core.ToolbarLogic是一个接口和   无法建造。你错过了类型映射吗?

调试我意识到这个例子是从这句话中提出的(包含在Razor视图中):

@{ Html.RenderAction("Toolbar", "Toolbar");  }

在UnityConfig文件中,所有类型都方便注册, 我在控制器中有一个空构造函数, 从我无法访问调试的地方引发异常......此外,我在StackOverflow和Google中已经阅读了很多答案,我不知道现在要尝试什么(我几乎尝试了所有内容)。

任何人都知道DI有什么问题?

代码:

ToolbarController.cs

public class ToolbarController : BaseController
{

    [Dependency]
    public IToolbarLogic ToolbarLogic { get; set; }

    public ToolbarController()
    {
    }


    // GET: Common/Toolbar
    public ActionResult Toolbar()
    {
        bool ShowConfidential = ToolbarLogic.ShowConfidential();
        string linkHome = ToolbarLogic.BindHome(base.User.Identity.Name);
        return PartialView(new ToolbarModel() {
            ShowConfidential = ShowConfidential,
            lnkHome = linkHome
        });
        return PartialView();
    }

}

UnityWebActivator.cs

/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
    /// <summary>Integrates Unity when the application starts.</summary>
    public static void Start() 
    {
        var container = UnityConfig.GetConfiguredContainer();

        FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
        FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        // TODO: Uncomment if you want to use PerRequestLifetimeManager
        // Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
    }

    /// <summary>Disposes the Unity container when the application is shut down.</summary>
    public static void Shutdown()
    {
        var container = UnityConfig.GetConfiguredContainer();
        container.Dispose();
    }
}

UnityConfig.cs

    /// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
    #region Unity Container
    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    });

    /// <summary>
    /// Gets the configured Unity container.
    /// </summary>
    public static IUnityContainer GetConfiguredContainer()
    {
        return container.Value;
    }
    #endregion

    /// <summary>Registers the type mappings with the Unity container.</summary>
    /// <param name="container">The unity container to configure.</param>
    /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
    /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterType<IToolbarLogic, ToolbarLogic>();

        // There is an Unity.config file
        container.LoadConfiguration();
    }
}

Unity.config

<?xml version="1.0"?>
<!-- Configuración de Unity -->
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <container>


  </container>
</unity>

编辑1:

我评论LoadConfiguration行是@Nkosi说的,因为它有意义,因为我在Unity.config中没有任何相关信息(见上文,它是空的)。

错误同样如此:

Resolution of the dependency failed, type = "MVCControls.Controllers.ToolbarController", name = "(none)".
Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, interfaces_logic.Interfaces.IToolbarLogic, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

  Resolving MVCControls.Controllers.ToolbarController,(none)
  Resolving value for property ToolbarController.ToolbarLogic
  Resolving interfaces_logic.Interfaces.IToolbarLogic,(none)

4 个答案:

答案 0 :(得分:0)

尝试通过以下方式替换控制器的构造函数:

public ToolbarController(IToolbarLogic toolbarLogic)
{
    this.ToolbarLogic = toolbarLogic;
}

答案 1 :(得分:0)

根据代码中的注释,表明存在统一配置文件。

在代码中配置后调用container.LoadConfiguration();将覆盖配置文件中的任何匹配类型注册。

public static void RegisterTypes(IUnityContainer container) {
    // Register the default type in code
    container.RegisterType<IToolbarLogic, ToolbarLogic>();

    // Override with the config file, if there is a unity section.
    if (ConfigurationManager.GetSection("unity") != null) {
        // There is an Unity.config file
        container.LoadConfiguration();
    }        
}

如果配置文件中的任何先前注册(如果有的话)与之匹配,则任何其他类型的注册都可以在加载配置文件后继续存在。

例如,如果我们有上面的代码和以下包含统一注册的配置文件

<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <!-- other config removed for brevity -->
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="..." />
    <container>
      <register type="IToolbarLogic" />
      <!-- Note the missing mapTo="ToolbarLogic" attribute -->
    </container>
  </unity>
  <!-- other config removed for brevity -->
</configuration>

它未正确映射IToolbarLogic界面。如果在代码中注册接口后调用LoadConfiguration扩展方法,那么先前的注册将被覆盖并导致OP的错误。

我建议您查看.config文件并确保已正确配置。您还应该确保如果ToolbarLogic类具有任何依赖关系,那么它们也会在容器中注册。 ToolbarLogic也应该继承IToolbarLogic

public class ToolbarLogic : IToolbarLogic {
    //...other code removed for brevity
}

来源:Dependency Injection with Unity

答案 2 :(得分:0)

用于解析配置的mapto注册条目缺少unity配置

<container>
  <register type="MyService"> ... </register> <!-- Default registration for type MyService -->
  <register type="ILogger" mapTo="EventLogLogger" /> <!-- type mapping -->
  <register type="ILogger" mapTo="PageAdminLogger" name="emergency" /> <!-- named registration -->
</container>

请使用this link了解详情。

答案 3 :(得分:0)

你可以试试这个

public class ToolbarControllerContext
{
    [Dependency]
    public IToolbarLogic ToolbarLogic { get; set; }
}

public class ToolbarController : BaseController
{    
    private readonly IToolbarLogic _toolbarLogic;

    public ToolbarController(ToolbarControllerContext context)
    {
        _toolbarLogic = context.ToolbarLogic;
    }


    // GET: Common/Toolbar
    public ActionResult Toolbar()
    {
        bool ShowConfidential = _toolbarLogic.ShowConfidential();
        string linkHome = _toolbarLogic.BindHome(base.User.Identity.Name);
        return PartialView(new ToolbarModel() {
            ShowConfidential = ShowConfidential,
            lnkHome = linkHome
        });
        return PartialView();
    }
}