K13 Medioclinic exception - Resolution of 'CMS.Base.ISiteService' failed

Aaron Macdonald asked on October 1, 2021 05:21

Hello

We are seeing an exception on startup of the live app when hosting in IIS, using K13 v13.0.41.

The context is a new project based on Medioclinic. Not much has been done yet and it hasn't deviated far from Medioclinic.

The exception only occurs in the case that we try to host the live app using IIS.

There is no problem running the live app via IISExpress

To host via IIS we have followed instructions provided by Microsoft and Kentico at these links:

  • https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/development-time-iis-support?view=aspnetcore-3.1
  • https://devnet.kentico.com/questions/do-you-have-to-publish-net-core

As per the instructions we are just trying to host the live app under Default Web Site with a binding like:

https://localhost/project-xzy

Domains have been set correctly under Sites in the admin app.

The infrastructure in this project is almost identical to medioclinic, it is using the same middleware, program.cs and startup.cs files.

We note that this same issue was raised by Sylvain C. on the 8th of September in the devnet link above, but no response was yet provided.

The relevant exception stack trace is below.


CMS.Core.ServiceResolutionException: Resolution of 'CMS.Base.ISiteService' failed with the following error: IoC container cannot be used for service resolution because it was not initialized yet. This means that the application was not properly pre-initialized or yet initialized. Use 'CMS.Core.Service.InitializeContainer' to perform container initialization.
 ---> System.InvalidOperationException: IoC container cannot be used for service resolution because it was not initialized yet. This means that the application was not properly pre-initialized or yet initialized. Use 'CMS.Core.Service.InitializeContainer' to perform container initialization.
   at CMS.Core.IoCContainer.get_ServiceProvider()
   at CMS.Core.IoCContainer.ResolveOptional[TService]()
   --- End of inner exception stack trace ---
   at CMS.Core.IoCContainer.ResolveOptional[TService]()
   at CMS.Core.Service.ResolveOptional[TService]()
   at XperienceAdapter.Logging.XperienceLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter) in C:\Users\***\Dropbox\wwwroot\***\XperienceAdapter\Logging\XperienceLogger.cs:line 48
   at Microsoft.Extensions.Logging.Logger.<Log>g__LoggerLog|12_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)

Correct Answer

Aaron Macdonald answered on October 12, 2021 02:43

We've identified the cause of the problem.

It is an issue with the medioclinic demo and not .NET configuration.

We were missing the last commit made to the demo in the file XperienceLogger.cs c2d18618, which has the message 'Fix IoC-related exceptions at startup'.

A small change was made which swallows exceptions in the case that ISiteService can't be resolved.

Having recently done the Kentico training it's not exactly clear why this change was required, but we assume there must be an edge case where the logger is hit before IOC is configured.

It's a bit disappointing that Kentico could only suggest that we had something wrong with our .NET configuration.

FYI the reason we've been determined to resolve this is that 'Development-time IIS support in Visual Studio for ASP.NET Core' provides a better development experience than repeatedly starting IIS Express, in addition to providing the full IIS feature set as in a production environment.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on October 4, 2021 20:24

There's a couple possibilities if this ONLY happens through IIS. One is it's not able to read the appsettings.json so it can't find the connection string and such. however usually when an interface isn't resolving properly, it's because something is messed up in the startup or middleware... but can be hard to figure out.

Also a common issue is not having the [Assembly: AssemblyDiscoverable] attribute on any included projects.

hit me up on tfayas@hbs.net if you still can't figure it out, i can try to do a quick call to help.

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on October 12, 2021 12:04

Hello Aaron MacDonald,

Good to read that I wasn't the only one facing the issue. I tried again on the MedioClinic project after updating the XperienceLogger class but I am still seeing some issues. The application is created in IIS, the application pool is also created but I am getting the following errors now

NullReferenceException: Object reference not set to an instance of an object.
CMS.Membership.Internal.VirtualContextAuthenticationConfiguration.get_Audience()

DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor()' on type 'VirtualContextRequestPrincipalRetriever'.
Autofac.Core.Activators.Reflection.BoundConstructor.Instantiate()

DependencyResolutionException: An exception was thrown while activating λ:Kentico.Web.Mvc.Internal.IRequestPrincipalRetriever[] -> Kentico.Content.Web.Mvc.VirtualContextRequestPrincipalRetriever.
Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)

I don't see what I am doing wrong and it is pretty frustrating to make it work on IIS. I really need to be able to have it working on my IIS as it is much more convenient for managing web farms, page builders and be ready for production. I hope you would be able to help me on this matter.

Sylvain

0 votesVote for this answer Mark as a Correct answer

Aaron Macdonald answered on October 13, 2021 01:35 (last edited on October 13, 2021 01:37)

Hi Sylvain

I'm not sure what's causing that particular exception, but I would suggest the following.

Ensure these are installed:

  • ASP.NET and web development workload (Visual Studio installer)
  • .NET Core cross-platform development workload (Visual Studio installer)
  • Latest .NET Core Runtime + Hosting Bundle + SDK

Do a clean install of the latest medioclinic demo with the IIS launch profile and verify that 'Development-time IIS support in VS' works as expected. It definitely should work.

Then if you have problems with another project, do a file diff between medioclinic and that project, looking particularly at configuration and infrastructure code.

0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on October 13, 2021 11:27

Thank you Aaron for your patience,

I have started everything from scratch, reinstall a new instance of Xperience, did a clean install of the MedioClinic project from GitHub,follow the "Xperience basics, development prerequistes" course from the training, verify all my dotnet installations, check that 'Development-time IIS support' was checked in VS installer but I still get the same error as mentioned before:

NullReferenceException: Object reference not set to an instance of an object.
CMS.Membership.Internal.VirtualContextAuthenticationConfiguration.get_Audience()

DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor()' on type 'VirtualContextRequestPrincipalRetriever'.
Autofac.Core.Activators.Reflection.BoundConstructor.Instantiate()

DependencyResolutionException: An exception was thrown while activating λ:Kentico.Web.Mvc.Internal.IRequestPrincipalRetriever[] -> Kentico.Content.Web.Mvc.VirtualContextRequestPrincipalRetriever.
Autofac.Core.Resolving.Middleware.ActivatorErrorHandlingMiddleware.Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)

The only error I saw during the first run of the new IIS profile is that the new application pool "MedioClinic AppPool" is created with the identity property set to "ApplicationPoolIdentity" which prevent it accessing the DB and needed to be changed to "NetworkService" but beside that, I really don't see what is wrong with my fresh installation and why I can't use the local IIS to make it work...

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.