locked
Type not found in cach error when getting ServiceLocator instance

    Question

  • Dear all,

    I have build a universal app project where I try to setup my Share part with different set of services. I have setup as well a ViewModelLocator define as below :

    public class ViewModelLocator
        {
            /// <summary>
            /// Initializes a new instance of the ViewModelLocator class.
            /// </summary>
             static ViewModelLocator()
            {
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    // Create design time view services and models
                    //SimpleIoc.Default.Register<IDataService, DesignDataService>();
                }
                else
                {
                    // Create run time view services and models
                    SimpleIoc.Default.Register<ISQLiteService, Services.SQLiteService>();
                }
    
                SimpleIoc.Default.Register<IMainViewModel,MainViewModel>();
            }
    
            public MainViewModel Main
            {
                get
                {
                    return ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }
    
            public static MainViewModel MainStatic
            {
                get
                {
                    return ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }
           
            public static void Cleanup()
            {
                // TODO Clear the ViewModels
            }
        }

    My MainViewModel is define as below :

     public class MainViewModel : ViewModelBase,IMainViewModel,ISQLiteService
        {
    ....
    
    }

    What is happening is that at the time I am retriviing the static instance of MainStatic property, I get an ActivationException error fo type:

    Type not found in cache: Track.ViewModel.MainViewModel.

    I am stretching my hairs for full day without knowing what is that, what it means and how to solved.

    Would be happy if someone could explain.

    PS : I notice that if I remove my ISQliteService interface, I do not get that error anymore

    regards



    • Edited by wakefun Wednesday, April 8, 2015 2:35 PM
    Wednesday, April 8, 2015 2:30 PM

All replies

  • Dear all,

    I have a wierd issue that I cannot understand correctly. Hope someone could light my torch.
    I have build a universall app application and in the share part, I would like to provide a service which retrive the Database connection .

    For that I have created and ISQLiteService interface as follow :

    public interface ISQLiteService
        {
            Task<SQLiteService> GetDatabase();
        }

    The corresponding implementation is done as follow :

    public class SQLiteService:ISQLiteService
        {
            private IStorageService _storageService;
            private SQLite.Net.Async.SQLiteAsyncConnection _connectionAsync;
            private ISQLitePlatform _sqlitePlatform;
    
            public SQLite.Net.Async.SQLiteAsyncConnection dbConnection { get; set; }
    
            #region "Constructor"
            public SQLiteService(ISQLitePlatform sqlitePlatform, IStorageService storageService)
            {
                this._sqlitePlatform = sqlitePlatform;
                this._storageService = storageService;
    
            }
            #endregion
    
            #region "Public methods"
    
            /// <summary>
            /// Retrive the database object
            /// </summary>
            /// <returns><see cref="SQLiteService"/></returns>
            public async Task<SQLiteService> GetDatabase()
            {
                SQLiteService _database = null;
               
    
                if (_database == null)
                {
                    
                    _database = new SQLiteService(new SQLitePlatformWinRT(), _storageService);
                    await _database.InitializeAsync();
                }
    
                return _database;
            }
    
            #endregion
    
    
            #region "Privaye methods"
            /// <summary>
            /// Initialize databse connection context
            /// </summary>
            /// <returns></returns>
            private async Task<SQLiteAsyncConnection> InitializeAsync()
            {
                if (_connectionAsync == null)
                {
                    string databaseFile = await _storageService.GetPathForFileAsync("App.sqlite");
                    SQLiteConnectionString _connectionParameters = new SQLiteConnectionString(databaseFile, false);
                    SQLite.Net.SQLiteConnectionPool dbConnectionPool = new SQLiteConnectionPool(_sqlitePlatform);
                   
                    _connectionAsync = new SQLite.Net.Async.SQLiteAsyncConnection(() =>
                         dbConnectionPool.GetConnection(_connectionParameters)); 
    
                        //new SQLiteConnectionWithLock(_sqlitePlatform, new SQLiteConnectionString(databaseFile, false)));
                    
                    dbConnection = _connectionAsync;
                    
                }
                return dbConnection;
            }
    
            #endregion
    
    
        }

    Then I have a ViewModelLocator class ( using MVVMLIght toolkit ) where my services gets register :

    public class ViewModelLocator
        {
            /// <summary>
            /// Initializes a new instance of the ViewModelLocator class.
            /// </summary>
             static ViewModelLocator()
            {
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    // Create design time view services and models
                    //SimpleIoc.Default.Register<IDataService, DesignDataService>();
                }
                else
                {
                    // Create run time view services and models
                    SimpleIoc.Default.Register<ISQLiteService, Services.SQLiteService>();
                }
    
                SimpleIoc.Default.Register<IMainViewModel,MainViewModel>();
            }
    
            public MainViewModel Main
            {
                get
                {
                    return ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }
    
            public static MainViewModel MainStatic
            {
                get
                {
                    return ServiceLocator.Current.GetInstance<MainViewModel>();
                }
            }
           
            public static void Cleanup()
            {
                // TODO Clear the ViewModels
            }
        }

    I try to get my ISQLiteService to be available from my MainView model in order that any call for getting data from database I will get a ready connection object

    Get really confused, thnaks for help

    regards

    Wednesday, April 8, 2015 2:48 PM
  • Hi wakefun,

    Simply guess, the issue happens because the code did not successfully register the service on this line:

    SimpleIoc.Default.Register<ISQLiteService, Services.SQLiteService>();
    

    Give a try to see if put this code out of the if-else helps.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 9, 2015 2:58 AM
    Moderator
  • Hello James,

    Based on the following code snipets of IOC registration in ViewModelLocator:

     static ViewModelLocator()
            {
                ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    // Create design time view services and models
                    //SimpleIoc.Default.Register<IDataService, DesignDataService>();
                }
                else
                {
                    // Create run time view services and models
                    SimpleIoc.Default.Register<ISQLiteService, Services.SQLiteService>();
                }
    
                SimpleIoc.Default.Register<IMainViewModel,MainViewModel>();
            }
    
     

    Is tehre a difference of creating the registration Inside the Else section ?

    I do not see the real logic here because if we place registration after the Else it is similar, the line will be executed.

    What is the logic of having something register in the Else part and some Other outside ?

    regards

    Thursday, April 9, 2015 9:26 AM