1
I am developing an application using Ioc and Ninject for dependency injection, and came across the following problem:
When uploading my application to the server I got the following error:
Someone could help me in how to solve this mistake?
Ioc.Install method
public static void Install()
{
DILoader.Install(
(tInterface, tClass) => IoC.Register(tInterface, tClass),
(tInterface, tClass) => IoC.RegisterInSingletonScope(tInterface, tClass),
(tInterface, tClass) => IoC.RegisterInThreadScope(tInterface, tClass)
);
}
This is where I carry the Assemblies
public static void LoadAssemblies()
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
loadedAssemblies
.SelectMany(x => x.GetReferencedAssemblies())
.Distinct()
.Where(y => loadedAssemblies.Any((a) => a.FullName == y.FullName) == false)
.ToList()
.ForEach(x => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(x)));
}
This is where I call these methods
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
LoadAssemblies();
IoC.Install();
}
Way where I register the Binds
public class NinjectIoCContainer : IIocContainer
{
private IKernel kernel;
private IKernel Kernel
{
get
{
if (kernel == null) {
kernel = new StandardKernel();
}
return kernel;
}
}
public void Register(Type tInterface, Type tClass)
{
Kernel.Bind(tInterface).To(tClass);
}
public void RegisterInSingletonScope(Type tInterface, Type tClass)
{
Kernel.Bind(tInterface).To(tClass).InSingletonScope();
}
public void RegisterInThreadScope(Type tInterface, Type tClass)
{
Kernel.Bind(tInterface).To(tClass).InThreadScope();
}
public TInterface Resolve<TInterface>()
{
return Kernel.Get<TInterface>();
}
}
}
To bind it takes the classes that have the attribute as the example below
[InstanceIoC]
public class UCManterUsuario : IManterManterUsuario
{
Ninject could not find any type associated with the interface
IManterUsuario
and being the same interface, it cannot create an instance to inject without an association being provided. You could post your code where you configure Ninject associations?– SomeDeveloper
Put your code (where you create the kernel and where you register dependencies) so we can help.
– Omni
I added more details!
– Ronaldo Asevedo
The code snippet where you set up the Ninject bindings does not appear yet. You must have some method similar to this:
public static void RegisterServices(IKernel kernel) { kernel.Bind<IManterUsuario>().To<ImplemetaManterUsuario>(); }
– iuristona
I have never used this Automatic loading. Apparently this implementation is failing to detect the attribution
InstanceIoC
. I would do a test to find out if it works by setting it in the methodInstall()
:IoC.Register(typeof(IManterUsuario), typeof(UCManterUsuario ));
– iuristona