简体   繁体   English

使用Xamarin的Autofac 4 + .iOS中的Forms无法生成

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

Versions 版本

iOS project iOS项目

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 我正在使用此模板(与PCL和Autofac 3.5.2兼容使用): https : //github.com/NowBI/Xamarin-Forms-Mobile-Template

I update my core to netstandard and update all package. 我将核心更新为netstandard并更新了所有软件包。

Droid project is running fine and dependencies injections works fine. Droid项目运行良好,依赖项注入工作正常。

But it's not working on iOS: 但是在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. 该类还可以,并且可以在Android上正常运行,并经过验证。 This Ioc dependency is part of the Core project: 此Ioc依赖项是Core项目的一部分:

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: 还有我在Core中的IoC声明:

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: 我在AppDelegate.cs的FinishedLaunching中调用IoC:

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. 我在Internet上寻找了一段时间,却不知道该如何解决。

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. 我可以还原更改,并离开netstandard和autofac 4+返回PCL和autofac 3.5.2,但这是一个新项目,并且希望保持最新状态。

Thanks for your help 谢谢你的帮助

Links on Github: Github上的链接:

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

https://github.com/NowBI/Xamarin-Forms-Mobile-Template/issues/3 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. 还要确保在.NET标准库中是否有对Newtonsoft.Json的引用(显然除了AutoFac以外),也要将其添加到iOS项目中。 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. 我遇到了这个问题(似乎完全不相关,但是在核心项目中对我的容器的.Build()调用失败了,并且异常与您相同),然后添加该程序包就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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