我有一个应用程序 .Net core 3.1。对于自动作业执行,我已经实现了hangfire。在本地一切正常,但当我在服务器上发布它时,我收到 CORS 政策错误。
我已经在启动文件中添加了所有配置,如下所示。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
// configure strongly typed settings object
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddDataProtection();
// Register the Swagger generator,defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",new OpenApiInfo { Title = "Crowd-Recruitment",Version = "v1" });
});
services.AddControllers().AddNewtonsoftJson();
services.AddDbContext<recruitmentContext>(options =>
options.UseSqlServer(Configuration.getconnectionString("DevConnection")));
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.getconnectionString("DevConnection"),new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),QueuePollInterval = TimeSpan.Zero,UseRecommendedIsolationLevel = true,DisableGlobalLocks = true
}));
services.AddCors(options =>
{
options.AddPolicy("AllOrigins",builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
});
services.AddHangfireServer();
services.AddMvc();
// Auto Mapper Configurations
var mapperConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
services.AddSingleton(mapper);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
app.UseMiddleware<ErrorHandlingMiddleware>();
if (env.IsDevelopment())
{
///app.UseDeveloperExceptionPage();
}
app.UseHangfireDashboard();
app.UseHangfireServer();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML,JS,CSS,etc.),changes
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json","Crowd-Recruitment");
});
app.UseRouting();
app.UseCors("AllOrigins");
app.UseAuthentication();
app.UseAuthorization();
// custom jwt auth middleware
app.UseMiddleware<JwtMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHangfireDashboard();
});
RecurringJob.AddOrupdate(() => UpdatePackages(),Cron.Minutely());
}