首页 > 解决方案 > c# Webapi azure 发布错误:getaddrinfo ENOTFOUND

问题描述

我开发了一个.netcore webapi backend并尝试将其发布到 Azure。

如果我在本地运行它,它可以完美运行,但是当我将它发布到 Azure 时,它​​就不再工作了。

我总是收到这个错误:

错误:getaddrinfo ENOTFOUND www.xxxxx.azurewebsites.net

我不明白为什么,这是我的创业公司,其他任何事情都非常标准。

public class Startup
{
    private string _cors = "corsPolicy";

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public delegate IStorageService ServiceResolver(string key);

    // 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 =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
    
        services.AddControllers()
            .AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.IgnoreNullValues = true;
#if DEBUG
                o.JsonSerializerOptions.WriteIndented = true;
#else
                o.JsonSerializerOptions.WriteIndented = false;
#endif
            });

        services.AddCors(o => o.AddPolicy(_cors, builder =>
        {
            builder
                .AllowAnyOrigin()
                // .WithOrigins("http://localhost:4200")
                .AllowAnyHeader()
                .AllowAnyMethod();
        }));


        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));


        services.AddIdentity<ApplicationUser, IdentityRole>
                (options => options.SignIn.RequireConfirmedAccount = false)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

        services.AddAuthentication()
            .AddIdentityServerJwt();

        var jwtBearerProvider = new JwtBearerProvider(Encoding.ASCII.GetBytes(Configuration["Jwt:SecretKey"]));
        services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = true;
                x.SaveToken = true;
                x.TokenValidationParameters = jwtBearerProvider.ValidationParameters;
            });

        services.AddAuthorization(o =>
        {
            o.AddPolicy(AuthPolicies.AdminOnly, builder => builder.RequireRole(Roles.Admin));
            o.AddPolicy(AuthPolicies.EditorsOnly, builder
                => builder.RequireAssertion(c => c.User.IsInRole(Roles.Admin) || c.User.IsInRole(Roles.Editor)));
        });

        services.AddSingleton(jwtBearerProvider);

        services.AddScoped<ApplicationUsersService>();
        services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();

        services.AddScoped<...interfaces..., ...implementations...>();

        services.AddControllersWithViews();
        // .AddRazorRuntimeCompilation();

        services.AddRazorPages();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        var rewriteOptions = new RewriteOptions().AddRedirectToHttpsPermanent();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            // app.UseDatabaseErrorPage();
        }
        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();
            rewriteOptions.AddRedirectToWwwPermanent();
        }


        app.UseRewriter(rewriteOptions);

        var provider = new FileExtensionContentTypeProvider();
        provider.Mappings[".webmanifest"] = "application/manifest+json";

        app.UseStaticFiles(new StaticFileOptions
        {
            ContentTypeProvider = provider
        });

        app.UseRouting();

        app.UseCors(_cors);

        app.UseStatusCodePages(async c =>
        {
            if (c.HttpContext.Response.StatusCode == 401)
            {
                c.HttpContext.Response.Redirect("/Account/Login");
            }
        });
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });



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

        app.UseEndpoints(e =>
        {
            e.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            e.MapRazorPages();
        });
    }
}

我尝试在同一个服务应用程序上发布另一个 webapi 项目并且它可以工作,所以我很确定问题出在这个启动的某个地方或我周围某处的配置中。

我错过了什么?

标签: c#azure-web-app-serviceasp.net-core-webapi

解决方案


推荐阅读