Search code examples
asp.net-mvc-5runtime-errorstructuremap

ServiceLocatorImplBase.cs not found when using StructureMap


I get the following error when debugging (F5) my code in Visual Studio (2017):

ServiceLocatorImplBase.cs not found

Reading the detail, it seems to be looking for a file on my computer in the following place:

'c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs'

I don't have a directory 'c:\Home\Chris...' and I have no idea why it would be looking there.

How can I fix this?


Solution

  • (Answering own question)

    I spent days looking for an answer.

    I noticed that I started getting the issue after I had installed StructureMap. I managed to solve it with the help of this explanation.

    In short, I had to install CommonServiceLocator v2.0.1. using NuGet. Once I did that I then had to update this file:

    StructureMapDependencyScope.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http.Dependencies;
    //using Microsoft.Practices.ServiceLocation 
    using StructureMap;
    using CommonServiceLocator;                  
    
    namespace UI.DependencyResolution
    {
        /// <summary>
        /// The structure map dependency scope.
        /// </summary>
        public class StructureMapDependencyScope : ServiceLocatorImplBase, IDependencyScope
        {
    

    I added a reference to CommonServiceLocator and removed the reference to Microsoft.Practices.ServiceLocation.

    This removed the hard dependency on c:\Home\Chris....

    Hope that helps others.