locked
Dependency injection complaining about missing default constructor when it exists RRS feed

  • Question

  • User132983 posted

    I've used the dependency injection successfully with other classes but I just created a class that the injection service seems to have a problem with and I don't understand why.

    The interface in the main project

    namespace MyApp.Infrastructure.AbstractHelpers
    {
        public interface IDataCacheHelper
        {
            void ClearData();
        }
    }
    

    The implementation in the Android project

    using MyApp.Droid.Services;
    using MyApp.Infrastructure.AbstractHelpers;
    
    [assembly: Xamarin.Forms.Dependency(typeof(IDataCacheHelper))]
    namespace MyApp.Droid.Infrastructure.Helpers
    {
        public class DataCacheHelper : Java.Lang.Object,  IDataCacheHelper
        {
            public DataCacheHelper () 
            {}
    
            #region IDataCacheHelper implementation
    
            public void ClearData ()
            {
                GcmRegistrationService.Unsubscribe ();
            }
    
            #endregion
    
        }
    }
    

    When I try to call DependencyService.Get<IDataCacheHelper>() in the main project it says that the default constructor cannot be found.

    Any thoughts?

    Tuesday, December 15, 2015 5:22 PM

Answers

  • User132983 posted

    Figured it out! I had the interface instead of the derived type in the Dependency attribute

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, December 15, 2015 7:04 PM

All replies

  • User162453 posted

    This may be a linking issue: https://developer.xamarin.com/guides/android/advanced_topics/linking/

    Do you have Linker Behavior set to Link All Assemblies? If so, does the problem persist if you change the option to "Don't Link" or "Link SDK Assemblies"?

    Tuesday, December 15, 2015 5:45 PM
  • User132983 posted

    The behavior was set to Don't Link. I tried the other 2 settings and still received the same error. I also tried to rename the interface in the off chance there was an obscure name collision some how, but that didn't fix it either.

    Tuesday, December 15, 2015 6:12 PM
  • User132983 posted

    I also have another interface and implementation in the same 2 locations and that works without issue.

    Interface:

    namespace MyApp.Infrastructure.AbstractHelpers
    {
        public interface IFileIoHelper
        {
            void SaveText(string filename, string text);
            string LoadText(string filename);
            string LoadOrCreateText (string filename);
        }
    }
    

    implementation:

    using System;
    using System.IO;
    using MyApp.Droid.Infrastructure.Helpers;
    using MyApp.Infrastructure.AbstractHelpers;
    
    [assembly: Xamarin.Forms.Dependency(typeof(FileIoHelper))]
    namespace MyApp.Droid.Infrastructure.Helpers
    {
        public class FileIoHelper : Java.Lang.Object, IFileIoHelper
        {
            public FileIoHelper()
            {}
    
            public void SaveText(string filename, string text)
            {
                var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var filePath = Path.Combine(documentsPath, filename);
                System.IO.File.WriteAllText(filePath, text);
            }
            public string LoadText(string filename)
            {
                var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var filePath = Path.Combine(documentsPath, filename);
                return System.IO.File.ReadAllText(filePath);
            } 
    
            public string LoadOrCreateText(string filename)
            {
                var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var filePath = Path.Combine(documentsPath, filename);
    
                var retVal = "";
                if (!File.Exists (filePath))
                    SaveText (filename, "");
                else
                    retVal = LoadText (filename);
    
                return retVal;
    
            }
        }
    }
    
    Tuesday, December 15, 2015 6:19 PM
  • User132983 posted

    Figured it out! I had the interface instead of the derived type in the Dependency attribute

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, December 15, 2015 7:04 PM