Ali*_*Ali 9 c# asp.net-mvc hangfire
我在 mvc 应用程序中使用 hangfire。我正在向用户发送预约提醒。我已经在我的应用程序中安装了 hangfire。我已经在 startup.cs 类中配置了 hangfire。但是当我运行该应用程序时,它会产生以下错误 JobStorage。当前属性值尚未初始化。您必须在使用 Hangfire Client 或 Server API 之前设置它。
using Hangfire;
using Hangfire.SqlServer;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using UKC.Data.Infrastructure;
using UKC.UI.Helper;
[assembly: OwinStartup(typeof(UKC.UI.App_Start.Startup))]
namespace UKC.UI.App_Start
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
GlobalConfiguration.Configuration
.UseSqlServerStorage("DbEntities");
app.UseHangfireDashboard();
app.UseHangfireServer();
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
你可以使用这条路:
1-安装Hangfire ->Hangfire.AspNetCore(v1.7.14) 和 Hangfire.Core(v1.7.14)
2-注册服务
class Program
{
static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add Hangfire services.
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("Server=-; Database=-; user=-; password=-;"));
// Add the processing server as IHostedService
services.AddHangfireServer();
}
Run Code Online (Sandbox Code Playgroud)
3-添加仪表板用户界面
public void Configure(IApplicationBuilder app, IBackgroundJobClient
backgroundJobs, IHostingEnvironment env)
{
app.UseHangfireDashboard();
backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from
Hangfire!"));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}
}
Run Code Online (Sandbox Code Playgroud)
4- 运行应用程序 由于我们创建了后台作业,因此还应该出现以下消息,该作业的唯一行为是将消息写入控制台。 来自 Hangfire 的世界,你好!
public static void InitializeHangFire()
{
var sqlStorage = new SqlServerStorage("connectionString");
var options = new BackgroundJobServerOptions
{
ServerName = "Test Server"
};
JobStorage.Current = sqlStorage;
}
Run Code Online (Sandbox Code Playgroud)
对于 1.7.30 版本,如果我像这样初始化 Hangfire:
services.AddHangfire(c => c.UseSqlServerStorage(configuration.GetConnectionString("Default")));
services.AddHangfireServer();
Run Code Online (Sandbox Code Playgroud)
然后尝试调用,例如,RecurringJob.AddOrUpdate在Configure方法中,我将从问题标题中得到异常。我本地没有,因为本地我有开发环境,并且applicationBuilder.UseHangfireDashboard()被调用。如果之前调用此方法,RecurringJob.AddOrUpdate将没有任何问题。
另一种方法是在调用之前注入IBackgroundJobClient(或从 IServiceProvider 获取,这并不重要)RecurringJob.AddOrUpdate。您不需要对IBackgroundJobClient实例执行任何操作,只需注入它即可。之后一切正常。我希望它能为某人节省时间。
此链接中有同样的问题。我希望这可以帮助你。
你能编写抛出异常的代码吗?我Startup在下面编写了您的类和测试控制器。效果很好。我没有遇到任何例外。
[RoutePrefix("")]
public class HomeController : ApiController
{
[Route(""), HttpGet]
public void Get()
{
Hangfire.BackgroundJob.Enqueue(() => Tasks.DoIt("test"));
Hangfire.BackgroundJob.Schedule(() => Tasks.InitializeJobs(), TimeSpan.FromSeconds(5));
}
}
public static class Tasks
{
public static void DoIt(string s)
{
Console.WriteLine(s);
}
public static void InitializeJobs()
{
Console.WriteLine(DateTime.Now.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14802 次 |
| 最近记录: |