简体   繁体   中英

Autofac 4+ With Xamarin.Forms in iOS failed to build

Versions

iOS project

package id="Autofac" version="4.6.1" targetFramework="xamarinios10"

Core project

TargetFramework=netstandard1.4
PackageTargetFallback=portable-win+net45+wp8+win81+wpa8
PackageReference Include="Autofac" Version="4.6.1"
PackageReference Include="HockeySDK.Xamarin" Version="4.1.5"
PackageReference Include="ReactiveProperty" Version="3.6.0"
PackageReference Include="Xamarin.Forms" Version="2.3.4.267"
PackageReference Include="Xamarin.Forms.Theme.Base" Version="1.0.0.43-pre1"
PackageReference Include="Xamarin.Forms.Theme.Dark" Version="1.0.0.43-pre1"
PackageReference Include="Xamarin.Forms.Theme.Light" Version="1.0.0.43-pre1"

I'm using this template (work out of the box with PCL and Autofac 3.5.2): https://github.com/NowBI/Xamarin-Forms-Mobile-Template

I update my core to netstandard and update all package.

Droid project is running fine and dependencies injections works fine.

But it's not working on iOS:

Build success but I get an exception on:

Container = builder.Build();

System.ArgumentException: The type 'MobileTemplate.Core.Services.MenuItemService' is not assignable to service 'MobileTemplate.Core.Services.IMenuItemService'.

The class is ok and verified as it is working on Android. This Ioc dependency is part of the Core project:

public interface IMenuItemService
{
    IReadOnlyReactiveProperty<IEnumerable<MenuItemModel>> MenuItems { get; }
}

public class MenuItemService : IMenuItemService
{
    private readonly IReactiveProperty<IEnumerable<MenuItemModel>> _menuItemsInternal;
    public IReadOnlyReactiveProperty<IEnumerable<MenuItemModel>> MenuItems { get; }
...

And my IoC declaration in Core:

public static class IoC
{
    public static IContainer Container;

    public static void Publish(this ContainerBuilder builder)
    {
        Container = builder.Build();
    }

    public static void RegisterCoreDependencies(this ContainerBuilder builder)
    {
        // -- Add your shared injected services here.
        // builder.RegisterType<Class>().As<IInterface>().SingleInstance();
        builder.RegisterType<NavigationService>().As<INavigationService>().SingleInstance();
        builder.RegisterType<MenuItemService>().As<IMenuItemService>().SingleInstance();

        builder.RegisterType<ShoppingItemService>().As<IShoppingItemService>().SingleInstance();
        builder.RegisterType<ShoppingCartService>().As<IShoppingCartService>().SingleInstance();
    }
}

I call the IoC in FinishedLaunching in AppDelegate.cs:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        RegisterHockeyApp();
        BuildIoCContainer();

        global::Xamarin.Forms.Forms.Init();
        VerifyThemeAssemblies();
        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }

I'm looking on Internet for a while and have no idea what to do to fix that.

I can revert my changes and leave netstandard and autofac 4+ to get back to PCL and autofac 3.5.2 but it's a new project and want to be up to date.

Thanks for your help

Links on Github:

https://github.com/autofac/Autofac/issues/864

https://github.com/NowBI/Xamarin-Forms-Mobile-Template/issues/3

通过将我的Core项目升级到Net Standard 2.0,我终于摆脱了这种例外

Also make sure if you have a reference to Newtonsoft.Json (in addition to AutoFac obviously) in your .NET standard library to add it to the iOS project as well. I was having the issue (seemed totally unrelated but the .Build() call on my container in the core project was failing with the same exception as you) and adding that package did the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM