AI OnAI Off
You should be using something like this
public class MyClass
{
privat IContentLoader _contentLoader;
public MyClass(IContentLoader contentLoader)
{
_contentLoader = contentLoader;
}
}
Hi Janmejay,
You have to set Dependency Resolver in ASP.NET.
This is a code sample from Alloy:
[InitializableModule]
public class DependencyResolverInitialization : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
}
public void Initialize(InitializationEngine context)
{
DependencyResolver.SetResolver(new ServiceLocatorDependencyResolver(context.Locate.Advanced));
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
public class ServiceLocatorDependencyResolver : IDependencyResolver
{
readonly IServiceLocator _serviceLocator;
public ServiceLocatorDependencyResolver(IServiceLocator serviceLocator)
{
_serviceLocator = serviceLocator;
}
public object GetService(Type serviceType)
{
if (serviceType.IsInterface || serviceType.IsAbstract)
{
return GetInterfaceService(serviceType);
}
return GetConcreteService(serviceType);
}
private object GetConcreteService(Type serviceType)
{
try
{
// Can't use TryGetInstance here because it won’t create concrete types
return _serviceLocator.GetInstance(serviceType);
}
catch (ActivationException)
{
return null;
}
}
private object GetInterfaceService(Type serviceType)
{
object instance;
return _serviceLocator.TryGetExistingInstance(serviceType, out instance) ? instance : null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _serviceLocator.GetAllInstances(serviceType).Cast<object>();
}
}
You can find documentation on this link: https://world.episerver.com/documentation/developer-guides/CMS/initialization/dependency-injection/
I have this code
var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
I want to get the instance of IContentLoader using constructor injection.
How i can do that.