0

I occasionally see the below error in my logs

Error processing robots.txt
Exception: Glass.Mapper.MapperExceptioMessage: Service has been disposed, cannot create object

My code in Robotservice is as below

        #region Injected
        //DI Implementation
        private readonly IMvcContext _mvcContext;

        public RobotsService(IMvcContext mvcContext)
        {
            _mvcContext = mvcContext; 
        }

        #endregion

        public string GetRobotsForItem()
        {
            var homeItem = _mvcContext.GetHomeItem<ISitecoreItem>();
            ...
            ...
           return <some text>;
        }

While debugging, I see that the data source is null for _mvcContext and hence Glass is complaining about this. When I searched(blogs, pages, etc) for this error, I see that DI should be implemented and is the solution for this error. But I already have DI Implemented and working in almost all scenarios. Any thoughts or ideas on this?

Thanks in Advance!

1 Answer 1

0

Yes, you will need to resolve your dependencies explicitly as shown below:

    public string GetRobotsForItem()
    {
        var mvcContext = DependencyResolver.Current.GetService<IMvcContext >();
        var homeItem = mvcContext.GetHomeItem<ISitecoreItem>();
        ...
        ...
       return <some text>;
    }

Hope this helps

If you getting same issue, you need to share how you are invoking this service please?

2
  • Thanks for responding. I changed my code as specified but still get the same error. Feb 1 at 9:07
  • I think you need to clean up your code and do away with the Interface variable. I will edit my suggested solution
    – Julius A
    Feb 1 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.