Forum Xamarin.Forms

Dependency injection complaining about missing default constructor when it exists

JoshuaRussoJoshuaRusso USMember ✭✭
edited December 2015 in Xamarin.Forms

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?

Best Answer

Answers

  • EZHartEZHart USXamarin Team Xamurai

    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"?

  • JoshuaRussoJoshuaRusso USMember ✭✭

    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.

  • JoshuaRussoJoshuaRusso USMember ✭✭
    edited December 2015

    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;
    
            }
        }
    }
    
Sign In or Register to comment.