Czesc, macie jakis pomysl jak sobie poradzic z tym bledem ? Nwm jaki tez kod tu wrzucic zebyscie mieli jakiekolwiek spojrzenie na ten problem dlatego prosilbym o jakas sugestie co tu dodac. Ogolnie rzecz biorac apka jest w wersji .net core 2.2 i tu lezy problem bo gdy zrobilem bardzo podobna do niej apke ale w wersji 2.1.2 bodajze to wszystko dzialalo
Zainstaluj odpowiednią wersję ASP.NET Core/.NET Core: Runtime & Hosting Bundle i spróbuj czy pójdzie.
Aktualnie mam teraz taki blad HTTP Error 502.5 - Process Failure bo troche pozmienialem w kodzie i tamten zniknal.
Cos takiego mi wyrzuca
at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action1 sqlServerOptionsAction) at ProjectCars.Startup.<ConfigureServices>b__4_1(DbContextOptionsBuilder options) in C:\Users\admin\source\repos\Projects\ProjectCars\ProjectCars\Startup.cs:line 36 at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_0
2.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action2 optionsAction) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass10_0
1.<AddCoreServices>b__0(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at ProjectCars.Program.Main(String[] args) in C:\Users\admin\source\repos\Projects\ProjectCars\ProjectCars\Program.cs:line 31
Co masz w tej linii w Program.cs?
C:\Users\admin\source\repos\Projects\ProjectCars\ProjectCars\Program.cs:line 31
To wklej treść błędu, a nie sam stack trace.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ProjectCars.Models;
namespace ProjectCars
{
public class Program
{
public static void Main(string[] args)
{
//CreateWebHostBuilder(args).Build().Run();
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<AppDbContext>(); //<------------------------------- tutaj jest problem
context.Database.Migrate();
DbInitializer.Seed(context);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Czy w pliku Startup.cs
masz jakieś odwołanie to typu AppDbContext
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BookListMVC.Models; //Nalezy dodac samodzielnie przy tworzeniu ConnectionStringa
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ReflectionIT.Mvc.Paging; //Paginacja
namespace BookListMVC
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//services.AddPaging(); //Paginacja
services.AddPaging(options => {
options.ViewName = "Bootstrap4";
options.HtmlIndicatorDown = " <span>↓</span>";
options.HtmlIndicatorUp = " <span>↑</span>";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Zamień
var context = services.GetRequiredService<AppDbContext>();
//na
var context = services.GetRequiredService<ApplicationDbContext>();
Albo w pliku Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//na
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Mam przeczucie, że kod jest CTRL+C/CTRL+V
;-)
Który DbContext jest prawidłowy? ApplicationDbContext
czy AppDbContext
Wkleilem jeden plik z kodem, wlasnie z tego pierwotnego projektu gdzie inaczaj nazwalem DbContext, w tym projekcie teraz nosi on nazwe AppDbContext i we wszystkich miejscach , ktore wymieniles zgadza sie niestety ://
Jezeli chcesz to moge tutaj wrzucic linka do repo z tym bo pewnie dosc ciezko tak analizowac bez calkowitego wgladu w prace
Dla mnie problem leży w nazywaniu DbContext.
Skoro we wszystkich miejscach się on zgadza to dlaczego w startup wczytujesz ApplicationDbContext
, a w program AppDbContext
. Gdzieś coś nie gra w nazywaniu skoro to jest "kopiowanka".
Zmień w Program.cs
, w linijce 31
var context = services.GetRequiredService<AppDbContext>();
na
var context = services.GetRequiredService<ApplicationDbContext>();
To skąd tutaj masz ApplicationDbContext
?
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Tak wygladal oryginalnie ten plik w tym aktualnym projekcie, przypadkowo wyslalem z tego z, ktorego czesc byla kopiowana
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ProjectCars.Models;
namespace ProjectCars
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<ICarRepository, CarRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
I dalej masz ten sam błąd i ten sam stacktrace?
tak, pokazac ci moze .csproj ? bo dokonywalem tam modyfikacji i moze przez to tak sie dzieje bo zmienilem AspNetCoreHostingModel na InProcess
zasugerowalem sie tym wpisem i stad ta zmiana, w web.config nie zmienialem nic jak cos
https://stackoverflow.com/questions/53811569/using-netcore-2-2-and-using-the-in-process-hosting-model
Wrzuć, może się przyda, ale mi się już pomysły skończyły. Musisz poczekać na kogoś mądrzejszego ode mnie. Ja odszedłem od Entity Framework w projektach
https://github.com/LilKeyboard/ProjectCars.git
Tu jest jakis problem z wersja .net mi sie wydaje bo wczesniej ten projekt tworzylem w wersji 2.1.2 chyba i nie bylo tam tego bledu
Czytalem ze opcja jakas jest downgrade na wczesniejsza wersje .net ale nie do konca wiem jak go zrobic bo moj osstatni downgrade duzo mi namieszal w projekcie bo zle go zrobilem
A tego nie musisz zmienić na 2.2?
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Ja piszę projekty pod 3.0 Preview + Angular bez EF. Łapie się już totalnie każdego szczegółu...
no niestety to nie to, myslisz ze dobrym pomyslem bedzie downgrade do .net core 2.1 ?
Weź może od 0 utwórz ten projekt w 2.2+
Czemu od 0 ?
Ale utworzylem go juz w 2.2 to po co jeszcze raz ?
Moge go wlasciwie utworzyc od 0 jeszcze raz ale co mialbym zmienic bo na ta chwile zrobilbym go tak samo, no moze tyle bym zmienil ze bylby w wersji 2.1 bo tak byl pierwotnie. Myslisz zeby tak sprobowac ?
Masz mnóstwo błędów, na przyszłość polecam robić jedną zmianę i sprawdzać czy działa i dopiero potem robić kolejną.
- W metodzie main w klasie Program wykomentowałeś metodę Run(), spoiler alert bez niej serwer nie wystartuje. dodaje na końcu maina host.Run();
- Chcesz uzywać migracji, a nie masz dodanego nugeta Microsoft.EntityFrameworkCore.Tools
- nie masz ani jednej migracji == nie masz bazy, dodaj startową migrację,, polecenie Add-Migration
- w metodzie seed nie możesz nadawać id przy tak zdefiniowanym modelu