You may encounter these errors when migrating your project to .Net 6 or .Net 7. Below is the error.
1. 'The ConnectionString property has not been initialized.'
In .Net 6 or 7, use has to use the keyword ConnectionStrings instead of ConnectionString otherwise you will get an error, connection string property has not been initialized.
2. A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)'
If you are getting the above error this means you need to make the connection string encrypted false in app settings.
3. An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'Service.IService' while attempting to activate
In .Net 6 or 7, we don’t have a start-up file. So, you must include everything in Program.cs. file
// Register interface and classes
builder.Services.AddScoped<IHotelService,
HotelService>();
In Program.cs file, Register dependency before builder.build() command. It will resolve your issue.
4. InvalidOperationException: Unable to resolve service for type 'Data.LocatorDBContext' while attempting to activate 'Service.HotelService'. or
System.AggregateException: 'Some services are not able to be constructed
If you are facing the above issue this means you have not resolved the context in the program.cs and have not included the connection string in the Program.cs. Add the below lines and it will resolve the dependency.
builder.Services.AddDbContext<LocatorDBContext>(options
=>
options.UseSqlServer(builder.Configuration.GetConnectionString("LocatorDB")));
For UseSQLserver keyword, you need to include Nuget package.
Microsoft.EntityFrameworkcore.SQLServer
5. System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.'
This means you have not registered MVC views in Program.cs file. Just add the below line in program.cs file.
builder.Services.AddControllersWithViews();
No comments:
Post a Comment