简体   繁体   中英

XAML designer “Could not load file or assembly Autofac 4.1” in App.xaml of UWP application using MVVMLight

Starting with a blank Universal Windows Application I added Autofac 4.1, Autofac.Extras.CommonServiceLocator 4.0 and MvvmLightLibs 5.3. I then created the following ViewModelLocator class.

using Autofac;
using Autofac.Extras.CommonServiceLocator;
using GalaSoft.MvvmLight.Views;
using Microsoft.Practices.ServiceLocation;

namespace UwpTest
{
    public class ViewModelLocator
    {
        public static IContainer Container { get; private set; }
        public static bool IsBuilt { get; set; }

        public ViewModelLocator()
        {
            if (!IsBuilt)
            {
                var builder = new ContainerBuilder();
                builder.RegisterType<DialogService>().As<IDialogService>();
                builder.RegisterType<NavigationService>().As<INavigationService>();

                Container = builder.Build();

                IsBuilt = true;
            }

            ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(Container));
        }
    }
}

Then in the App.xaml I added the view model locator as a resource.

<Application
    x:Class="UwpTest.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UwpTest"
    RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <local:ViewModelLocator x:Key="ViewModelLocator" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

At this point I get the following error:

Could not load file or assembly 'Autofac, Version=4.1.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified.

Everything works fine at run time, but not at design time. I have double check that all packages have been restored. How can I find the location that the designer is attempting to load the Autofac dll from? Or what am I missing about how the project is setup?

I have already been though the MSDN guide to troubleshooting design time issues , tried to debug the issue using a second instance of Visual Studio, and searched for answer in every way I can think of.

Repo with repo of the issue can be found here .

With your project uploaded I reproduced your issue. The solution for resolving this error is easy, just remove the Autofac 4.1.1 package it will be fine.

This is because when you install the Autofac.Extras.CommonServiceLocator NuGet package, it will help you install the dependencies which contain the Autofac package at the same time. You don't need to install the Autofac package by yourself. When you installed Autofac.Extras.CommonServiceLocator 4.0.0 it will help you install Autofac 3.5.0 . If you install another version Autofac package it may lead ambiguity.

If you do want add two packages at the same time, please update the Autofac version from 4.1.1 to 3.5.0 . And it will also be fine, but I don't think it is necessary.

For how I know the Auofac is version 3.5.0 please see the following picture. If you tried to install the Autofac 4.11 after the Autofac.Extras.CommonServiceLocator 4.0.0 installed, you will get a update prompt about Autofac from 3.5.0 to 4.1.1 .

在此输入图像描述

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