I'm having problems trying to inject dependencies inside the MVC.
The current exception is as follows:
The current type myproject.Core.ToolbarLogic is an interface and cannot be built. Do you miss type mapping?
Debugging I realized that this exception is related to this sentence (included in the Razor view):
@{ Html.RenderAction("Toolbar", "Toolbar"); }
In the UnityConfig file, all types are registered conveniently, I have an empty constructor in the controller, an exception arises from a place where I cannot access debugging ... Also I read a lot of answers here in StackOverflow and Google, I don’t know what try it now (I tried almost everything).
Does anyone know what the problem is with DI?
The code:
ToolbarController.cs
public class ToolbarController : BaseController
{
[Dependency]
public IToolbarLogic ToolbarLogic { get; set; }
public ToolbarController()
{
}
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
public static class UnityWebActivator
{
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));
}
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
UnityConfig.cs
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IToolbarLogic, ToolbarLogic>();
container.LoadConfiguration();
}
}
Unity.config
<?xml version="1.0"?>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
</container>
</unity>
Change 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)