0

I'm trying to use Autofac to register a service into Xamarin.Forms (netstandard2.0)

here's my code:

SqliteManager.cs

public interface ISqliteManager
{
    void test();
}

public class SqliteManager : ISqliteManager
{
    private readonly string _path;
    public SqliteManager()
    {
        _path = "test";
    }

    public void test()
    {
        Console.Write(_path);
    }
}

App.xaml.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        ViewModelLocator.RegisterDependencies();
        ViewModelLocator.Resolve<ISqliteManager>().test();
    }
}

ViewModelLocator.cs

public static class ViewModelLocator
{
    private static IContainer _container;

    public static void RegisterDependencies()
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<SqliteManager>().As<ISqliteManager>().SingleInstance();

        _container?.Dispose();
        _container = builder.Build();
    }

    public static T Resolve<T>()
    {
        return _container.Resolve<T>();
    }
}

it's seems ok, but i get this error when i try to resolve the service in app.xaml.cs:

Registration: Activator = SqliteManager (ReflectionActivator), Services = [Tracker.Managers.Interfaces.ISqliteManager], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> No constructors on type 'Tracker.Managers.SqliteManager' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.

but sqlitemanager has a constructor!

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
Doc
  • 5,078
  • 6
  • 52
  • 88
  • 3
    Please post a [mcve] as simply pasting that code into LINQPad and adding the AutoFac nuget package gave me back "test" on the console so clearly AutoFac found that constructor on my end. Are you sure you didn't simplify the code for the question? One notable difference is of course that LINQPad doesn't use .NET Standard. – Lasse V. Karlsen Jan 03 '18 at 13:32
  • 5
    Or you have mutltiple `SqliteManager` in your source code, one of which doesn't have a constructor. – Liam Jan 03 '18 at 13:33
  • [Have you read the doc?](http://autofac.readthedocs.io/en/latest/advanced/cross-platform-apps.html#xamarin) – Travis Illig Jan 04 '18 at 03:42

0 Answers0