0
votes

I am using Prism 6.3.0 with Autofac.

When I register a type for navigation inside the Initialize() method of a IModule like this:

 public class TestModule : IModule
 {
    public TestModule (ContainerBuilder builder)
    {
        this.Builder = builder;
    }

    private ContainerBuilder Builder { get; set; }

    public void Initialize()
    {
        ...
        this.Builder.RegisterTypeForNavigation<ViewTest>();
    }
}

And then try to navigate from another Module like this:

  private void NavigateTest()
        =>
            this.RegionManager.RequestNavigate("MainContentRegion", new Uri("ViewTest", UriKind.Relative), this.Result);

    private void Result(NavigationResult obj)
    { // Breakpoint here
    }

It fails with the following error:

The requested service 'ViewTest (System.Object)' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

And StackTrace:

en Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters) en Prism.Autofac.AutofacServiceLocatorAdapter.DoGetInstance(Type serviceType, String key) en Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) en c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:lĂ­nea 49

But if I register the type in ConfigureContainerBuilder(ContainerBuilder builder), it works fine and I can navigate without problems:

protected override void ConfigureContainerBuilder(ContainerBuilder builder)
    {
        builder.RegisterTypeForNavigation<ViewTest >();

        base.ConfigureContainerBuilder(builder);
    }

Is it possible to do the registration in the Module's Initialize() method or does it have to be in ConfigureContainerBuilder()?

Thanks

1

1 Answers

0
votes

Autofac does not support modules as it is a immutable container. The Builder you are passing in is not the builder the bootstrapper is using, but rather a new instance. For now, you could work around it by updating the Container with the new builder, but Autofac has marked the Update method as obsolete and it will be removed. Due to this change, going forward I would not recommend Autofac for apps that require modules, because in v7 Prism will no longer allow modules to be loaded at all if using Autofac for this reason.

If you feel strongly about this, you could post an issue to the Autofac project asking them not to make the container immutable.