簡體   English   中英

ASP.NET Core App無法在Localhost上運行

[英]ASP.NET Core App Doesn't Run On Localhost

我正在嘗試使用Angular運行ASP.NET Core 盡管能夠配置Angular項目,但不幸的是, ASP.NET Core項目成功運行,但在瀏覽器中得到以下信息:

HTTP Error 502.5 - Process Failure

Common causes of this issue:
The application process failed to start
The application process started but then stopped
The application process started but failed to listen on the configured port

讓我寫一下,我已經在另一台PC上成功運行了該項目,並且該PC具有與當前PC相同的配置。 兩者具有相同的核心版本-2.2。 我不確定為什么要達到上述要求,但希望能有一些解決方案。

Startup.cs

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;
        });


        #region MyRegion
        var connection = Configuration.GetConnectionString("DatabaseConnection");
        services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(connection, b => b.UseRowNumberForPaging()));

        var appSettingsSection = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingsSection);

        // configure jwt authentication
        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);
        services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });


        services.AddSingleton<IConfiguration>(Configuration);       
        services.AddTransient<ISchemeMaster, SchemeMasterConcrete>();
        services.AddTransient<IPlanMaster, PlanMasterConcrete>();
        services.AddTransient<IPeriodMaster, PeriodMasterConcrete>();
        services.AddTransient<IRole, RoleConcrete>();
        services.AddTransient<IMemberRegistration, MemberRegistrationConcrete>();
        services.AddTransient<IUsers, UsersConcrete>();
        services.AddTransient<IUsersInRoles, UsersInRolesConcrete>();
        services.AddTransient<IPaymentDetails, PaymentDetailsConcrete>();
        services.AddTransient<IRenewal, RenewalConcrete>();
        services.AddTransient<IReports, ReportsMaster>();
        services.AddTransient<IGenerateRecepit, GenerateRecepitConcrete>();
        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
        services.AddScoped<IUrlHelper>(implementationFactory =>
        {
            var actionContext = implementationFactory.GetService<IActionContextAccessor>().ActionContext;
            return new UrlHelper(actionContext);
        });
        #endregion

        // Start Registering and Initializing AutoMapper

        Mapper.Initialize(cfg => cfg.AddProfile<MappingProfile>());
        services.AddAutoMapper();

        // End Registering and Initializing AutoMapper

        services.AddMvc(options => { options.Filters.Add(typeof(CustomExceptionFilterAttribute)); })            
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)      
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
        });
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials()
                    .WithExposedHeaders("X-Pagination"));
        });
    }

    // 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");
        }

        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();

        app.UseCors("CorsPolicy");
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

appsettings.json

{
  "AppSettings": {
    "Secret": "6XJCIEJO41PQZNWJC4RR"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DatabaseConnection": "Data Source=.;Database=SampleDB;"
  }
}

更新 :它是版本2.2。 最初跳過該部分。

解決這個問題很愚蠢。 盡管讓我寫信給任何人遇到此問題,但只要檢查錯誤日志或輸出窗口即可查看所需的工具或.NET Core版本。 就我而言,我忘了安裝.NET Core 2.2 ,這是下面的鏈接:

.NET Core 2.2 SDK

感謝所有發表評論以解決此問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM