Upgrade from CMS 11 to CMS 12 gives startup error

Vote:
 

Hi!

I'm trying to upgrade a site from CMS 11 to 12 and the site compiles but wont startup.

public class Program
{
    public static void Main(string[] args)
    {
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        var isDevelopment = environment == Environments.Development;

        if (isDevelopment)
        {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Warning()
                .WriteTo.File("app_data/log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();
        }


        CreateHostBuilder(args, isDevelopment).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args, bool isDevelopment)
    {
        if (isDevelopment)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(config =>
                {
                    config.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("COMPUTERNAME")}.json", optional: true, reloadOnChange: true);
                })
                .ConfigureCmsDefaults()
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }
        else
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureCmsDefaults()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }
    }
}
public class Startup
{
    private readonly IWebHostEnvironment _webHostingEnvironment;
    private readonly IConfiguration _configuration;

    
    public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
    {
        _configuration = configuration;
        _webHostingEnvironment = webHostEnvironment;
    }


    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
            .AddRazorOptions(ro => ro.ViewLocationExpanders.Add(new FeatureViewLocationExpander()));

        if (_webHostingEnvironment.IsDevelopment())
        {
            services.Configure<ClientResourceOptions>(uiOptions =>
            {
                uiOptions.Debug = true;
            });
        }

        services.AddEmbeddedLocalization<Startup>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(name: "Default", pattern: "{controller}/{action}/{id?}");
            endpoints.MapControllers();
            endpoints.MapContent();
        });

    }
}

gives me: 

InvalidOperationException: Sequence contains no matching element
System.Linq.ThrowHelper.ThrowNoMatchException()
System.Linq.Enumerable.First<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
EPiServer.DependencyInjection.ServiceLocatorProviderFactoryFacade<TContainerBuilder>.CreateBuilder(IServiceCollection services)
Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter<TContainerBuilder>.CreateBuilder(IServiceCollection services)
Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
Microsoft.Extensions.Hosting.HostBuilder.Build()
Web.Episerver12.Program.Main(string[] args) in Program.cs
+
            CreateHostBuilder(args, false).Build().Run();

I guess it could be the Core projects setup as well but I cannot figure this one out.

Thanks!

/Kristoffer

#270119
Jan 17, 2022 17:35
Vote:
 

Hi Kristoffer

It looks like you miss some services registration. Have a look of Startup file used in Foundation project

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCmsAspNetIdentity<SiteUser>();
            services.AddMvc(o => o.Conventions.Add(new FeatureConvention()))
                .AddRazorOptions(ro => ro.ViewLocationExpanders.Add(new FeatureViewLocationExpander()));

            if (_webHostingEnvironment.IsDevelopment())
            {
                services.Configure<ClientResourceOptions>(uiOptions => uiOptions.Debug = true);
            }

            services.AddCms();
            services.AddDisplay();
            services.AddTinyMce();
            services.AddFind();
            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/util/Login";
                options.ExpireTimeSpan = new TimeSpan(0, 20, 0);
                options.SlidingExpiration = true;
            });
            services.TryAddEnumerable(Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Singleton(typeof(IFirstRequestInitializer), typeof(ContentInstaller)));
            services.AddDetection();
        }

https://raw.githubusercontent.com/episerver/foundation-mvc-cms/net5/src/Foundation/Startup.cs

#270155
Jan 18, 2022 0:42
Vote:
 

Thanks Vincent!

I actually had this Startup.cs as my guidance:

https://github.com/episerver/Foundation/blob/net5/src/Foundation/Startup.cs

And that is for Commerce as well so the AddCms() I think was the key here.

/Kristoffer

#270157
Jan 18, 2022 7:20
Vincent - Jan 19, 2022 22:32
Glad to hear it's working. In commerce, AddCms() was being called by AddCommerce() internally.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.