@@ -140,35 +140,27 @@ public static IUmbracoBuilder AddModelsBuilder(this IUmbracoBuilder builder)
140140 return builder ;
141141 }
142142
143+ // See notes in RefreshingRazorViewEngine for information on what this is doing.
143144 private static IUmbracoBuilder AddInMemoryModelsRazorEngine ( this IUmbracoBuilder builder )
144145 {
145- // See notes in RefreshingRazorViewEngine for information on what this is doing.
146-
147- // copy the current collection, we need to use this later to rebuild a container
148- // to re-create the razor compiler provider
149- builder . Services . AddSingleton < UmbracoRazorReferenceManager > ( ) ;
150- builder . Services . AddSingleton < CompilationOptionsProvider > ( ) ;
151- builder . Services . AddSingleton < IViewCompilerProvider , UmbracoViewCompilerProvider > ( ) ;
152- builder . Services . AddSingleton < RuntimeCompilationCacheBuster > ( ) ;
153- builder . Services . AddSingleton < InMemoryAssemblyLoadContextManager > ( ) ;
154-
155- builder . Services . AddSingleton < InMemoryModelFactory > ( ) ;
156-
157- // This is what the community MB would replace, all of the above services are fine to be registered
158- // even if the community MB is in place.
159- builder . Services . AddSingleton < IPublishedModelFactory > ( factory =>
146+ // We should only add/replace these services when models builder is InMemory, otherwise we'll cause issues.
147+ // Since these services expect the ModelsMode to be InMemoryAuto
148+ if ( builder . Config . GetModelsMode ( ) is ModelsMode . InMemoryAuto )
160149 {
161- ModelsBuilderSettings modelsBuilderSettings = factory . GetRequiredService < IOptions < ModelsBuilderSettings > > ( ) . Value ;
162- if ( modelsBuilderSettings . ModelsMode == ModelsMode . InMemoryAuto )
163- {
164- return factory . GetRequiredService < InMemoryModelFactory > ( ) ;
165- }
166- else
167- {
168- return factory . CreateDefaultPublishedModelFactory ( ) ;
169- }
170- } ) ;
150+ builder . Services . AddSingleton < UmbracoRazorReferenceManager > ( ) ;
151+ builder . Services . AddSingleton < CompilationOptionsProvider > ( ) ;
152+ builder . Services . AddSingleton < IViewCompilerProvider , UmbracoViewCompilerProvider > ( ) ;
153+ builder . Services . AddSingleton < RuntimeCompilationCacheBuster > ( ) ;
154+ builder . Services . AddSingleton < InMemoryAssemblyLoadContextManager > ( ) ;
155+
156+ builder . Services . AddSingleton < InMemoryModelFactory > ( ) ;
157+ // Register the factory as IPublishedModelFactory
158+ builder . Services . AddSingleton < IPublishedModelFactory , InMemoryModelFactory > ( ) ;
159+ return builder ;
160+ }
171161
162+ // This is what the community MB would replace, all of the above services are fine to be registered
163+ builder . Services . AddSingleton < IPublishedModelFactory > ( factory => factory . CreateDefaultPublishedModelFactory ( ) ) ;
172164 return builder ;
173165 }
174166}
0 commit comments