c# - 在.Net core 3.1中添加hangfire后,应用程序在服务器上不起作用
问题描述
我有一个应用程序.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());
}
解决方案
推荐阅读
- android - 如何处理多个身份验证提供程序(firebase)
- python-3.x - 连接数据帧,重命名新索引,删除旧索引
- node.js - 拥有数千个虚拟用户的 Node.JS 负载测试工具
- openssl - nelem.h 在哪里?(对于 OpenSSL)
- javascript - 如何使用 Javascript 相对于时间更改 HTML 中文本的颜色?
- c# - HttpClient 不发送响应
- java - [Java][Spring-boot] @NotNull 注解不是 DDL
- regex - 我需要一个只接受这种格式 YYYYWWWW 的正则表达式(例如 2021WW12)
- html - Bootstrap 模态文本点击
- python - Selenium 在打开 Python 后自动关闭窗口