fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.3k stars 1.59k forks source link

[BUG] Unable to load one or more of the requested types in FSH.WebApi.Infrastructure.Common.StartUp #724

Closed MarcoGaribaldi closed 2 years ago

MarcoGaribaldi commented 2 years ago

Hi!

I have recently cloned this project with the intent of studying the architecture. When I run the application I get an error at startup. The error occurs in Program.cs of the Host project when executing the line

builder.Services.AddInfrastructure(builder.Configuration);

The error occurs in the following code in FSH.WebApi.Infrastructure.Common.StartUp

    internal static IServiceCollection AddServices(this IServiceCollection services, Type interfaceType, ServiceLifetime lifetime)
    {
        var interfaceTypes =
            AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(t => interfaceType.IsAssignableFrom(t)
                            && t.IsClass && !t.IsAbstract)
                .Select(t => new
                {
                    Service = t.GetInterfaces().FirstOrDefault(),
                    Implementation = t
                })
                .Where(t => t.Service is not null
                            && interfaceType.IsAssignableFrom(t.Service));

        foreach (var type in interfaceTypes)
        {
            services.AddService(type.Service!, type.Implementation, lifetime);     <----- This is where the error occurs
        }

        return services;
    }   

This is the exception:

ex {"Unable to load one or more of the requested types.\r\nCould not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.\r\nCould not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.\r\nCould not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.\r\nCould not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified."} System.Exception {System.Reflection.ReflectionTypeLoadException}

Any idea on what may be wrong?

fretje commented 2 years ago

Hmm this looks strange... you simply cloned the project and build and ran it without any modifications?

System.ServiceModel version 4.0.0.0 feels like a .NET framework thing? Something must be mixed up with versions on your build system maybe?

What exactly is it trying to add (type.Service and type.Implementation) when that error gets thrown?

MarcoGaribaldi commented 2 years ago

Hi @fretje,

yes, I simply cloned the project with VS2022, no modifications. Just to be sure, I cloned the solution again and I get the same error.

System.ServiceModel is indeed a .NET framework thing. I did a search on Google about it, one suggestion I found was to install the nuget package System.ServiceModel.Primitives. This doesn't resolve the problem but the error is different, though still related to System.ServiceModel. After installing the nuget package the error I get is the following

ex {"Unable to load one or more of the requested types.\r\nCould not load type 'System.ServiceModel.Configuration.BehaviorExtensionElement' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'."} System.Exception {System.Reflection.ReflectionTypeLoadException}

None of the other solutions I have on this machine gives me this problem. To be sure it is not something related to versions in my computer, I tried cloning the solution to another computer and got the same error.

The exception is thrown in the code I posted in my first post when loading the IAuditService interface.

fretje commented 2 years ago

That is very strange. I can't repeat this. I also just cloned the project, and it builds and runs fine. Something database related maybe? You have Sql Server Express LocalDB installed? (that is what the default connectionstring is referring to).

MarcoGaribaldi commented 2 years ago

I do have Sql Server Express LocalDB installed. Migration creates the db with all its schemas, the tables are properly seeded.

2022-07-04 12_45_06-Playter – console  fullStackHeroDb  2
fretje commented 2 years ago

That's even stranger, as the database initialization happens after where you say you get that exception... So the database initialization shouldn't even be reached? If it does, that probably means the code where you get the exception (startup) is called twice somehow? It seems to be called again after the database initialization, but then in the context of .NET framework? Doesn't really make much sense... but that's all I can tell from what I see here ;-)

MarcoGaribaldi commented 2 years ago

Hmm... wait, I am not sure I follow you. The db is seeded when I execute dotnet run

E:\Source\Repos\Samples\dotnet-webapi-boilerplate\src\Host>dotnet run Building... [17:28:08 INF] Server Booting Up... [17:28:14 INF] Hangfire: Current Storage Provider : mssql [17:28:14 INF] For more Hangfire storage, visit https://www.hangfire.io/extensions.html [17:28:22 INF] Current DB Provider: mssql [17:28:25 INF] Applying Root Migrations. [17:28:26 INF] Applying Migrations for 'root' tenant. [17:28:26 INF] Connection to root's Database Succeeded. [17:28:26 INF] Seeding Admin Role for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Dashboard.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Hangfire.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.Search' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.Create' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.Delete' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Users.Export' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.UserRoles.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.UserRoles.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Roles.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Roles.Create' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Roles.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Roles.Delete' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.RoleClaims.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.RoleClaims.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.Search' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.Create' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.Delete' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Products.Export' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Search' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Create' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Delete' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Generate' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Brands.Clean' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Tenants.View' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Tenants.Create' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Tenants.Update' for 'root' Tenant. [17:28:26 INF] Seeding Admin Permission 'Permissions.Tenants.UpgradeSubscription' for 'root' Tenant. [17:28:26 INF] Seeding Basic Role for 'root' Tenant. [17:28:26 INF] Seeding Basic Permission 'Permissions.Products.View' for 'root' Tenant. [17:28:26 INF] Seeding Basic Permission 'Permissions.Products.Search' for 'root' Tenant. [17:28:26 INF] Seeding Basic Permission 'Permissions.Brands.View' for 'root' Tenant. [17:28:26 INF] Seeding Basic Permission 'Permissions.Brands.Search' for 'root' Tenant. [17:28:26 INF] Seeding Default Admin User for 'root' Tenant. [17:28:26 INF] Assigning Admin Role to Admin User for 'root' Tenant. [17:28:26 INF] Started to Seed Brands. [17:28:27 INF] Seeded Brands. [17:28:27 INF] For documentations and guides, visit https://www.fullstackhero.net [17:28:27 INF] To Sponsor this project, visit https://opencollective.com/fullstackhero [17:28:29 INF] Current DB Provider: mssql [17:28:29 INF] Now listening on: https://localhost:5001 [17:28:29 INF] Now listening on: http://localhost:5000 [17:28:29 INF] Application started. Press Ctrl+C to shut down. [17:28:29 INF] Hosting environment: NotDevelopment [17:28:29 INF] Content root path: E:\Source\Repos\Samples\dotnet-webapi-boilerplate\src\Host\

Are log files written in the \dotnet-webapi-boilerplate\src\Host\Logs? I don't understand why the exceptions are not logged.

MarcoGaribaldi commented 2 years ago

Ok, I am not sure why but after cloning the solution for the umpteen time I don't get an exception anymore. So, I close the issue.